Skip to content

Commit 62d4f8f

Browse files
committed
Added a select2 trait. Fixes #2898
1 parent 531ea69 commit 62d4f8f

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

src/libcore/pipes.rs

+49
Original file line numberDiff line numberDiff line change
@@ -822,3 +822,52 @@ impl chan<T: send> of channel<T> for shared_chan<T> {
822822
fn shared_chan<T:send>(+c: chan<T>) -> shared_chan<T> {
823823
arc::exclusive(c)
824824
}
825+
826+
trait select2<T: send, U: send> {
827+
fn try_select() -> either<option<T>, option<U>>;
828+
fn select() -> either<T, U>;
829+
}
830+
831+
impl<T: send, U: send, Left: selectable recv<T>, Right: selectable recv<U>>
832+
of select2<T, U> for (Left, Right) {
833+
834+
fn select() -> either<T, U> {
835+
alt self {
836+
(lp, rp) {
837+
alt select2i(lp, rp) {
838+
left(()) { left (lp.recv()) }
839+
right(()) { right(rp.recv()) }
840+
}
841+
}
842+
}
843+
}
844+
845+
fn try_select() -> either<option<T>, option<U>> {
846+
alt self {
847+
(lp, rp) {
848+
alt select2i(lp, rp) {
849+
left(()) { left (lp.try_recv()) }
850+
right(()) { right(rp.try_recv()) }
851+
}
852+
}
853+
}
854+
}
855+
}
856+
857+
#[cfg(test)]
858+
mod test {
859+
#[test]
860+
fn test_select2() {
861+
let (c1, p1) = pipes::stream();
862+
let (c2, p2) = pipes::stream();
863+
864+
c1.send("abc");
865+
866+
alt (p1, p2).select() {
867+
right(_) { fail }
868+
_ { }
869+
}
870+
871+
c2.send(123);
872+
}
873+
}

0 commit comments

Comments
 (0)