Skip to content

std: Export the select! macro #12603

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions src/libstd/comm/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,6 @@ use rt::task::{Task, BlockedTask};
use super::Port;
use uint;

macro_rules! select {
(
$($name:pat = $port:ident.$meth:ident() => $code:expr),+
) => ({
use std::comm::Select;
let sel = Select::new();
$( let mut $port = sel.handle(&$port); )+
unsafe {
$( $port.add(); )+
}
let ret = sel.wait();
$( if ret == $port.id() { let $name = $port.$meth(); $code } else )+
{ unreachable!() }
})
}

/// The "port set" of the select interface. This structure is used to manage a
/// set of ports which are being selected over.
pub struct Select {
Expand Down
44 changes: 44 additions & 0 deletions src/libstd/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,3 +368,47 @@ macro_rules! vec(
})
)


/// 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 (p1, c1) = Chan::new();
/// let (p2, c2) = Chan::new();
/// # fn long_running_task() {}
/// # fn calculate_the_answer() -> int { 42 }
///
/// spawn(proc() { long_running_task(); c1.send(()) });
/// spawn(proc() { c2.send(calculate_the_answer()) });
///
/// select! (
/// () = p1.recv() => println!("the long running task finished first"),
/// answer = p2.recv() => {
/// println!("the answer was: {}", answer);
/// }
/// )
/// ```
///
/// For more information about select, see the `std::comm::Select` structure.
#[macro_export]
#[experimental]
macro_rules! select {
(
$($name:pat = $port:ident.$meth:ident() => $code:expr),+
) => ({
use std::comm::Select;
let sel = Select::new();
$( let mut $port = sel.handle(&$port); )+
unsafe {
$( $port.add(); )+
}
let ret = sel.wait();
$( if ret == $port.id() { let $name = $port.$meth(); $code } else )+
{ unreachable!() }
})
}