about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2014-02-23 21:30:18 -0800
committerAlex Crichton <alex@alexcrichton.com>2014-02-27 21:04:05 -0800
commit98782bb5c9b0196cd10e3cfaf07cd02c6fe9f2f3 (patch)
tree7ab866c082727a73bab73f6242507c23ad626246 /src/libstd
parent1bcd8252ee22ea4f153de812e9bf83e3cfe4f8e0 (diff)
downloadrust-98782bb5c9b0196cd10e3cfaf07cd02c6fe9f2f3.tar.gz
rust-98782bb5c9b0196cd10e3cfaf07cd02c6fe9f2f3.zip
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
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/comm/select.rs16
-rw-r--r--src/libstd/macros.rs44
2 files changed, 44 insertions, 16 deletions
diff --git a/src/libstd/comm/select.rs b/src/libstd/comm/select.rs
index 02066086ad7..75e7265705a 100644
--- a/src/libstd/comm/select.rs
+++ b/src/libstd/comm/select.rs
@@ -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 {
diff --git a/src/libstd/macros.rs b/src/libstd/macros.rs
index 490f2c9b198..e16d944fb46 100644
--- a/src/libstd/macros.rs
+++ b/src/libstd/macros.rs
@@ -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!() }
+    })
+}