Skip to main content

Sender

Struct Sender 

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

Sends values to every Receiver of the channel.

Created by channel. Clone it for more producers. The channel closes for the receivers when the last sender is dropped.

Implementations§

Source§

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

Source

pub fn new(capacity: usize) -> Self

A sender of a new channel holding the last capacity values (rounded up to a power of two), with no receiver yet: each comes from subscribe.

§Panics

When capacity is zero, or above usize::MAX / 2.

use nuclei::sync::broadcast::Sender;

let tx = Sender::new(4);
assert!(tx.send(1).is_err(), "no receiver yet");
let mut rx = tx.subscribe();
tx.send(2).unwrap();
assert_eq!(rx.try_recv(), Ok(2));
Source

pub fn send(&self, value: T) -> Result<usize, SendError<T>>

Sends value to every receiver there is now, returning how many.

A send never waits: the channel holds the last capacity values, and a receiver too far behind loses the oldest.

§Errors

When there is no receiver; the value is handed back.

use nuclei::sync::broadcast;

let (tx, rx) = broadcast::channel(2);
assert_eq!(tx.send("a"), Ok(1));
drop(rx);
assert_eq!(tx.send("b").unwrap_err().0, "b");
Source

pub fn subscribe(&self) -> Receiver<T>

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

use nuclei::sync::broadcast;

let (tx, _rx) = broadcast::channel(2);
tx.send(1).unwrap();
let mut late = tx.subscribe();
tx.send(2).unwrap();
assert_eq!(late.try_recv(), Ok(2));
Source

pub fn receiver_count(&self) -> usize

How many receivers there are.

Source

pub fn downgrade(&self) -> WeakSender<T>

A WeakSender of the channel, which does not keep it open. The sender itself stays.

Source

pub fn strong_count(&self) -> usize

How many Senders exist.

Source

pub fn weak_count(&self) -> usize

How many WeakSenders exist.

Source

pub fn len(&self) -> usize

How many values the slowest receiver has yet to take, at most the capacity (older ones are lost to it); zero with no receiver.

use nuclei::sync::broadcast;

let (tx, mut rx) = broadcast::channel(4);
tx.send(1).unwrap();
tx.send(2).unwrap();
assert_eq!(tx.len(), 2);
rx.try_recv().unwrap();
assert_eq!(tx.len(), 1);
Source

pub fn is_empty(&self) -> bool

Whether every receiver took every value it can still take.

Source

pub fn same_channel(&self, other: &Self) -> bool

Whether other sends on the same channel.

Source

pub async fn closed(&self)

Completes once there is no receiver.

use core::future::Future;
use core::pin::pin;
use core::task::{Context, Poll, Waker};
use nuclei::sync::broadcast;

let (tx, rx) = broadcast::channel::<u8>(1);
let mut cx = Context::from_waker(Waker::noop());
let mut closed = pin!(tx.closed());
assert_eq!(closed.as_mut().poll(&mut cx), Poll::Pending);
drop(rx);
assert_eq!(closed.as_mut().poll(&mut cx), Poll::Ready(()));

Trait Implementations§

Source§

impl<T: Send + Sync + 'static> Clone for Sender<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Send + Sync + 'static> Debug for Sender<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 Sender<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 Sender<T>

§

impl<T> !UnwindSafe for Sender<T>

§

impl<T> Freeze for Sender<T>

§

impl<T> Send for Sender<T>

§

impl<T> Sync for Sender<T>

§

impl<T> Unpin for Sender<T>

§

impl<T> UnsafeUnpin for Sender<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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.