Adding try_send for pipes::chan and pipes::shared_chan
This commit is contained in:
parent
1beb1f491f
commit
c0874dbd21
@ -877,6 +877,9 @@ trait channel<T: send> {
|
||||
|
||||
/// 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<T: send> of channel<T> for chan<T> {
|
||||
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<T: send> of recv<T> for port<T> {
|
||||
@ -1047,6 +1062,15 @@ impl chan<T: send> of channel<T> for shared_chan<T> {
|
||||
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`.
|
||||
|
Loading…
x
Reference in New Issue
Block a user