Struct AsyncTx

Source
pub struct AsyncTx<T> { /* private fields */ }
Expand description

Single producer (sender) that works in async context.

Additional methods can be accessed through Deref<Target=ChannelShared>.

AsyncTx can be converted into Tx via From trait, that means you can have two types of senders both within async context and blocking context to the same channel.

NOTE: AsyncTx is not Clone, nor Sync. If you need concurrent access, use MAsyncTx instead.

AsyncTx has Send marker, can be moved to other coroutine. The following code is OK :

use crossfire::*;
async fn foo() {
    let (tx, rx) = spsc::bounded_async::<usize>(100);
    tokio::spawn(async move {
         let _ = tx.send(2).await;
    });
    drop(rx);
}

Because AsyncTx does not have Sync marker, using Arc<AsyncTx> will lose Send marker.

For your safety, the following code should not compile:

use crossfire::*;
use std::sync::Arc;
async fn foo() {
    let (tx, rx) = spsc::bounded_async::<usize>(100);
    let tx = Arc::new(tx);
    tokio::spawn(async move {
         let _ = tx.send(2).await;
    });
    drop(rx);
}

Implementations§

Source§

impl<T> AsyncTx<T>

Source

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

Try to send message, non-blocking

Returns Ok(()) when successful.

Returns Err(TrySendError::Full) on channel full for bounded channel.

Returns Err(TrySendError::Disconnected) when all Rx dropped.

Source

pub fn len(&self) -> usize

The number of messages in the channel at the moment

Source

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

The capacity of the channel

Source

pub fn is_empty(&self) -> bool

Whether channel is empty at the moment

Source

pub fn is_full(&self) -> bool

Whether the channel is full at the moment

Source

pub fn into_sink(self) -> AsyncSink<T>

Source

pub fn into_blocking(self) -> Tx<T>

Source§

impl<T: Unpin + Send + 'static> AsyncTx<T>

Source

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

Send message. Will await when channel is full.

Returns Ok(()) on successful.

Returns Err(SendError) when all Rx is dropped.

Source

pub fn make_send_future<'a>(&'a self, item: T) -> SendFuture<'a, T>

👎Deprecated

Use send() instead

Source

pub fn send_timeout<'a>( &'a self, item: T, duration: Duration, ) -> SendTimeoutFuture<'a, T>

Available on crate features tokio or async_std only.

Waits for a message to be sent into the channel, but only for a limited time. Will await when channel is full.

The behavior is atomic, either message sent successfully or returned on error.

Returns Ok(()) when successful.

Returns Err(SendTimeoutError::Timeout) when the operation timed out.

Returns Err(SendTimeoutError::Disconnected) when all Rx dropped.

Source

pub fn send_blocking(&self, item: T) -> Result<(), SendError<T>>

Send a message while blocking the current thread. Be careful!

Returns Ok(())on successful.

Returns Err(SendError) when all Rx is dropped.

NOTE: Do not use it in async context otherwise will block the runtime.

Methods from Deref<Target = ChannelShared>§

Source

pub fn is_disconnected(&self) -> bool

Return true if all the senders or receivers are dropped

Source

pub fn get_tx_count(&self) -> usize

Get the count of alive senders

Source

pub fn get_rx_count(&self) -> usize

Get the count of alive receivers

Source

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

Just for debugging purpose, to monitor queue size

Trait Implementations§

Source§

impl<T> AsRef<ChannelShared> for AsyncTx<T>

Source§

fn as_ref(&self) -> &ChannelShared

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<T: Unpin + Send + 'static> AsyncTxTrait<T> for AsyncTx<T>

Source§

fn try_send(&self, item: T) -> Result<(), TrySendError<T>>

Try to send message, non-blocking Read more
Source§

fn len(&self) -> usize

The number of messages in the channel at the moment
Source§

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

The capacity of the channel, None for unbounded.
Source§

fn is_empty(&self) -> bool

Whether channel is empty at the moment
Source§

fn is_full(&self) -> bool

Whether the channel is full at the moment
Source§

fn send<'a>(&'a self, item: T) -> SendFuture<'a, T>

Send message. Will await when channel is full. Read more
Source§

fn send_timeout<'a>( &'a self, item: T, duration: Duration, ) -> SendTimeoutFuture<'a, T>

Available on crate features tokio or async_std only.
Waits for a message to be sent into the channel, but only for a limited time. Will await when channel is full. Read more
Source§

fn is_disconnected(&self) -> bool

Return true if the other side has closed
Source§

impl<T> Debug for AsyncTx<T>

Source§

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

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

impl<T> Deref for AsyncTx<T>

Source§

type Target = ChannelShared

The resulting type after dereferencing.
Source§

fn deref(&self) -> &ChannelShared

Dereferences the value.
Source§

impl<T> Display for AsyncTx<T>

Source§

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

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

impl<T> Drop for AsyncTx<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T: Unpin + Send + 'static> From<AsyncTx<T>> for AsyncSink<T>

Source§

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

Converts to this type from the input type.
Source§

impl<T> From<AsyncTx<T>> for Tx<T>

Source§

fn from(value: AsyncTx<T>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<MAsyncTx<T>> for AsyncTx<T>

Source§

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

Converts to this type from the input type.
Source§

impl<T: Send> Send for AsyncTx<T>

Auto Trait Implementations§

§

impl<T> Freeze for AsyncTx<T>

§

impl<T> !RefUnwindSafe for AsyncTx<T>

§

impl<T> !Sync for AsyncTx<T>

§

impl<T> Unpin for AsyncTx<T>

§

impl<T> !UnwindSafe for AsyncTx<T>

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<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.