Struct jackiechan::Receiver[][src]

pub struct Receiver<T> { /* fields omitted */ }
Expand description

The receiving side of a channel.

Receivers can be cloned and shared among threads. When all receivers associated with a channel are dropped, the channel becomes closed.

The channel can also be closed manually by calling Receiver::close().

Receivers implement the [Stream] trait.

Implementations

impl<T> Receiver<T>[src]

pub fn try_recv(&self) -> Result<T, TryRecvError>[src]

Attempts to receive a message from the channel.

If the channel is empty or closed, this method returns an error.

Examples

use jackiechan::{bounded, TryRecvError};

let (s, r) = bounded(10);
assert_eq!(s.send(1), Ok(()));

assert_eq!(r.try_recv(), Ok(1));
assert_eq!(r.try_recv(), Err(TryRecvError::Empty));

drop(s);
assert_eq!(r.try_recv(), Err(TryRecvError::Closed));

pub fn recv(&self) -> Result<T, RecvError>[src]

Receives a message from the channel.

If the channel is empty, this method waits until there is a message.

If the channel is closed, this method receives a message or returns an error if there are no more messages.

Examples

use jackiechan::{bounded, RecvError};

let (s, r) = bounded(10);

assert_eq!(s.send(1), Ok(()));
drop(s);

assert_eq!(r.recv(), Ok(1));
assert_eq!(r.recv(), Err(RecvError));

pub fn recv_timeout(&self, timeout: Duration) -> Result<T, RecvTimeoutError>[src]

Receives a message from the channel.

If the channel is empty, this method waits until there is a message.

If the channel is closed, this method receives a message or returns an error if there are no more messages.

Examples

use jackiechan::{bounded, RecvTimeoutError};
use std::time::Duration;

let (s, r) = bounded(10);

assert_eq!(s.send(1), Ok(()));
assert_eq!(r.recv_timeout(Duration::from_secs(1)), Ok(1));
assert_eq!(r.recv_timeout(Duration::from_secs(1)), Err(RecvTimeoutError::Timeout));

pub fn recv_deadline(&self, deadline: Instant) -> Result<T, RecvTimeoutError>[src]

Receives a message from the channel.

If the channel is empty, this method waits until there is a message.

If the channel is closed, this method receives a message or returns an error if there are no more messages.

Examples

use jackiechan::{bounded, RecvTimeoutError};
use std::time::{Instant, Duration};

let (s, r) = bounded(10);

assert_eq!(s.send(1), Ok(()));

assert_eq!(r.recv_deadline(Instant::now() + Duration::from_secs(1)), Ok(1));
assert_eq!(r.recv_deadline(Instant::now() + Duration::from_secs(1)), Err(RecvTimeoutError::Timeout));

pub async fn async_recv(&self) -> Result<T, RecvError>[src]

Receives a message from the channel. If the channel is empty, this method waits until there is a message.

If the channel is closed, this method receives a message or returns an error if there are no more messages.

pub fn close(&self) -> bool[src]

Closes the channel.

Returns true if this call has closed the channel and it was not closed already.

The remaining messages can still be received.

Examples

use jackiechan::{bounded, RecvError};

let (s, r) = bounded(10);
assert_eq!(s.send(1), Ok(()));

assert!(r.close());
assert_eq!(r.recv(), Ok(1));
assert_eq!(r.recv(), Err(RecvError));

pub fn is_closed(&self) -> bool[src]

Returns true if the channel is closed.

Examples

use jackiechan::{bounded, RecvError};

let (s, r) = bounded::<()>(10);
assert!(!r.is_closed());

drop(s);
assert!(r.is_closed());

pub fn is_empty(&self) -> bool[src]

Returns true if the channel is empty.

Examples

use jackiechan::bounded;

let (s, r) = bounded(10);

assert!(s.is_empty());
s.send(1);
assert!(!s.is_empty());

pub fn is_full(&self) -> bool[src]

Returns true if the channel is full.

Examples

use jackiechan::bounded;

let (s, r) = bounded(1);

assert!(!r.is_full());
s.send(1);
assert!(r.is_full());

pub fn len(&self) -> usize[src]

Returns the number of messages in the channel.

Examples

use jackiechan::bounded;

let (s, r) = bounded(10);
assert_eq!(r.len(), 0);

s.send(1);
s.send(2);
assert_eq!(r.len(), 2);

pub fn capacity(&self) -> Option<usize>[src]

Returns the channel capacity if it’s bounded.

Examples

use jackiechan::{bounded};

let (s, r) = bounded::<i32>(5);
assert_eq!(r.capacity(), Some(5));

pub fn receiver_count(&self) -> usize[src]

Returns the number of receivers for the channel.

Examples

use jackiechan::bounded;

let (s, r) = bounded::<()>(10);
assert_eq!(r.receiver_count(), 1);

let r2 = r.clone();
assert_eq!(r.receiver_count(), 2);

pub fn sender_count(&self) -> usize[src]

Returns the number of senders for the channel.

Examples

use jackiechan::bounded;

let (s, r) = bounded::<()>(10);
assert_eq!(r.sender_count(), 1);

let s2 = s.clone();
assert_eq!(r.sender_count(), 2);

Trait Implementations

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

fn clone(&self) -> Receiver<T>[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> Drop for Receiver<T>[src]

fn drop(&mut self)[src]

Executes the destructor for this type. Read more

Auto Trait Implementations

impl<T> RefUnwindSafe for Receiver<T>

impl<T> Send for Receiver<T> where
    T: Send

impl<T> Sync for Receiver<T> where
    T: Send

impl<T> Unpin for Receiver<T>

impl<T> UnwindSafe for Receiver<T>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

pub fn to_owned(&self) -> T[src]

Creates owned data from borrowed data, usually by cloning. Read more

pub fn clone_into(&self, target: &mut T)[src]

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>[src]

Performs the conversion.