pub struct Rx<T> { /* private fields */ }Expand description
A single consumer (receiver) that works in a blocking context.
Additional methods in ChannelShared can be accessed through Deref.
NOTE: Rx is not Clone or Sync.
If you need concurrent access, use MRx instead.
Rx has a Send marker and can be moved to other threads.
The following code is OK:
use crossfire::*;
let (tx, rx) = mpsc::bounded_blocking::<usize>(100);
std::thread::spawn(move || {
let _ = rx.recv();
});
drop(tx);Because Rx does not have a Sync marker, using Arc<Rx> will lose the Send marker.
For your safety, the following code should not compile:
use crossfire::*;
use std::sync::Arc;
let (tx, rx) = mpsc::bounded_blocking::<usize>(100);
let rx = Arc::new(rx);
std::thread::spawn(move || {
let _ = rx.recv();
});
drop(tx);Implementations§
Source§impl<T> Rx<T>
impl<T> Rx<T>
Sourcepub fn recv<'a>(&'a self) -> Result<T, RecvError>
pub fn recv<'a>(&'a self) -> Result<T, RecvError>
Receives a message from the channel. This method will block until a message is received or the channel is closed.
Returns Ok(T) on success.
Returns Err(RecvError) if the sender has been dropped.
Sourcepub fn try_recv(&self) -> Result<T, TryRecvError>
pub fn try_recv(&self) -> Result<T, TryRecvError>
Attempts to receive a message from the channel without blocking.
Returns Ok(T) when successful.
Returns Err(TryRecvError::Empty) if the channel is empty.
Returns Err(TryRecvError::Disconnected) if the sender has been dropped and the channel is empty.
Sourcepub fn recv_timeout(&self, timeout: Duration) -> Result<T, RecvTimeoutError>
pub fn recv_timeout(&self, timeout: Duration) -> Result<T, RecvTimeoutError>
Receives a message from the channel with a timeout. Will block when channel is empty.
The behavior is atomic: the message is either received successfully or the operation is canceled due to a timeout.
Returns Ok(T) when successful.
Returns Err(RecvTimeoutError::Timeout) when a message could not be received because the channel is empty and the operation timed out.
Returns Err(RecvTimeoutError::Disconnected) if the sender has been dropped and the channel is empty.