pub struct MAsyncTx<F: Flavor>(/* private fields */);Expand description
A multi-producer (sender) that works in an async context.
Inherits from AsyncTx<T> and implements Clone.
Additional methods in ChannelShared can be accessed through Deref.
You can use into() to convert it to AsyncTx<T>.
MAsyncTx can be converted into MTx via the From trait,
which means you can have two types of senders, both within async and
blocking contexts, for the same channel.
Implementations§
Source§impl<F: Flavor> MAsyncTx<F>
impl<F: Flavor> MAsyncTx<F>
pub fn into_sink(self) -> AsyncSink<F>
pub fn into_blocking(self) -> MTx<F>
Sourcepub fn downgrade(&self) -> WeakTx<F>where
F: FlavorMP,
pub fn downgrade(&self) -> WeakTx<F>where
F: FlavorMP,
Get a weak reference of sender.
§Example
use crossfire::*;
let (tx, rx) = mpsc::bounded_async::<usize>(100);
assert_eq!(tx.get_tx_count(), 1);
let weak_tx = tx.downgrade();
let tx_clone = weak_tx.upgrade::<MAsyncTx<_>>().unwrap();
assert_eq!(tx.get_tx_count(), 2);
drop(tx);
drop(tx_clone);
assert!(weak_tx.upgrade::<MAsyncTx<_>>().is_none());
assert_eq!(weak_tx.get_tx_count(), 0);
drop(rx);Methods from Deref<Target = AsyncTx<F>>§
Sourcepub fn is_disconnected(&self) -> bool
pub fn is_disconnected(&self) -> bool
Return true if the other side has closed
Sourcepub fn send<'a>(&'a self, item: F::Item) -> SendFuture<'a, F> ⓘ
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.
Sourcepub fn try_send(&self, item: F::Item) -> Result<(), TrySendError<F::Item>>
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.
Sourcepub fn send_timeout(
&self,
item: F::Item,
duration: Duration,
) -> SendTimeoutFuture<'_, F, Sleep, ()> ⓘ
Available on crate feature tokio only.
pub fn send_timeout( &self, item: F::Item, duration: Duration, ) -> SendTimeoutFuture<'_, F, Sleep, ()> ⓘ
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.
pub fn send_timeout( &self, item: F::Item, duration: Duration, ) -> SendTimeoutFuture<'_, F, impl Future<Output = ()>, ()> ⓘ
async_std only.Sourcepub fn send_with_timer<FR, R>(
&self,
item: F::Item,
fut: FR,
) -> SendTimeoutFuture<'_, F, FR, R> ⓘwhere
FR: Future<Output = R>,
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 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<F>>§
Trait Implementations§
Source§fn as_ref(&self) -> &ChannelShared<F>
fn as_ref(&self) -> &ChannelShared<F>
Source§impl<F: Flavor + FlavorMP> AsyncTxTrait<<F as Queue>::Item> for &MAsyncTx<F>
impl<F: Flavor + FlavorMP> AsyncTxTrait<<F as Queue>::Item> for &MAsyncTx<F>
Source§fn capacity(&self) -> Option<usize>
fn capacity(&self) -> Option<usize>
The capacity of the channel, return None for unbounded channel.
Source§fn is_disconnected(&self) -> bool
fn is_disconnected(&self) -> bool
Return true if the other side has closed
Source§fn try_send(&self, item: F::Item) -> Result<(), TrySendError<F::Item>>
fn try_send(&self, item: F::Item) -> Result<(), TrySendError<F::Item>>
Source§fn send(
&self,
item: F::Item,
) -> impl Future<Output = Result<(), SendError<F::Item>>> + Send
fn send( &self, item: F::Item, ) -> impl Future<Output = Result<(), SendError<F::Item>>> + Send
Source§fn send_timeout<'a>(
&'a self,
item: F::Item,
duration: Duration,
) -> impl Future<Output = Result<(), SendTimeoutError<F::Item>>> + Send
fn send_timeout<'a>( &'a self, item: F::Item, duration: Duration, ) -> impl Future<Output = Result<(), SendTimeoutError<F::Item>>> + Send
tokio or async_std only.Source§fn send_with_timer<FR, R>(
&self,
item: F::Item,
fut: FR,
) -> impl Future<Output = Result<(), SendTimeoutError<F::Item>>> + Send
fn send_with_timer<FR, R>( &self, item: F::Item, fut: FR, ) -> impl Future<Output = Result<(), SendTimeoutError<F::Item>>> + Send
Source§fn get_tx_count(&self) -> usize
fn get_tx_count(&self) -> usize
Source§fn get_rx_count(&self) -> usize
fn get_rx_count(&self) -> usize
fn get_wakers_count(&self) -> (usize, usize)
Source§impl<F: Flavor + FlavorMP> AsyncTxTrait<<F as Queue>::Item> for MAsyncTx<F>
impl<F: Flavor + FlavorMP> AsyncTxTrait<<F as Queue>::Item> for MAsyncTx<F>
Source§fn capacity(&self) -> Option<usize>
fn capacity(&self) -> Option<usize>
The capacity of the channel, return None for unbounded channel.
Source§fn is_disconnected(&self) -> bool
fn is_disconnected(&self) -> bool
Return true if the other side has closed
Source§fn try_send(&self, item: F::Item) -> Result<(), TrySendError<F::Item>>
fn try_send(&self, item: F::Item) -> Result<(), TrySendError<F::Item>>
Source§fn send(
&self,
item: F::Item,
) -> impl Future<Output = Result<(), SendError<F::Item>>> + Send
fn send( &self, item: F::Item, ) -> impl Future<Output = Result<(), SendError<F::Item>>> + Send
Source§fn send_timeout<'a>(
&'a self,
item: F::Item,
duration: Duration,
) -> impl Future<Output = Result<(), SendTimeoutError<F::Item>>> + Send
fn send_timeout<'a>( &'a self, item: F::Item, duration: Duration, ) -> impl Future<Output = Result<(), SendTimeoutError<F::Item>>> + Send
tokio or async_std only.