Skip to main content

AsyncSink

Struct AsyncSink 

Source
pub struct AsyncSink<F: Flavor> { /* private fields */ }
Expand description

An async sink that allows you to write custom futures with poll_send(ctx).

Implementations§

Source§

impl<F: Flavor> AsyncSink<F>

Source

pub fn new(tx: AsyncTx<F>) -> Self

Source§

impl<F: Flavor> AsyncSink<F>
where F::Item: Unpin,

Source

pub fn poll_send( &mut self, ctx: &mut Context<'_>, item: F::Item, ) -> Result<(), TrySendError<F::Item>>

poll_send() will try to send a message. If the channel is full, it will register a notification for the next poll.

§Behavior

The polling behavior is different from SendFuture. Because the waker is not exposed to the user, you cannot perform delicate operations on the waker (compared to the Drop handler in SendFuture). To make sure no deadlock happens on cancellation, the WakerState will be Init after being registered (and will not be converted to Waiting). The receivers will wake up all Init state wakers until they find a normal pending sender in the Waiting state.

§Return value:

Returns Ok(()) on message sent.

Returns Err([crate::TrySendError::Full]) for a Poll::Pending case. The next time the channel is not full, your future will be woken again. You should then continue calling poll_send() to send the message. If you want to cancel, just don’t call poll_send() again. There are no side effects, and other senders will have a chance to send their messages.

Returns Err([crate::TrySendError::Disconnected]) when all Rx are dropped.

Methods from Deref<Target = AsyncTx<F>>§

Source

pub fn is_disconnected(&self) -> bool

Return true if the other side has closed

Source

pub fn send<'a>(&'a self, item: F::Item) -> SendFuture<'a, F>

Sends a message. This method will await until the message is sent or the channel is closed.

This function is cancellation-safe, so it’s safe to use with timeout() and the select! macro. When a SendFuture is dropped, no message will be sent. However, the original message cannot be returned due to API limitations. For timeout scenarios, we recommend using AsyncTx::send_timeout(), which returns the message in a SendTimeoutError.

Returns Ok(()) on success.

Returns Err(SendError) if the receiver has been dropped.

§Safety

Due to the nature of buffered channel, it’s possible that message being send concurrently while receiver dropping concurrently, still result in message send successfully without any one to receive them. You should rely on the Drop trait of the message to cleanup.

Source

pub fn try_send(&self, item: F::Item) -> Result<(), TrySendError<F::Item>>

Attempts to send a message without blocking.

Returns Ok(()) when successful.

Returns Err(TrySendError::Full) if the channel is full.

Returns Err(TrySendError::Disconnected) if the receiver has been dropped.

§Safety

Due to the nature of buffered channel, it’s possible that message being send concurrently while receiver dropping concurrently, still result in message send successfully without any one to receive them. You should rely on the Drop trait of the message to cleanup.

Source

pub fn send_timeout( &self, item: F::Item, duration: Duration, ) -> SendTimeoutFuture<'_, F, Sleep, ()>

Available on crate feature tokio only.

Sends a message with a timeout. Will await when channel is full.

The behavior is atomic: the message is either sent successfully or returned with error.

Returns Ok(()) when successful.

Returns Err(SendTimeoutError::Timeout) if the operation timed out. The error contains the message that failed to be sent.

Returns Err(SendTimeoutError::Disconnected) if the receiver has been dropped. The error contains the message that failed to be sent.

Source

pub fn send_timeout( &self, item: F::Item, duration: Duration, ) -> SendTimeoutFuture<'_, F, impl Future<Output = ()>, ()>

Available on crate feature async_std only.
Source

pub fn send_with_timer<FR, R>( &self, item: F::Item, fut: FR, ) -> SendTimeoutFuture<'_, F, FR, R>
where FR: Future<Output = R>,

Sends a message with a custom timer function (from other async runtime).

The behavior is atomic: the message is either sent successfully or returned with error.

Returns Ok(()) when successful.

Returns Err(SendTimeoutError::Timeout) if the operation timed out. The error contains the message that failed to be sent.

Returns Err(SendTimeoutError::Disconnected) if the receiver has been dropped. The error contains the message that failed to be sent.

§Argument:
  • fut: The sleep function. It’s possible to wrap this function with cancelable handle, you can control when to stop polling. the return value of fut is ignore. We add generic R just in order to support smol::Timer.
§Example:
extern crate smol;
use std::time::Duration;
use crossfire::*;
async fn foo() {
    let (tx, rx) = mpmc::bounded_async::<usize>(10);
    match tx.send_with_timer(1, smol::Timer::after(Duration::from_secs(1))).await {
        Ok(_)=>{
            println!("message sent");
        }
        Err(SendTimeoutError::Timeout(_item))=>{
            println!("send timeout");
        }
        Err(SendTimeoutError::Disconnected(_item))=>{
            println!("receiver-side closed");
        }
    }
}

Methods from Deref<Target = ChannelShared<F>>§

Source

pub fn len(&self) -> usize

The number of messages in the channel.

Source

pub fn capacity(&self) -> Option<usize>

The capacity of the channel. Returns None for unbounded channels.

Source

pub fn is_empty(&self) -> bool

Returns true if the channel is empty.

Source

pub fn is_full(&self) -> bool

Returns true if the channel is full.

Source

pub fn get_tx_count(&self) -> usize

Returns the number of senders for the channel.

Source

pub fn get_rx_count(&self) -> usize

Returns the number of receivers for the channel.

Source

pub fn get_wakers_count(&self) -> (usize, usize)

Returns the number of wakers for senders and receivers. For debugging purposes.

Trait Implementations§

Source§

impl<F: Flavor> Debug for AsyncSink<F>

Source§

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

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

impl<F: Flavor> Deref for AsyncSink<F>

Source§

type Target = AsyncTx<F>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<F: Flavor> Display for AsyncSink<F>

Source§

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

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

impl<F: Flavor> Drop for AsyncSink<F>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl<F: Flavor> From<AsyncTx<F>> for AsyncSink<F>

Source§

fn from(tx: AsyncTx<F>) -> Self

Converts to this type from the input type.
Source§

impl<F: Flavor + FlavorMP> From<MAsyncTx<F>> for AsyncSink<F>

Source§

fn from(tx: MAsyncTx<F>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<F> !RefUnwindSafe for AsyncSink<F>

§

impl<F> !Sync for AsyncSink<F>

§

impl<F> Freeze for AsyncSink<F>
where <<F as Flavor>::Send as Registry>::Waker: Freeze,

§

impl<F> Send for AsyncSink<F>

§

impl<F> Unpin for AsyncSink<F>

§

impl<F> UnsafeUnpin for AsyncSink<F>
where <<F as Flavor>::Send as Registry>::Waker: UnsafeUnpin,

§

impl<F> UnwindSafe for AsyncSink<F>
where <<F as Flavor>::Send as Registry>::Waker: UnwindSafe, F: RefUnwindSafe, <F as Flavor>::Send: RefUnwindSafe, <F as Flavor>::Recv: RefUnwindSafe,

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

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

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.