Sender in async_std::channel - Rust (original) (raw)
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().
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)));
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)));
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)));
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));
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());
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());
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());
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);
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);
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);
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);
Downgrade the sender to a weak reference.