Skip to content

Commit c0874db

Browse files
committed
Adding try_send for pipes::chan and pipes::shared_chan
1 parent 1beb1f4 commit c0874db

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/libcore/pipes.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,9 @@ trait channel<T: send> {
877877

878878
/// Sends a message.
879879
fn send(+x: T);
880+
881+
/// Sends a message, or report if the receiver has closed the connection.
882+
fn try_send(+x: T) -> bool;
880883
}
881884

882885
/// A trait for things that can receive multiple messages.
@@ -931,6 +934,18 @@ impl chan<T: send> of channel<T> for chan<T> {
931934
self.endp = some(
932935
streamp::client::data(unwrap(endp), x))
933936
}
937+
938+
fn try_send(+x: T) -> bool {
939+
let mut endp = none;
940+
endp <-> self.endp;
941+
match move streamp::client::try_data(unwrap(endp), x) {
942+
some(next) => {
943+
self.endp = some(move_it!(next));
944+
true
945+
}
946+
none => false
947+
}
948+
}
934949
}
935950

936951
impl port<T: send> of recv<T> for port<T> {
@@ -1047,6 +1062,15 @@ impl chan<T: send> of channel<T> for shared_chan<T> {
10471062
chan.send(option::unwrap(x))
10481063
}
10491064
}
1065+
1066+
fn try_send(+x: T) -> bool {
1067+
let mut xx = some(x);
1068+
do self.with |chan| {
1069+
let mut x = none;
1070+
x <-> xx;
1071+
chan.try_send(option::unwrap(x))
1072+
}
1073+
}
10501074
}
10511075

10521076
/// Converts a `chan` into a `shared_chan`.

0 commit comments

Comments
 (0)