pub struct Sender<T: Send + Sync + 'static> { /* private fields */ }Expand description
Implementations§
Source§impl<T: Send + Sync + 'static> Sender<T>
impl<T: Send + Sync + 'static> Sender<T>
Sourcepub fn new(capacity: usize) -> Self
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));Sourcepub fn send(&self, value: T) -> Result<usize, SendError<T>>
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");Sourcepub fn subscribe(&self) -> Receiver<T>
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));Sourcepub fn receiver_count(&self) -> usize
pub fn receiver_count(&self) -> usize
How many receivers there are.
Sourcepub fn downgrade(&self) -> WeakSender<T>
pub fn downgrade(&self) -> WeakSender<T>
A WeakSender of the channel, which does not keep it open. The
sender itself stays.
Sourcepub fn strong_count(&self) -> usize
pub fn strong_count(&self) -> usize
How many Senders exist.
Sourcepub fn weak_count(&self) -> usize
pub fn weak_count(&self) -> usize
How many WeakSenders exist.
Sourcepub fn len(&self) -> usize
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);Sourcepub fn same_channel(&self, other: &Self) -> bool
pub fn same_channel(&self, other: &Self) -> bool
Whether other sends on the same channel.
Sourcepub async fn closed(&self)
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(()));