pub struct Channel<S, R> { /* private fields */ }Expand description
One side of a bidirectional channel. This channel can send to and receive from its counterpart.
§Examples
let (l, r) = bichannel::channel();
l.send(1).unwrap();
assert_eq!(Ok(1), r.recv());
r.send(1).unwrap();
assert_eq!(Ok(1), l.recv());
Implementations§
Source§impl<S, R> Channel<S, R>
impl<S, R> Channel<S, R>
Sourcepub fn send(&self, s: S) -> Result<(), SendError<S>>
pub fn send(&self, s: S) -> Result<(), SendError<S>>
See mpsc::Sender::send
Attempts to send a value to the other side of this channel, returning it back if it could not be sent.
A successful send occurs when it is determined that the other end of
the channel has not hung up already. An unsuccessful send would be one
where the corresponding receiver has already been deallocated. Note
that a return value of Err means that the data will never be
received, but a return value of Ok does not mean that the data
will be received. It is possible for the corresponding receiver to
hang up immediately after this function returns Ok.
This method will never block the current thread.
§Examples
let (l, r) = bichannel::channel();
// This send is always successful
l.send(1).unwrap();
// This send will fail because the receiver is gone
drop(l);
assert_eq!(r.send(1).unwrap_err().0, 1);Sourcepub fn recv(&self) -> Result<R, RecvError>
pub fn recv(&self) -> Result<R, RecvError>
See mpsc::Receiver::recv
Attempts to wait for a value from the other side, returning an error if the other side has hung up.
This function will always block the current thread if there is no data available and it’s possible for more data to be sent. Once a message is sent from the other side then this will wake up and return that message.
If the corresponding channel has disconnected, or it disconnects while
this call is blocking, this call will wake up and return Err to
indicate that no more messages can ever be received on this channel.
However, since channels are buffered, messages sent before the disconnect
will still be properly received.
§Examples
use std::thread;
let (left, right) = bichannel::channel::<u8, u8>();
let _result = thread::spawn(move || {
right.send(1u8).unwrap();
}).join().unwrap();
assert_eq!(Ok(1), left.recv());Buffering behavior:
use std::sync::mpsc;
use std::thread;
use std::sync::mpsc::RecvError;
let (send, recv) = mpsc::channel();
let handle = thread::spawn(move || {
send.send(1u8).unwrap();
send.send(2).unwrap();
send.send(3).unwrap();
drop(send);
});
// wait for the thread to join so we ensure the sender is dropped
handle.join().unwrap();
assert_eq!(Ok(1), recv.recv());
assert_eq!(Ok(2), recv.recv());
assert_eq!(Ok(3), recv.recv());
assert_eq!(Err(RecvError), recv.recv());Sourcepub fn try_recv(&self) -> Result<R, TryRecvError>
pub fn try_recv(&self) -> Result<R, TryRecvError>
See mpsc::Receiver::try_recv.
Attempts to return a pending value from the other side without blocking.
This method will never block the caller in order to wait for data to become available. Instead, this will always return immediately with a possible option of pending data on the channel.
This is useful for a flavor of “optimistic check” before deciding to block on a receiver.
Compared with recv, this function has two failure cases instead of one
(one for disconnection, one for an empty buffer).
§Examples
let (_, right) = bichannel::channel::<(), ()>();
assert!(right.try_recv().is_err());