Skip to content

Instantly share code, notes, and snippets.

@olsonjeffery
Last active December 22, 2015 17:19
Show Gist options
  • Save olsonjeffery/6505233 to your computer and use it in GitHub Desktop.
Save olsonjeffery/6505233 to your computer and use it in GitHub Desktop.
time for bed, old man!
use std::rt::comm;
use std::rt::io::timer::Timer;
use std::cell::Cell;
trait SleepyPort<T: Send> {
fn recv_timeout(self, msecs: u64) -> Option<T>;
}
impl<T: Send> SleepyPort<T> for comm::PortOne<T> {
fn recv_timeout(self, msecs: u64) -> Option<T> {
if self.peek() { return Some(self.recv()); }
match Timer::new() {
Some(ref mut t) => t.sleep(msecs),
None => {} // this probably raised a condition...
}
match self.peek() {
true => Some(self.recv()),
_ => None
}
}
}
fn main() {
println("bleh");
let (p, c) = comm::oneshot();
let c_cell = Cell::new(c);
do spawn { let c = c_cell.take(); c.send("f") }
let r = p.recv_timeout(2000).unwrap();
println(r);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment