[][src]Struct async_channel::Sender

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

The sending side of a channel.

Senders can be cloned and shared among threads. When all senders associated with a channel are dropped, the channel becomes closed.

The channel can also be closed manually by calling Sender::close().

Implementations

impl<T> Sender<T>[src]

pub fn try_send(&self, msg: T) -> Result<(), TrySendError<T>>[src]

Attempts to send a message into the channel.

If the channel is full or closed, this method returns an error.

Examples

use async_channel::{bounded, TrySendError};

let (s, r) = bounded(1);

assert_eq!(s.try_send(1), Ok(()));
assert_eq!(s.try_send(2), Err(TrySendError::Full(2)));

drop(r);
assert_eq!(s.try_send(3), Err(TrySendError::Closed(3)));

pub async fn send<'_>(&'_ self, msg: T) -> Result<(), SendError<T>>[src]

Sends a message into the channel.

If the channel is full, this method waits until there is space for a message.

If the channel is closed, this method returns an error.

Examples

use async_channel::{unbounded, SendError};

let (s, r) = unbounded();

assert_eq!(s.send(1).await, Ok(()));
drop(r);
assert_eq!(s.send(2).await, Err(SendError(2)));

pub fn close(&self) -> bool[src]

Closes the channel.

Returns true if this call has closed the channel and it was not closed already.

The remaining messages can still be received.

Examples

use async_channel::{unbounded, RecvError};

let (s, r) = unbounded();
assert_eq!(s.send(1).await, Ok(()));
assert!(s.close());

assert_eq!(r.recv().await, Ok(1));
assert_eq!(r.recv().await, Err(RecvError));

pub fn is_closed(&self) -> bool[src]

Returns true if the channel is closed.

Examples

use async_channel::{unbounded, RecvError};

let (s, r) = unbounded::<()>();
assert!(!s.is_closed());

drop(r);
assert!(s.is_closed());

pub fn is_empty(&self) -> bool[src]

Returns true if the channel is empty.

Examples

use async_channel::unbounded;

let (s, r) = unbounded();

assert!(s.is_empty());
s.send(1).await;
assert!(!s.is_empty());

pub fn is_full(&self) -> bool[src]

Returns true if the channel is full.

Unbounded channels are never full.

Examples

use async_channel::bounded;

let (s, r) = bounded(1);

assert!(!s.is_full());
s.send(1).await;
assert!(s.is_full());

pub fn len(&self) -> usize[src]

Returns the number of messages in the channel.

Examples

use async_channel::unbounded;

let (s, r) = unbounded();
assert_eq!(s.len(), 0);

s.send(1).await;
s.send(2).await;
assert_eq!(s.len(), 2);

pub fn capacity(&self) -> Option<usize>[src]

Returns the channel capacity if it's bounded.

Examples

use async_channel::{bounded, unbounded};

let (s, r) = bounded::<i32>(5);
assert_eq!(s.capacity(), Some(5));

let (s, r) = unbounded::<i32>();
assert_eq!(s.capacity(), None);

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

Returns the number of receivers for the channel.

Examples

use async_channel::unbounded;

let (s, r) = unbounded::<()>();
assert_eq!(s.receiver_count(), 1);

let r2 = r.clone();
assert_eq!(s.receiver_count(), 2);

pub fn sender_count(&self) -> usize[src]

Returns the number of receivers for the channel.

Examples

use async_channel::unbounded;

let (s, r) = unbounded::<()>();
assert_eq!(s.sender_count(), 1);

let s2 = s.clone();
assert_eq!(s.sender_count(), 2);

Trait Implementations

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

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

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

Auto Trait Implementations

impl<T> RefUnwindSafe for Sender<T>

impl<T> Send for Sender<T> where
    T: Send

impl<T> Sync for Sender<T> where
    T: Send

impl<T> Unpin for Sender<T>

impl<T> UnwindSafe for Sender<T>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

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.

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.