# tokio::sync::broadcast::Sender - Rust
API documentation for the Rust `Sender` struct in crate `tokio`.
Keywords: rust, rustlang, rust-lang, Sender

# Struct tokio::sync::broadcast::Sender [−] [src]

    pub struct Sender<T> { /* fields omitted */ }

Sending-half of the `broadcast` channel.

May be used from many threads. Messages can be sent with `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>` [src]

#### `pub fn send(&self, value: T) -> Result<usize, SendError<T>>` [src]

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 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();
    }

#### `pub fn subscribe(&self) -> Receiver<T>` [src]

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);
    }

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

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();
    }

## Trait Implementations

### `impl<T> Clone for Sender<T>` [src]

#### `fn clone(&self) -> Sender<T>` [src]

Returns a copy of the value. Read more

#### `pub fn clone_from(&mut self, source: &Self)`1.0.0 [src]

Performs copy-assignment from `source`. Read more

### `impl<T> Debug for Sender<T>` [src]

#### `fn fmt(&self, fmt: &mut Formatter<'_>) -> Result` [src]

Formats the value using the given formatter. Read more

### `impl<T> Drop for Sender<T>` [src]

#### `fn drop(&mut self)` [src]

Executes the destructor for this type. Read more

### `impl<T: Send> Send for Sender<T>` [src]

### `impl<T: Send> Sync for Sender<T>` [src]

## Auto Trait Implementations

### `impl<T> !RefUnwindSafe for Sender<T>` [src]

### `impl<T> Unpin for Sender<T>` [src]

### `impl<T> !UnwindSafe for Sender<T>` [src]

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

### Notable traits for &'_ mut F

`impl<'_, F> Future for &'_ mut F where
F: Unpin + Future + ?Sized, type Output = <F as Future>::Output;`
` [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ⓘ

### Notable traits for &'_ mut F

`impl<'_, F> Future for &'_ mut F where
F: Unpin + Future + ?Sized, type Output = <F as Future>::Output;`
` [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.