pub struct MAsyncRx<T>(/* private fields */);
Expand description
A multi-consumer (receiver) that works in an async context.
Inherits from AsyncRx<T>
and implements Clone
.
Additional methods in ChannelShared can be accessed through Deref
.
You can use into()
to convert it to AsyncRx<T>
.
MAsyncRx
can be converted into MRx
via the From
trait,
which means you can have two types of receivers, both within async and
blocking contexts, for the same channel.
Implementations§
Source§impl<T> MAsyncRx<T>
impl<T> MAsyncRx<T>
pub fn into_stream(self) -> AsyncStream<T>
pub fn into_blocking(self) -> MRx<T>
Methods from Deref<Target = 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 offut
is ignore. We add genericR
just 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.
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 MAsyncRx<T>
impl<T: Unpin + Send + 'static> AsyncRxTrait<T> for MAsyncRx<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.