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); + } +}