Skip to main content

Receiver

Struct Receiver 

Source
pub struct Receiver<T: Send + Sync + 'static> { /* private fields */ }
Expand description

Receives the values of the channel, in order.

Created by channel, Sender::subscribe or Receiver::resubscribe; each takes every value sent after it was made.

Implementations§

Source§

impl<T: Send + Sync + 'static> Receiver<T>

Source

pub fn len(&self) -> usize

How many values were sent that this receiver has yet to take; past the capacity, its next receive reports a lag.

Source

pub fn is_empty(&self) -> bool

Whether this receiver took every value sent.

Source

pub fn resubscribe(&self) -> Self

A new receiver, which takes the values sent from now on.

Source§

impl<T: Clone + Send + Sync + 'static> Receiver<T>

Source

pub fn try_recv(&mut self) -> Result<T, TryRecvError>

Takes the next value, if one came, without waiting.

§Errors

TryRecvError::Empty when none came since the last one taken; TryRecvError::Lagged once, when the receiver lost values, before it goes on from the oldest one still held; TryRecvError::Closed once every sender is gone and every value was taken.

use nuclei::sync::broadcast::{self, error::TryRecvError};

let (tx, mut rx) = broadcast::channel(2);
for i in 0..3 {
    tx.send(i).unwrap();
}
assert_eq!(rx.try_recv(), Err(TryRecvError::Lagged(1)));
assert_eq!(rx.try_recv(), Ok(1));
assert_eq!(rx.try_recv(), Ok(2));
assert_eq!(rx.try_recv(), Err(TryRecvError::Empty));
drop(tx);
assert_eq!(rx.try_recv(), Err(TryRecvError::Closed));
Source

pub async fn recv(&mut self) -> Result<T, RecvError>

Waits for the next value and takes it.

§Errors

RecvError::Lagged once, when the receiver lost values, before it goes on from the oldest one still held; RecvError::Closed once every sender is gone and every value was taken.

§Cancel safety

Dropping the future takes nothing: the next receive gets the value.

§Cooperative scheduling

On a nuclei shard a receive that completes spends one operation of its task’s budget; with the budget spent it takes nothing and yields to the shard’s other tasks first (see nuclei::task::coop).

Source

pub fn blocking_recv(&mut self) -> Result<T, RecvError>

Receives the next value from a thread that may block.

Like recv, but it parks the calling thread while no value came and the channel is open. On a nuclei shard (inside one of its tasks) it does not park the thread: it runs a nested block_on, so the shard keeps running its other tasks meanwhile. Inside another runtime’s task it blocks that task’s thread.

§Errors

As for recv.

§Panics

On wasm32, where no other thread can send, when no value came and the channel is open: the call could never end.

use nuclei::sync::broadcast::{self, RecvError};

let (tx, mut rx) = broadcast::channel::<u8>(4);
let receiver = std::thread::spawn(move || {
    assert_eq!(rx.blocking_recv(), Ok(10));
    assert_eq!(rx.blocking_recv(), Err(RecvError::Closed));
});
tx.send(10).unwrap();
drop(tx);
receiver.join().unwrap();

Trait Implementations§

Source§

impl<T: Send + Sync + 'static> Debug for Receiver<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Send + Sync + 'static> Drop for Receiver<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for Receiver<T>

§

impl<T> !UnwindSafe for Receiver<T>

§

impl<T> Freeze for Receiver<T>

§

impl<T> Send for Receiver<T>

§

impl<T> Sync for Receiver<T>

§

impl<T> Unpin for Receiver<T>

§

impl<T> UnsafeUnpin for Receiver<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> MaybeSend for T
where T: Send + ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.