Skip to content

Instantly share code, notes, and snippets.

@bblum
Last active December 20, 2015 00:38
Show Gist options
  • Save bblum/6042762 to your computer and use it in GitHub Desktop.
Save bblum/6042762 to your computer and use it in GitHub Desktop.
17 /// Trait for message-passing primitives that can be select()ed on.
18 pub trait Select {
19 // Returns true if data was available.
20 fn optimistic_check(&mut self) -> bool;
21 // Returns true if data was available. If so, shall also wake() the task.
22 fn block_on(&mut self, &mut Scheduler, BlockedTask) -> bool;
23 // Returns true if data was available.
24 fn unblock_from(&mut self) -> bool;
25 }
26
27 /// Trait for message-passing primitives that can use the select2() convenience wrapper.
28 // (This is separate from the above trait to enable heterogeneous lists of ports
29 // that implement Select on different types to use select().)
30 pub trait SelectPort<T> : Select {
31 fn recv_ready(self) -> Option<T>;
32 }
......
43 pub fn select<A: Select>(ports: &mut [A]) -> uint {
......
87 pub fn select2<TA, A: SelectPort<TA>, TB, B: SelectPort<TB>>(mut a: A, mut b: B)
88 -> Either<(Option<TA>, B), (A, Option<TB>)> {
89 let result = {
90 let mut ports = [&mut a as &mut Select, &mut b as &mut Select];
91 select(ports)
92 };
93 match result {
94 0 => Left ((a.recv_ready(), b)),
95 1 => Right((a, b.recv_ready())),
96 x => fail!("impossible case in select2: %?", x)
97 }
98 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment