std::macros::select
macro_rules! select { ( $($name:pat = $rx:ident.$meth:ident() => $code:expr),+ ) => ({ use std::comm::Select; let sel = Select::new(); $( let mut $rx = sel.handle(&$rx); )+ unsafe { $( $rx.add(); )+ } let ret = sel.wait(); $( if ret == $rx.id() { let $name = $rx.$meth(); $code } else )+ { unreachable!() } }) }
A macro to select an event from a number of ports.
This macro is used to wait for the first event to occur on a number of ports. It places no restrictions on the types of ports given to this macro, this can be viewed as a heterogeneous select.
Example
let (tx1, rx1) = channel(); let (tx2, rx2) = channel(); spawn(proc() { long_running_task(); tx1.send(()) }); spawn(proc() { tx2.send(calculate_the_answer()) }); select! ( () = rx1.recv() => println!("the long running task finished first"), answer = rx2.recv() => { println!("the answer was: {}", answer); } )
For more information about select, see the std::comm::Select
structure.