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>

source

pub fn len(&self) -> usize

Returns the number of queued elements.

source

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.

source

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

source

pub fn is_closed(&self) -> bool

Returns true if the channel is closed.

source

pub fn is_empty(&self) -> bool

Returns true if the channel is empty.

source

pub fn same_channel(&self, other: &Self) -> bool

Returns true if self and other are handles for the same channel instance.

source

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.

source

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.

source

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.

source

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

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

Trait Implementations§

source§

impl<T> Clone for SenderRef<'_, T>

source§

fn clone(&self) -> Self

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 SenderRef<'_, T>

source§

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

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

impl<T> Drop for SenderRef<'_, T>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<'a, T> !RefUnwindSafe for SenderRef<'a, T>

§

impl<'a, T> !Send for SenderRef<'a, T>

§

impl<'a, T> !Sync for SenderRef<'a, T>

§

impl<'a, T> Unpin for SenderRef<'a, T>

§

impl<'a, T> !UnwindSafe for SenderRef<'a, T>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · 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 Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.