pub struct Receiver<T>(/* private fields */);Expand description
The receiving half of a channel.
Receivers can be cloned and shared among multiple threads.
§Examples
use std::thread;
use std::time::Duration;
use crossbeam_channel::unbounded;
let (tx, rx) = unbounded();
thread::spawn(move || {
tx.send("Hello world!").unwrap();
thread::sleep(Duration::from_secs(2)); // Block for two seconds.
tx.send("Delayed for 2 seconds").unwrap();
});
println!("{}", rx.recv().unwrap()); // Received immediately.
println!("Waiting...");
println!("{}", rx.recv().unwrap()); // Received after 2 seconds.Implementations§
Source§impl<T> Receiver<T>
impl<T> Receiver<T>
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.
This method will never block in order to wait for a message to become available. Instead, this will always return immediately with a message if there is one, or an error if the channel is empty or disconnected.
If called on a zero-capacity channel, this method will receive a message only if there happens to be a send operation on the other side of the channel at the same time.
§Examples
use crossbeam_channel::{unbounded, TryRecvError};
let (tx, rx) = unbounded();
assert_eq!(rx.try_recv(), Err(TryRecvError::Empty));
tx.send(5).unwrap();
drop(tx);
assert_eq!(rx.try_recv(), Ok(5));
assert_eq!(rx.try_recv(), Err(TryRecvError::Disconnected));Sourcepub fn recv(&self) -> Result<T, RecvError>
pub fn recv(&self) -> Result<T, RecvError>
Waits for a message to be received from the channel.
This method will always block in order to wait for a message to become available. If the channel is (or becomes) empty and disconnected, this call will wake up and return an error.
If called on a zero-capacity channel, this method will wait for a send operation to appear on the other side of the channel.
§Examples
use std::thread;
use std::time::Duration;
use crossbeam_channel::unbounded;
let (tx, rx) = unbounded();
thread::spawn(move || {
thread::sleep(Duration::from_secs(1));
tx.send(5).unwrap();
drop(tx);
});
assert_eq!(rx.recv(), Ok(5));
assert!(rx.recv().is_err());Sourcepub fn recv_timeout(&self, timeout: Duration) -> Result<T, RecvTimeoutError>
pub fn recv_timeout(&self, timeout: Duration) -> Result<T, RecvTimeoutError>
Waits for a message to be received from the channel but only for a limited time.
This method will always block in order to wait for a message to become available. If the
channel is (or becomes) empty and disconnected, or if it waits for longer than timeout,
it will wake up and return an error.
If called on a zero-capacity channel, this method will wait for a send operation to appear on the other side of the channel.
§Examples
use std::thread;
use std::time::Duration;
use crossbeam_channel::{unbounded, RecvTimeoutError};
let (tx, rx) = unbounded();
thread::spawn(move || {
thread::sleep(Duration::from_secs(1));
tx.send(5).unwrap();
drop(tx);
});
assert_eq!(rx.recv_timeout(Duration::from_millis(500)), Err(RecvTimeoutError::Timeout));
assert_eq!(rx.recv_timeout(Duration::from_secs(1)), Ok(5));
assert_eq!(rx.recv_timeout(Duration::from_secs(1)), Err(RecvTimeoutError::Disconnected));Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the channel is empty.
Zero-capacity channels are always empty.
§Examples
use crossbeam_channel::unbounded;
let (tx, rx) = unbounded();
assert!(rx.is_empty());
tx.send(0).unwrap();
assert!(!rx.is_empty());
// Drop the only sender, thus disconnecting the channel.
drop(tx);
// Even a disconnected channel can be non-empty.
assert!(!rx.is_empty());Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of messages in the channel.
§Examples
use crossbeam_channel::unbounded;
let (tx, rx) = unbounded();
assert_eq!(rx.len(), 0);
tx.send(1).unwrap();
tx.send(2).unwrap();
assert_eq!(rx.len(), 2);Sourcepub fn capacity(&self) -> Option<usize>
pub fn capacity(&self) -> Option<usize>
If the channel is bounded, returns its capacity.
§Examples
use crossbeam_channel::{bounded, unbounded};
let (tx, _) = unbounded::<i32>();
assert_eq!(tx.capacity(), None);
let (tx, _) = bounded::<i32>(5);
assert_eq!(tx.capacity(), Some(5));
let (tx, _) = bounded::<i32>(0);
assert_eq!(tx.capacity(), Some(0));Sourcepub fn is_disconnected(&self) -> bool
pub fn is_disconnected(&self) -> bool
Returns true if the channel is disconnected.
§Examples
use crossbeam_channel::unbounded;
let (tx, rx) = unbounded::<i32>();
tx.send(1).unwrap();
assert!(!rx.is_disconnected());
drop(tx);
assert!(rx.is_disconnected());
assert_eq!(rx.recv(), Ok(1));Sourcepub fn iter(&self) -> Iter<'_, T> ⓘ
pub fn iter(&self) -> Iter<'_, T> ⓘ
Returns an iterator that waits for messages until the channel is disconnected.
Each call to next will block waiting for the next message. It will finally return None
when the channel is empty and disconnected.
§Examples
use std::thread;
use crossbeam_channel::unbounded;
let (tx, rx) = unbounded::<i32>();
thread::spawn(move || {
tx.send(1).unwrap();
tx.send(2).unwrap();
tx.send(3).unwrap();
});
let v: Vec<_> = rx.iter().collect();
assert_eq!(v, [1, 2, 3]);Sourcepub fn try_iter(&self) -> TryIter<'_, T> ⓘ
pub fn try_iter(&self) -> TryIter<'_, T> ⓘ
Returns an iterator that receives messages until the channel is empty or disconnected.
Each call to next will return a message if there is at least one in the channel. The
iterator will never block waiting for new messages.
§Examples
use std::thread;
use std::time::Duration;
use crossbeam_channel::unbounded;
let (tx, rx) = unbounded::<i32>();
thread::spawn(move || {
thread::sleep(Duration::from_secs(1));
tx.send(1).unwrap();
tx.send(2).unwrap();
thread::sleep(Duration::from_secs(2));
tx.send(3).unwrap();
});
thread::sleep(Duration::from_secs(2));
let v: Vec<_> = rx.try_iter().collect();
assert_eq!(v, [1, 2]);Sourcepub fn disconnect(&self) -> bool
pub fn disconnect(&self) -> bool
Disconnects the channel.
Returns true if this call disconnected the channel and false if it was already
disconnected.
Disconnection prevents any further messages from being sent into the channel, while still allowing the receiver to drain any existing buffered messages.
§Examples
use crossbeam_channel::unbounded;
let (tx, rx) = unbounded::<i32>();
tx.send(1);
tx.send(2);
tx.disconnect();
assert_eq!(rx.recv(), Ok(1));
assert_eq!(rx.recv(), Ok(2));
assert!(rx.recv().is_err());Trait Implementations§
Source§impl<'a, T> IntoIterator for &'a Receiver<T>
impl<'a, T> IntoIterator for &'a Receiver<T>
Source§impl<T> IntoIterator for Receiver<T>
impl<T> IntoIterator for Receiver<T>
Source§impl<T> Ord for Receiver<T>
impl<T> Ord for Receiver<T>
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<T> PartialOrd for Receiver<T>
impl<T> PartialOrd for Receiver<T>
impl<T> Eq for Receiver<T>
impl<T> Send for Receiver<T>where
T: Send,
impl<T> Sync for Receiver<T>where
T: Send,
Auto Trait Implementations§
impl<T> Freeze for Receiver<T>
impl<T> !RefUnwindSafe for Receiver<T>
impl<T> Unpin for Receiver<T>
impl<T> !UnwindSafe for Receiver<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more