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>

source

pub fn len(&self) -> usize

Returns the number of currently 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 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.

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

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

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

Trait Implementations§

source§

impl<T> Clone for Sender<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 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> !RefUnwindSafe for Sender<T>

§

impl<T> !Send for Sender<T>

§

impl<T> !Sync for Sender<T>

§

impl<T> Unpin for Sender<T>

§

impl<T> !UnwindSafe for Sender<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.