From 62d4f8fe825c907bd03c275f85aeeaf7b25c4336 Mon Sep 17 00:00:00 2001 From: Eric Holk Date: Wed, 25 Jul 2012 14:46:15 -0700 Subject: [PATCH] Added a select2 trait. Fixes #2898 --- src/libcore/pipes.rs | 49 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/src/libcore/pipes.rs b/src/libcore/pipes.rs index a999d615e31b..4ffa040250f6 100644 --- a/src/libcore/pipes.rs +++ b/src/libcore/pipes.rs @@ -822,3 +822,52 @@ impl chan of channel for shared_chan { fn shared_chan(+c: chan) -> shared_chan { arc::exclusive(c) } + +trait select2 { + fn try_select() -> either, option>; + fn select() -> either; +} + +impl, Right: selectable recv> + of select2 for (Left, Right) { + + fn select() -> either { + alt self { + (lp, rp) { + alt select2i(lp, rp) { + left(()) { left (lp.recv()) } + right(()) { right(rp.recv()) } + } + } + } + } + + fn try_select() -> either, option> { + alt self { + (lp, rp) { + alt select2i(lp, rp) { + left(()) { left (lp.try_recv()) } + right(()) { right(rp.try_recv()) } + } + } + } + } +} + +#[cfg(test)] +mod test { + #[test] + fn test_select2() { + let (c1, p1) = pipes::stream(); + let (c2, p2) = pipes::stream(); + + c1.send("abc"); + + alt (p1, p2).select() { + right(_) { fail } + _ { } + } + + c2.send(123); + } +}