MAsyncRx

Struct MAsyncRx 

Source
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>

Source

pub fn into_stream(self) -> AsyncStream<T>
where T: Send + Unpin + 'static,

Source

pub fn into_blocking(self) -> MRx<T>

Methods from Deref<Target = AsyncRx<T>>§

Source

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.

Source

pub fn recv_timeout<'a>( &'a self, duration: Duration, ) -> RecvTimeoutFuture<'a, T, ()>

Available on crate features 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.

Source

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 of fut is ignore. We add generic R 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");
        }
    }
}
Source

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>>§

Source

pub fn len(&self) -> usize

The number of messages in the channel.

Source

pub fn capacity(&self) -> Option<usize>

The capacity of the channel. Returns None for unbounded channels.

Source

pub fn is_empty(&self) -> bool

Returns true if the channel is empty.

Source

pub fn is_full(&self) -> bool

Returns true if the channel is full.

Source

pub fn is_disconnected(&self) -> bool

Returns true if all senders or receivers have been dropped.

Source

pub fn get_tx_count(&self) -> usize

Returns the number of senders for the channel.

Source

pub fn get_rx_count(&self) -> usize

Returns the number of receivers for the channel.

Source

pub fn get_wakers_count(&self) -> (usize, usize)

Returns the number of wakers for senders and receivers. For debugging purposes.

Trait Implementations§

Source§

impl<T> AsRef<ChannelShared<T>> for MAsyncRx<T>

Source§

fn as_ref(&self) -> &ChannelShared<T>

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<T: Unpin + Send + 'static> AsyncRxTrait<T> for MAsyncRx<T>

Source§

fn clone_to_vec(self, count: usize) -> Vec<Self>

Source§

fn try_recv(&self) -> Result<T, TryRecvError>

Try to receive message, non-blocking. Read more
Source§

fn recv<'a>(&'a self) -> RecvFuture<'a, T>

Receive message, will await when channel is empty. Read more
Source§

fn recv_timeout<'a>( &'a self, duration: Duration, ) -> RecvTimeoutFuture<'a, T, ()>

Available on crate features tokio or async_std only.
Waits for a message to be received from the channel, but only for a limited time. Will await when channel is empty. Read more
Source§

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). Read more
Source§

fn len(&self) -> usize

The number of messages in the channel at the moment
Source§

fn capacity(&self) -> Option<usize>

The capacity of the channel, return None for unbounded channel.
Source§

fn is_empty(&self) -> bool

Whether channel is empty at the moment
Source§

fn is_full(&self) -> bool

Whether the channel is full at the moment
Source§

fn is_disconnected(&self) -> bool

Return true if the other side has closed
Source§

impl<T> Clone for MAsyncRx<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for MAsyncRx<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Deref for MAsyncRx<T>

Source§

fn deref(&self) -> &Self::Target

inherit all the functions of AsyncRx

Source§

type Target = AsyncRx<T>

The resulting type after dereferencing.
Source§

impl<T> Display for MAsyncRx<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> From<MAsyncRx<T>> for AsyncRx<T>

Source§

fn from(rx: MAsyncRx<T>) -> Self

Converts to this type from the input type.
Source§

impl<T: Unpin + Send + 'static> From<MAsyncRx<T>> for AsyncStream<T>

Source§

fn from(rx: MAsyncRx<T>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<MAsyncRx<T>> for MRx<T>

Source§

fn from(value: MAsyncRx<T>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<MRx<T>> for MAsyncRx<T>

Source§

fn from(value: MRx<T>) -> Self

Converts to this type from the input type.
Source§

impl<T: Send> Sync for MAsyncRx<T>

Auto Trait Implementations§

§

impl<T> !Freeze for MAsyncRx<T>

§

impl<T> !RefUnwindSafe for MAsyncRx<T>

§

impl<T> Send for MAsyncRx<T>
where T: Send,

§

impl<T> Unpin for MAsyncRx<T>

§

impl<T> !UnwindSafe for MAsyncRx<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.