async_channel

Struct Sender

Source
pub struct Sender<T> { /* private fields */ }
Expand description

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§

Source§

impl<T> Sender<T>

Source

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

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

pub fn send(&self, msg: T) -> Send<'_, T>

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

pub fn send_blocking(&self, msg: T) -> Result<(), SendError<T>>

Sends a message into this channel using the blocking strategy.

If the channel is full, this method will block until there is room. If the channel is closed, this method returns an error.

§Blocking

Rather than using asynchronous waiting, like the send method, this method will block the current thread until the message is sent.

This method should not be used in an asynchronous context. It is intended to be used such that a channel can be used in both asynchronous and synchronous contexts. Calling this method in an asynchronous context may result in deadlocks.

§Examples
use async_channel::{unbounded, SendError};

let (s, r) = unbounded();

assert_eq!(s.send_blocking(1), Ok(()));
drop(r);
assert_eq!(s.send_blocking(2), Err(SendError(2)));
Source

pub fn close(&self) -> bool

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

pub fn is_closed(&self) -> bool

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

pub fn is_empty(&self) -> bool

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

pub fn is_full(&self) -> bool

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

pub fn len(&self) -> usize

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

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

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

pub fn receiver_count(&self) -> usize

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

pub fn sender_count(&self) -> usize

Returns the number of senders 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);
Source

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

Downgrade the sender to a weak reference.

Trait Implementations§

Source§

impl<T> Clone for Sender<T>

Source§

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

Returns a copy of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for Sender<T>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<T> Drop for Sender<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Sender<T>

§

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§

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, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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.

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.