From c0874dbd21c89854c68e8db7201f5557ab5e6259 Mon Sep 17 00:00:00 2001 From: Eric Holk Date: Wed, 8 Aug 2012 17:24:07 -0700 Subject: [PATCH] Adding try_send for pipes::chan and pipes::shared_chan --- src/libcore/pipes.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/libcore/pipes.rs b/src/libcore/pipes.rs index c72a777c0a8..e455d22e70e 100644 --- a/src/libcore/pipes.rs +++ b/src/libcore/pipes.rs @@ -877,6 +877,9 @@ trait channel { /// Sends a message. fn send(+x: T); + + /// Sends a message, or report if the receiver has closed the connection. + fn try_send(+x: T) -> bool; } /// A trait for things that can receive multiple messages. @@ -931,6 +934,18 @@ impl chan of channel for chan { self.endp = some( streamp::client::data(unwrap(endp), x)) } + + fn try_send(+x: T) -> bool { + let mut endp = none; + endp <-> self.endp; + match move streamp::client::try_data(unwrap(endp), x) { + some(next) => { + self.endp = some(move_it!(next)); + true + } + none => false + } + } } impl port of recv for port { @@ -1047,6 +1062,15 @@ impl chan of channel for shared_chan { chan.send(option::unwrap(x)) } } + + fn try_send(+x: T) -> bool { + let mut xx = some(x); + do self.with |chan| { + let mut x = none; + x <-> xx; + chan.try_send(option::unwrap(x)) + } + } } /// Converts a `chan` into a `shared_chan`.