Skip to content

Commit 98782bb

Browse files
committed
std: Export the select! macro
Mark it as #[experimental] for now. In theory this attribute will be read in the future. I believe that the implementation is solid enough for general use, although I would not be surprised if there were bugs in it still. I think that it's at the point now where public usage of it will start to uncover hopefully the last few remaining bugs. Closes #12044
1 parent 1bcd825 commit 98782bb

File tree

2 files changed

+44
-16
lines changed

2 files changed

+44
-16
lines changed

src/libstd/comm/select.rs

-16
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,6 @@ use rt::task::{Task, BlockedTask};
5858
use super::Port;
5959
use uint;
6060

61-
macro_rules! select {
62-
(
63-
$($name:pat = $port:ident.$meth:ident() => $code:expr),+
64-
) => ({
65-
use std::comm::Select;
66-
let sel = Select::new();
67-
$( let mut $port = sel.handle(&$port); )+
68-
unsafe {
69-
$( $port.add(); )+
70-
}
71-
let ret = sel.wait();
72-
$( if ret == $port.id() { let $name = $port.$meth(); $code } else )+
73-
{ unreachable!() }
74-
})
75-
}
76-
7761
/// The "port set" of the select interface. This structure is used to manage a
7862
/// set of ports which are being selected over.
7963
pub struct Select {

src/libstd/macros.rs

+44
Original file line numberDiff line numberDiff line change
@@ -368,3 +368,47 @@ macro_rules! vec(
368368
})
369369
)
370370

371+
372+
/// A macro to select an event from a number of ports.
373+
///
374+
/// This macro is used to wait for the first event to occur on a number of
375+
/// ports. It places no restrictions on the types of ports given to this macro,
376+
/// this can be viewed as a heterogeneous select.
377+
///
378+
/// # Example
379+
///
380+
/// ```
381+
/// let (p1, c1) = Chan::new();
382+
/// let (p2, c2) = Chan::new();
383+
/// # fn long_running_task() {}
384+
/// # fn calculate_the_answer() -> int { 42 }
385+
///
386+
/// spawn(proc() { long_running_task(); c1.send(()) });
387+
/// spawn(proc() { c2.send(calculate_the_answer()) });
388+
///
389+
/// select! (
390+
/// () = p1.recv() => println!("the long running task finished first"),
391+
/// answer = p2.recv() => {
392+
/// println!("the answer was: {}", answer);
393+
/// }
394+
/// )
395+
/// ```
396+
///
397+
/// For more information about select, see the `std::comm::Select` structure.
398+
#[macro_export]
399+
#[experimental]
400+
macro_rules! select {
401+
(
402+
$($name:pat = $port:ident.$meth:ident() => $code:expr),+
403+
) => ({
404+
use std::comm::Select;
405+
let sel = Select::new();
406+
$( let mut $port = sel.handle(&$port); )+
407+
unsafe {
408+
$( $port.add(); )+
409+
}
410+
let ret = sel.wait();
411+
$( if ret == $port.id() { let $name = $port.$meth(); $code } else )+
412+
{ unreachable!() }
413+
})
414+
}

0 commit comments

Comments
 (0)