pub struct AsyncTx<T> { /* private fields */ }Expand description
A single producer (sender) that works in an async context.
Additional methods in ChannelShared can be accessed through Deref.
AsyncTx can be converted into Tx via the From trait.
This means you can have two types of senders, both within async and blocking contexts, for the same channel.
NOTE: AsyncTx is not Clone or Sync.
If you need concurrent access, use MAsyncTx instead.
AsyncTx has a Send marker and can be moved to other coroutines.
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 a Sync marker, using Arc<AsyncTx> will lose the 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: 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> ⓘ
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.
Sourcepub fn try_send(&self, item: T) -> Result<(), TrySendError<T>>
pub fn try_send(&self, item: T) -> Result<(), TrySendError<T>>
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.
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.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.
Sourcepub fn send_with_timer<'a, F, R>(
&'a self,
item: T,
fut: F,
) -> SendTimeoutFuture<'a, T, R> ⓘwhere
F: Future<Output = R> + 'static,
pub fn send_with_timer<'a, F, R>(
&'a self,
item: T,
fut: F,
) -> SendTimeoutFuture<'a, T, R> ⓘwhere
F: Future<Output = R> + 'static,
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 offutis ignore. We add genericRjust 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<T>>§
Trait Implementations§
Source§fn as_ref(&self) -> &ChannelShared<T>
fn as_ref(&self) -> &ChannelShared<T>
Source§impl<T: Unpin + Send + 'static> AsyncTxTrait<T> for AsyncTx<T>
impl<T: Unpin + Send + 'static> AsyncTxTrait<T> for AsyncTx<T>
fn clone_to_vec(self, count: usize) -> Vec<Self>
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.