pub struct Sender<T> { /* private fields */ }
Expand description
An owned handle for sending elements through a bounded split Channel
.
Implementations§
Source§impl<T> Sender<T>
impl<T> Sender<T>
Sourcepub fn max_capacity(&self) -> usize
pub fn max_capacity(&self) -> usize
Returns the maximum buffer capacity of the channel.
This is the capacity initially specified when creating the channel and remains constant.
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the current capacity of the channel.
The capacity goes down when sending a value and goes up when receiving a value. When the capacity is zero, any subsequent sends will only resolve once sufficient capacity is available
Sourcepub fn same_channel(&self, other: &Self) -> bool
pub fn same_channel(&self, other: &Self) -> bool
Returns true
if self
and other
are handles for the same channel
instance.
Sourcepub fn try_send(&self, elem: T) -> Result<(), TrySendError<T>>
pub fn try_send(&self, elem: T) -> Result<(), TrySendError<T>>
Sends a value through the channel if there is sufficient capacity.
§Errors
Fails, if the queue is closed or there is no available capacity.
Sourcepub async fn send(&self, elem: T) -> Result<(), SendError<T>>
pub async fn send(&self, elem: T) -> Result<(), SendError<T>>
Sends a value through the channel, potentially waiting until there is sufficient capacity.
§Errors
Fails, if the queue is closed.
Sourcepub fn try_reserve(&self) -> Result<Permit<'_, T>, TrySendError<()>>
pub fn try_reserve(&self) -> Result<Permit<'_, T>, TrySendError<()>>
Attempts to reserve a slot in the channel without blocking, if none are available.
The returned Permit
can be used to immediately send a value to the
channel at a later point.
Dropping the permit without sending a value will return the capacity to
the channel.
§Errors
Fails, if there are no available permits or the channel has been closed.
§Examples
use async_unsync::bounded;
let (tx, mut rx) = bounded::channel(1).into_split();
// reserve capacity, reducing available slots to 0
let permit = tx.try_reserve().unwrap();
assert!(tx.try_send(1).is_err());
assert!(tx.try_reserve().is_err());
permit.send(1);
assert_eq!(rx.recv().await, Some(1));
Sourcepub fn try_reserve_owned(self) -> Result<OwnedPermit<T>, TrySendError<Self>>
pub fn try_reserve_owned(self) -> Result<OwnedPermit<T>, TrySendError<Self>>
Attempts to reserve a slot in the channel without blocking, if none are available.
This moves the sender by value and returns an owned permit that can be used to immediately send a value to the channel at a later point. Dropping the permit without sending a value will return the capacity to the channel.
§Errors
Fails, if there are no available permits or the channel has been closed.
§Examples
use async_unsync::bounded;
let (tx, mut rx) = bounded::channel(2).into_split();
// cloning senders is cheap, so arbitrary numbers of owned permits are
// easily created
let p1 = tx.clone().try_reserve_owned().unwrap();
let p2 = tx.clone().try_reserve_owned().unwrap();
assert!(tx.try_send(1).is_err());
assert!(tx.try_reserve().is_err());
drop(tx);
let _ = p2.send(1);
let _ = p1.send(2);
assert_eq!(rx.recv().await, Some(1));
assert_eq!(rx.recv().await, Some(2));
assert_eq!(rx.recv().await, None);
Sourcepub async fn reserve(&self) -> Result<Permit<'_, T>, SendError<()>>
pub async fn reserve(&self) -> Result<Permit<'_, T>, SendError<()>>
Attempts to reserve a slot in the channel without blocking.
If no capacity is available in the channel, this will block until a slot
becomes available.
The returned Permit
can be used to immediately send a value to the
channel at a later point.
Dropping the permit without sending a value will return the capacity to
the channel.
§Errors
Fails, if there are no available permits or the channel has been closed.
§Examples
use async_unsync::bounded;
let (tx, mut rx) = bounded::channel(1).into_split();
// reserve capacity, reducing available slots to 0
let permit = tx.reserve().await.unwrap();
assert!(tx.try_send(1).is_err());
assert!(tx.try_reserve().is_err());
permit.send(1);
assert_eq!(rx.recv().await, Some(1));
Sourcepub async fn reserve_owned(self) -> Result<OwnedPermit<T>, SendError<Self>>
pub async fn reserve_owned(self) -> Result<OwnedPermit<T>, SendError<Self>>
Attempts to reserve a slot in the channel without blocking.
If no capacity is available in the channel, this will block until a slot becomes available. This moves the sender by value and returns an owned permit that can be used to immediately send a value to the channel at a later point. Dropping the permit without sending a value will return the capacity to the channel.
§Errors
Fails, if there are no available permits or the channel has been closed.
§Examples
use async_unsync::bounded;
let (tx, mut rx) = bounded::channel(1).into_split();
// reserve capacity, reducing available slots to 0
let permit = tx.clone().reserve_owned().await.unwrap();
assert!(tx.try_send(1).is_err());
assert!(tx.try_reserve().is_err());
permit.send(1);
assert_eq!(rx.recv().await, Some(1));