pub struct AsyncRx<T> { /* private fields */ }Expand description
A single consumer (receiver) that works in an async context.
Additional methods in ChannelShared can be accessed through Deref.
AsyncRx can be converted into Rx via the From trait,
which means you can have two types of receivers, both within async and
blocking contexts, for the same channel.
NOTE: AsyncRx is not Clone or Sync.
If you need concurrent access, use MAsyncRx instead.
AsyncRx has a Send marker and can be moved to other coroutines.
The following code is OK:
use crossfire::*;
async fn foo() {
let (tx, rx) = mpsc::bounded_async::<usize>(100);
tokio::spawn(async move {
let _ = rx.recv().await;
});
drop(tx);
}Because AsyncRx does not have a Sync marker, using Arc<AsyncRx> 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) = mpsc::bounded_async::<usize>(100);
let rx = Arc::new(rx);
tokio::spawn(async move {
let _ = rx.recv().await;
});
drop(tx);
}Implementations§
Source§impl<T> AsyncRx<T>
impl<T> AsyncRx<T>
Sourcepub fn recv<'a>(&'a self) -> RecvFuture<'a, T> ⓘ
pub fn recv<'a>(&'a self) -> RecvFuture<'a, T> ⓘ
Receives a message from the channel. This method will await until a message is received or the channel is closed.
This function is cancellation-safe, so it’s safe to use with timeout() and the select! macro.
When a RecvFuture is dropped, no message will be received from the channel.
For timeout scenarios, there’s an alternative: AsyncRx::recv_timeout().
Returns Ok(T) on success.
Returns Err(RecvError) if the sender has been dropped.
Sourcepub fn recv_timeout<'a>(
&'a self,
duration: Duration,
) -> RecvTimeoutFuture<'a, T, ()> ⓘ
Available on crate features tokio or async_std only.
pub fn recv_timeout<'a>( &'a self, duration: Duration, ) -> RecvTimeoutFuture<'a, T, ()> ⓘ
tokio or async_std only.Receives a message from the channel with a timeout. Will await when channel is empty.
The behavior is atomic: the message is either received successfully or the operation is canceled due to a timeout.
Returns Ok(T) when successful.
Returns Err(RecvTimeoutError::Timeout) when a message could not be received because the channel is empty and the operation timed out.
Returns Err(RecvTimeoutError::Disconnected) if the sender has been dropped and the channel is empty.
Sourcepub fn recv_with_timer<'a, F, R>(
&'a self,
fut: F,
) -> RecvTimeoutFuture<'a, T, R> ⓘwhere
F: Future<Output = R> + 'static,
pub fn recv_with_timer<'a, F, R>(
&'a self,
fut: F,
) -> RecvTimeoutFuture<'a, T, R> ⓘwhere
F: Future<Output = R> + 'static,
Receives a message from the channel with a custom timer function (from other async runtime).
The behavior is atomic: the message is either received successfully or the operation is canceled due to a timeout.
Returns Ok(T) when successful.
Returns Err(RecvTimeoutError::Timeout) when a message could not be received because the channel is empty and the operation timed out.
Returns Err(RecvTimeoutError::Disconnected) if the sender has been dropped and the channel is empty.
§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 rx.recv_with_timer(smol::Timer::after(Duration::from_secs(1))).await {
Ok(_item)=>{
println!("message recv");
}
Err(RecvTimeoutError::Timeout)=>{
println!("timeout");
}
Err(RecvTimeoutError::Disconnected)=>{
println!("sender-side closed");
}
}
}Sourcepub fn try_recv(&self) -> Result<T, TryRecvError>
pub fn try_recv(&self) -> Result<T, TryRecvError>
Attempts to receive a message from the channel without blocking.
Returns Ok(T) on successful.
Returns Err(TryRecvError::Empty) if the channel is empty.
Returns Err(TryRecvError::Disconnected) if the sender has been dropped and the channel is empty.
pub fn into_stream(self) -> AsyncStream<T>
pub fn into_blocking(self) -> Rx<T>
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> AsyncRxTrait<T> for AsyncRx<T>
impl<T: Unpin + Send + 'static> AsyncRxTrait<T> for AsyncRx<T>
fn clone_to_vec(self, _count: usize) -> Vec<Self>
Source§fn recv<'a>(&'a self) -> RecvFuture<'a, T> ⓘ
fn recv<'a>(&'a self) -> RecvFuture<'a, T> ⓘ
Source§fn recv_timeout<'a>(
&'a self,
duration: Duration,
) -> RecvTimeoutFuture<'a, T, ()> ⓘ
fn recv_timeout<'a>( &'a self, duration: Duration, ) -> RecvTimeoutFuture<'a, T, ()> ⓘ
tokio or async_std only.