Struct async_broadcast::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 (active) receivers associated with a channel are dropped, the channel becomes closed. You can deactivate a receiver using Receiver::deactivate if you would like the channel to remain open without keeping active receivers around.

Implementations

impl<T> Receiver<T>[src]

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

Returns the channel capacity.

Examples

use async_broadcast::broadcast;

let (_s, r) = broadcast::<i32>(5);
assert_eq!(r.capacity(), 5);

pub fn set_capacity(&mut self, new_cap: usize)[src]

Set the channel capacity.

There are times when you need to change the channel’s capacity after creating it. If the new_cap is less than the number of messages in the channel, the oldest messages will be dropped to shrink the channel.

Examples

use async_broadcast::{broadcast, TrySendError, TryRecvError};

let (s, mut r) = broadcast::<i32>(3);
assert_eq!(r.capacity(), 3);
s.try_broadcast(1).unwrap();
s.try_broadcast(2).unwrap();
s.try_broadcast(3).unwrap();

r.set_capacity(1);
assert_eq!(r.capacity(), 1);
assert_eq!(r.try_recv().unwrap(), 3);
assert_eq!(r.try_recv(), Err(TryRecvError::Empty));
s.try_broadcast(1).unwrap();
assert_eq!(s.try_broadcast(2), Err(TrySendError::Full(2)));

r.set_capacity(2);
assert_eq!(r.capacity(), 2);
s.try_broadcast(2).unwrap();
assert_eq!(s.try_broadcast(2), Err(TrySendError::Full(2)));

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

If overflow mode is enabled on this channel.

Examples

use async_broadcast::broadcast;

let (_s, r) = broadcast::<i32>(5);
assert!(!r.overflow());

pub fn set_overflow(&mut self, overflow: bool)[src]

Set overflow mode on the channel.

When overflow mode is set, broadcasting to the channel will succeed even if the channel is full. It achieves that by removing the oldest message from the channel.

Examples

use async_broadcast::{broadcast, TrySendError, TryRecvError};

let (s, mut r) = broadcast::<i32>(2);
s.try_broadcast(1).unwrap();
s.try_broadcast(2).unwrap();
assert_eq!(s.try_broadcast(3), Err(TrySendError::Full(3)));
r.set_overflow(true);
assert_eq!(s.try_broadcast(3).unwrap(), Some(1));
assert_eq!(s.try_broadcast(4).unwrap(), Some(2));

assert_eq!(r.try_recv().unwrap(), 3);
assert_eq!(r.try_recv().unwrap(), 4);
assert_eq!(r.try_recv(), Err(TryRecvError::Empty));

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 async_broadcast::{broadcast, RecvError};

let (s, mut r) = broadcast(1);
s.broadcast(1).await.unwrap();
assert!(s.close());

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

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

Returns true if the channel is closed.

Examples

use async_broadcast::{broadcast, RecvError};

let (s, r) = broadcast::<()>(1);
assert!(!s.is_closed());

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

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

Returns true if the channel is empty.

Examples

use async_broadcast::broadcast;

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

assert!(s.is_empty());
s.broadcast(1).await;
assert!(!s.is_empty());

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

Returns true if the channel is full.

Examples

use async_broadcast::broadcast;

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

assert!(!s.is_full());
s.broadcast(1).await;
assert!(s.is_full());

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

Returns the number of messages in the channel.

Examples

use async_broadcast::broadcast;

let (s, r) = broadcast(2);
assert_eq!(s.len(), 0);

s.broadcast(1).await;
s.broadcast(2).await;
assert_eq!(s.len(), 2);

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

Returns the number of receivers for the channel.

Examples

use async_broadcast::broadcast;

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

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

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

Returns the number of senders for the channel.

Examples

use async_broadcast::broadcast;

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

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

pub fn deactivate(self) -> InactiveReceiver<T>[src]

Downgrade to a InactiveReceiver.

An inactive receiver is one that can not and does not receive any messages. Its only purpose is keep the associated channel open even when there are no (active) receivers. An inactive receiver can be upgraded into a Receiver using InactiveReceiver::activate or InactiveReceiver::activate_cloned.

Sender::try_broadcast will return TrySendError::Inactive if only inactive receivers exists for the associated channel and Sender::broadcast will wait until an active receiver is available.

Examples

use async_broadcast::{broadcast, TrySendError};

let (s, r) = broadcast(1);
let inactive = r.deactivate();
assert_eq!(s.try_broadcast(10), Err(TrySendError::Inactive(10)));

let mut r = inactive.activate();
assert_eq!(s.broadcast(10).await, Ok(None));
assert_eq!(r.recv().await, Ok(10));

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

pub fn recv(&mut self) -> Recv<'_, T>

Notable traits for Recv<'a, T>

impl<'a, T: Clone> Future for Recv<'a, T> type Output = 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 async_broadcast::{broadcast, RecvError};

let (s, mut r1) = broadcast(1);
let mut r2 = r1.clone();

assert_eq!(s.broadcast(1).await, Ok(None));
drop(s);

assert_eq!(r1.recv().await, Ok(1));
assert_eq!(r1.recv().await, Err(RecvError));
assert_eq!(r2.recv().await, Ok(1));
assert_eq!(r2.recv().await, Err(RecvError));

pub fn try_recv(&mut 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 async_broadcast::{broadcast, TryRecvError};

let (s, mut r1) = broadcast(1);
let mut r2 = r1.clone();
assert_eq!(s.broadcast(1).await, Ok(None));

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

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

Trait Implementations

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> 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

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

fn is_terminated(&self) -> bool[src]

Returns true if the stream should no longer be polled.

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

type Item = T

Values yielded by the stream.

fn poll_next(
    self: Pin<&mut Self>,
    cx: &mut Context<'_>
) -> Poll<Option<Self::Item>>
[src]

Attempt to pull out the next value of this stream, registering the current task for wakeup if the value is not yet available, and returning None if the stream is exhausted. Read more

fn size_hint(&self) -> (usize, Option<usize>)[src]

Returns the bounds on the remaining length of the stream. 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.

impl<S, T, E> TryStream for S where
    S: Stream<Item = Result<T, E>> + ?Sized
[src]

type Ok = T

The type of successful values yielded by this future

type Error = E

The type of failures yielded by this future

pub fn try_poll_next(
    self: Pin<&mut S>,
    cx: &mut Context<'_>
) -> Poll<Option<Result<<S as TryStream>::Ok, <S as TryStream>::Error>>>
[src]

Poll this TryStream as if it were a Stream. Read more