std::select! [-]  [+] [src]

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 receivers.

This macro is used to wait for the first event to occur on a number of receivers. It places no restrictions on the types of receivers given to this macro, this can be viewed as a heterogeneous select.

Example

fn main() { use std::thread::Thread; let (tx1, rx1) = channel(); let (tx2, rx2) = channel(); fn long_running_task() {} fn calculate_the_answer() -> int { 42i } Thread::spawn(move|| { long_running_task(); tx1.send(()) }).detach(); Thread::spawn(move|| { tx2.send(calculate_the_answer()) }).detach(); select! ( () = rx1.recv() => println!("the long running task finished first"), answer = rx2.recv() => { println!("the answer was: {}", answer); } ) }
use std::thread::Thread;

let (tx1, rx1) = channel();
let (tx2, rx2) = channel();

Thread::spawn(move|| { long_running_task(); tx1.send(()) }).detach();
Thread::spawn(move|| { tx2.send(calculate_the_answer()) }).detach();

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.