Struct ergo_sync::ch::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]

[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));

[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());

[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));

[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());

[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);

[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));

[src]

Returns true if the channel is disconnected (i.e. there are no senders).

Examples

use crossbeam_channel::unbounded;

let (tx, rx) = unbounded::<i32>();
assert!(!rx.is_disconnected());
drop(tx);
assert!(rx.is_disconnected());

[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]);

[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]);

Trait Implementations

impl<T> Clone for Receiver<T>
[src]

[src]

impl<'a, T> IntoIterator for &'a Receiver<T>
[src]

[src]

impl<T> IntoIterator for Receiver<T>
[src]

[src]

impl<T> Send for Receiver<T> where
    T: Send
[src]

impl<T> Drop for Receiver<T>
[src]

[src]

impl<T> Debug for Receiver<T>
[src]

[src]

impl<T> Sync for Receiver<T> where
    T: Send
[src]