Struct Sender

struct Sender<T> { ... }

Sending-half of the broadcast channel.

May be used from many threads. Messages can be sent with [send][Sender::send].

Examples

use tokio::sync::broadcast;

#[tokio::main]
async fn main() {
    let (tx, mut rx1) = broadcast::channel(16);
    let mut rx2 = tx.subscribe();

    tokio::spawn(async move {
        assert_eq!(rx1.recv().await.unwrap(), 10);
        assert_eq!(rx1.recv().await.unwrap(), 20);
    });

    tokio::spawn(async move {
        assert_eq!(rx2.recv().await.unwrap(), 10);
        assert_eq!(rx2.recv().await.unwrap(), 20);
    });

    tx.send(10).unwrap();
    tx.send(20).unwrap();
}

Implementations

impl<T> Sender<T>

fn new(capacity: usize) -> Self

Creates the sending-half of the broadcast channel.

See the documentation of broadcast::channel for more information on this method.

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

Attempts to send a value to all active Receiver handles, returning it back if it could not be sent.

A successful send occurs when there is at least one active Receiver handle. An unsuccessful send would be one where all associated Receiver handles have already been dropped.

Return

On success, the number of subscribed Receiver handles is returned. This does not mean that this number of receivers will see the message as a receiver may drop or lag (see lagging) before receiving the message.

Note

A return value of Ok does not mean that the sent value will be observed by all or any of the active Receiver handles. Receiver handles may be dropped before receiving the sent message.

A return value of Err does not mean that future calls to send will fail. New Receiver handles may be created by calling subscribe.

Examples

use tokio::sync::broadcast;

#[tokio::main]
async fn main() {
    let (tx, mut rx1) = broadcast::channel(16);
    let mut rx2 = tx.subscribe();

    tokio::spawn(async move {
        assert_eq!(rx1.recv().await.unwrap(), 10);
        assert_eq!(rx1.recv().await.unwrap(), 20);
    });

    tokio::spawn(async move {
        assert_eq!(rx2.recv().await.unwrap(), 10);
        assert_eq!(rx2.recv().await.unwrap(), 20);
    });

    tx.send(10).unwrap();
    tx.send(20).unwrap();
}
fn subscribe(self: &Self) -> Receiver<T>

Creates a new Receiver handle that will receive values sent after this call to subscribe.

Examples

use tokio::sync::broadcast;

#[tokio::main]
async fn main() {
    let (tx, _rx) = broadcast::channel(16);

    // Will not be seen
    tx.send(10).unwrap();

    let mut rx = tx.subscribe();

    tx.send(20).unwrap();

    let value = rx.recv().await.unwrap();
    assert_eq!(20, value);
}
fn downgrade(self: &Self) -> WeakSender<T>

Converts the Sender to a WeakSender that does not count towards RAII semantics, i.e. if all Sender instances of the channel were dropped and only WeakSender instances remain, the channel is closed.

fn len(self: &Self) -> usize

Returns the number of queued values.

A value is queued until it has either been seen by all receivers that were alive at the time it was sent, or has been evicted from the queue by subsequent sends that exceeded the queue's capacity.

Note

In contrast to Receiver::len, this method only reports queued values and not values that have been evicted from the queue before being seen by all receivers.

Examples

use tokio::sync::broadcast;

#[tokio::main]
async fn main() {
    let (tx, mut rx1) = broadcast::channel(16);
    let mut rx2 = tx.subscribe();

    tx.send(10).unwrap();
    tx.send(20).unwrap();
    tx.send(30).unwrap();

    assert_eq!(tx.len(), 3);

    rx1.recv().await.unwrap();

    // The len is still 3 since rx2 hasn't seen the first value yet.
    assert_eq!(tx.len(), 3);

    rx2.recv().await.unwrap();

    assert_eq!(tx.len(), 2);
}
fn is_empty(self: &Self) -> bool

Returns true if there are no queued values.

Examples

use tokio::sync::broadcast;

#[tokio::main]
async fn main() {
    let (tx, mut rx1) = broadcast::channel(16);
    let mut rx2 = tx.subscribe();

    assert!(tx.is_empty());

    tx.send(10).unwrap();

    assert!(!tx.is_empty());

    rx1.recv().await.unwrap();

    // The queue is still not empty since rx2 hasn't seen the value.
    assert!(!tx.is_empty());

    rx2.recv().await.unwrap();

    assert!(tx.is_empty());
}
fn receiver_count(self: &Self) -> usize

Returns the number of active receivers.

An active receiver is a Receiver handle returned from channel or subscribe. These are the handles that will receive values sent on this Sender.

Note

It is not guaranteed that a sent message will reach this number of receivers. Active receivers may never call recv again before dropping.

Examples

use tokio::sync::broadcast;

#[tokio::main]
async fn main() {
    let (tx, _rx1) = broadcast::channel(16);

    assert_eq!(1, tx.receiver_count());

    let mut _rx2 = tx.subscribe();

    assert_eq!(2, tx.receiver_count());

    tx.send(10).unwrap();
}
fn same_channel(self: &Self, other: &Self) -> bool

Returns true if senders belong to the same channel.

Examples

use tokio::sync::broadcast;

#[tokio::main]
async fn main() {
    let (tx, _rx) = broadcast::channel::<()>(16);
    let tx2 = tx.clone();

    assert!(tx.same_channel(&tx2));

    let (tx3, _rx3) = broadcast::channel::<()>(16);

    assert!(!tx3.same_channel(&tx2));
}
async fn closed(self: &Self)

A future which completes when the number of [Receiver]s subscribed to this Sender reaches zero.

Examples

use futures::FutureExt;
use tokio::sync::broadcast;

#[tokio::main]
async fn main() {
    let (tx, mut rx1) = broadcast::channel::<u32>(16);
    let mut rx2 = tx.subscribe();

    let _ = tx.send(10);

    assert_eq!(rx1.recv().await.unwrap(), 10);
    drop(rx1);
    assert!(tx.closed().now_or_never().is_none());

    assert_eq!(rx2.recv().await.unwrap(), 10);
    drop(rx2);
    assert!(tx.closed().now_or_never().is_some());
}
fn strong_count(self: &Self) -> usize

Returns the number of Sender handles.

fn weak_count(self: &Self) -> usize

Returns the number of WeakSender handles.

impl<T> Any for Sender<T>

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for Sender<T>

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for Sender<T>

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

impl<T> Clone for Sender<T>

fn clone(self: &Self) -> Sender<T>

impl<T> CloneToUninit for Sender<T>

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

impl<T> Debug for Sender<T>

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

impl<T> Drop for Sender<T>

fn drop(self: &mut Self)

impl<T> Freeze for Sender<T>

impl<T> From for Sender<T>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> RefUnwindSafe for Sender<T>

impl<T> Send for Sender<T>

impl<T> Sync for Sender<T>

impl<T> ToOwned for Sender<T>

fn to_owned(self: &Self) -> T
fn clone_into(self: &Self, target: &mut T)

impl<T> Unpin for Sender<T>

impl<T> UnwindSafe for Sender<T>

impl<T, U> Into for Sender<T>

fn into(self: Self) -> U

Calls U::from(self).

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

impl<T, U> TryFrom for Sender<T>

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

impl<T, U> TryInto for Sender<T>

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