Struct crossbeam_channel::Receiver
[−]
[src]
pub struct Receiver<T>(_);
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.
Methods
impl<T> Receiver<T>
[src]
pub fn try_recv(&self) -> Result<T, TryRecvError>
[src]
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));
pub fn recv(&self) -> Result<T, RecvError>
[src]
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());
pub fn recv_timeout(&self, timeout: Duration) -> Result<T, RecvTimeoutError>
[src]
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));
pub fn is_empty(&self) -> bool
[src]
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());
pub fn len(&self) -> usize
[src]
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);
pub fn capacity(&self) -> Option<usize>
[src]
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));
pub fn is_disconnected(&self) -> bool
[src]
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));
ⓘImportant traits for Iter<'a, T>pub fn iter(&self) -> Iter<T>
[src]
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]);
ⓘImportant traits for TryIter<'a, T>pub fn try_iter(&self) -> TryIter<T>
[src]
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]);
pub fn disconnect(&self) -> bool
[src]
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
impl<T: Send> Send for Receiver<T>
[src]
impl<T: Send> Sync for Receiver<T>
[src]
impl<T> Drop for Receiver<T>
[src]
impl<T> Clone for Receiver<T>
[src]
fn clone(&self) -> Self
[src]
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more
impl<T> Debug for Receiver<T>
[src]
fn fmt(&self, f: &mut Formatter) -> Result
[src]
Formats the value using the given formatter. Read more
impl<T> Hash for Receiver<T>
[src]
fn hash<H: Hasher>(&self, state: &mut H)
[src]
Feeds this value into the given [Hasher
]. Read more
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
H: Hasher,
Feeds a slice of this type into the given [Hasher
]. Read more
impl<T> PartialEq for Receiver<T>
[src]
fn eq(&self, other: &Receiver<T>) -> bool
[src]
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests for !=
.
impl<T> Eq for Receiver<T>
[src]
impl<T> PartialOrd for Receiver<T>
[src]
fn partial_cmp(&self, other: &Receiver<T>) -> Option<Ordering>
[src]
This method returns an ordering between self
and other
values if one exists. Read more
fn lt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than (for self
and other
) and is used by the <
operator. Read more
fn le(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
fn gt(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
fn ge(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
impl<T> Ord for Receiver<T>
[src]
fn cmp(&self, other: &Receiver<T>) -> Ordering
[src]
This method returns an Ordering
between self
and other
. Read more
fn max(self, other: Self) -> Self
1.21.0[src]
Compares and returns the maximum of two values. Read more
fn min(self, other: Self) -> Self
1.21.0[src]
Compares and returns the minimum of two values. Read more
impl<'a, T> IntoIterator for &'a Receiver<T>
[src]
type Item = T
The type of the elements being iterated over.
type IntoIter = Iter<'a, T>
Which kind of iterator are we turning this into?
fn into_iter(self) -> Self::IntoIter
[src]
Creates an iterator from a value. Read more