Struct async_unsync::bounded::SenderRef
source · pub struct SenderRef<'a, T> { /* private fields */ }
Expand description
A borrowing handle for sending elements through a bounded split Channel
.
Implementations§
source§impl<T> SenderRef<'_, T>
impl<T> SenderRef<'_, 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 fn unbounded_send(&self, elem: T) -> Result<(), SendError<T>>
pub fn unbounded_send(&self, elem: T) -> Result<(), SendError<T>>
Sends a value through the channel, ignoring any capacity constraints.
This will immediately enqueue elem
, even if there are currently
senders waiting due to a lack of available capacity.
Care must be taken with unbounded sends, as they may undermine
assumptions about message ordering and the ability to apply
backpressure.
Alternatively, this can be thought of as a one-time capacity increase.
Errors
Fails, if the queue is closed.
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 blocking 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 mut chan = bounded::channel(1);
let (tx, mut rx) = chan.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 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 mut chan = bounded::channel(1);
let (tx, mut rx) = chan.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));