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>
impl<T> AsyncTx<T>
Sourcepub fn try_send(&self, item: T) -> Result<(), TrySendError<T>>
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.
pub fn into_sink(self) -> AsyncSink<T>
pub fn into_blocking(self) -> Tx<T>
Source§impl<T: Unpin + Send + 'static> AsyncTx<T>
impl<T: Unpin + Send + 'static> AsyncTx<T>
Sourcepub fn send<'a>(&'a self, item: T) -> SendFuture<'a, T> ⓘ
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.
Sourcepub fn make_send_future<'a>(&'a self, item: T) -> SendFuture<'a, T> ⓘ
👎Deprecated
pub fn make_send_future<'a>(&'a self, item: T) -> SendFuture<'a, T> ⓘ
Use send() instead
Sourcepub fn send_timeout<'a>(
&'a self,
item: T,
duration: Duration,
) -> SendTimeoutFuture<'a, T> ⓘ
Available on crate features tokio
or async_std
only.
pub fn send_timeout<'a>( &'a self, item: T, duration: Duration, ) -> SendTimeoutFuture<'a, T> ⓘ
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.
Methods from Deref<Target = ChannelShared>§
Trait Implementations§
Source§fn as_ref(&self) -> &ChannelShared
fn as_ref(&self) -> &ChannelShared
Source§impl<T: Unpin + Send + 'static> AsyncTxTrait<T> for AsyncTx<T>
impl<T: Unpin + Send + 'static> AsyncTxTrait<T> for AsyncTx<T>
Source§fn try_send(&self, item: T) -> Result<(), TrySendError<T>>
fn try_send(&self, item: T) -> Result<(), TrySendError<T>>
Source§fn send<'a>(&'a self, item: T) -> SendFuture<'a, T> ⓘ
fn send<'a>(&'a self, item: T) -> SendFuture<'a, T> ⓘ
Source§fn send_timeout<'a>(
&'a self,
item: T,
duration: Duration,
) -> SendTimeoutFuture<'a, T> ⓘ
fn send_timeout<'a>( &'a self, item: T, duration: Duration, ) -> SendTimeoutFuture<'a, T> ⓘ
tokio
or async_std
only.