pub struct Rx<T> { /* private fields */ }Expand description
Single consumer (receiver) that works in blocking context.
Additional methods can be accessed through Deref<Target=ChannelShared>.
NOTE: Rx is not Clone, nor Sync. If you need concurrent access, use MRx instead.
Rx has Send marker, can be moved to other thread. 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 Sync marker, using Arc<Rx> will lose 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>
Receive message, will block when channel is empty.
Returns Ok(T) when successful.
Returns Err(RecvError) when all Tx dropped.
Sourcepub fn try_recv(&self) -> Result<T, TryRecvError>
pub fn try_recv(&self) -> Result<T, TryRecvError>
Try to receive message, non-blocking.
Returns Ok(T) when successful.
Returns Err(TryRecvError::Empty) when channel is empty.
returns Err(TryRecvError::Disconnected) when all Tx dropped and channel is empty.
Sourcepub fn recv_timeout(&self, duration: Duration) -> Result<T, RecvTimeoutError>
pub fn recv_timeout(&self, duration: Duration) -> Result<T, RecvTimeoutError>
Waits for a message to be received from the channel, but only for a limited time. Will block when channel is empty.
The behavior is atomic, either successfully polls a message, or operation cancelled due to 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) when all Tx dropped and channel is empty.