Receiver

Struct Receiver 

Source
pub struct Receiver<T> { /* private fields */ }
Expand description

Receives values from the associated Sender.

Created by the channel function.

Implementations§

Source§

impl<T> Receiver<T>

Source

pub fn observe<F, R>(&self, f: F) -> R
where F: FnOnce(&T) -> R,

Observes the latest value sent to the channel.

This method takes a closure and calls it with a reference to the value. While the closure is running send calls are blocked. Because of this, the closure should run only as long as needed. A common pattern is to copy or clone the value inside the closure, then return and work with the copy outside.

You can observe the value at any time, but usually you want to wait until it changes. For that, use the changed async method.

§Examples
let (tx, mut rx) = async_observe::channel(0);

// Send a new value
tx.send(1);

// Wait until the value changes
rx.changed().await?;

// Now we can read the new value
let n = rx.observe(|n| *n);
assert_eq!(n, 1);

If the value type implements Clone, you can use recv instead, which waits for a change and returns the new value.

let (tx, mut rx) = async_observe::channel(0);

// Send a new value
tx.send(1);

// Wait until the value changes and read it
let n = rx.recv().await?;
assert_eq!(n, 1);
§Possible deadlock

Calling send inside the closure will deadlock:

let (tx, rx) = async_observe::channel(0);
rx.observe(|n| {
    _ = tx.send(n + 1);
});
Source

pub async fn changed(&mut self) -> Result<(), RecvError>

Waits for the value to change.

Call observe to read the new value.

Source

pub async fn recv(&mut self) -> Result<T, RecvError>
where T: Clone,

Waits for the value to change and then returns a clone of it.

§Examples
let (tx, mut rx) = async_observe::channel(0);

// Send a new value
tx.send(1);

// Wait until the value changes and read it
let n = rx.recv().await?;
assert_eq!(n, 1);
Source

pub fn into_stream(self) -> impl Stream<Item = T>
where T: Clone,

Creates a stream from the receiver.

The stream ends when the Sender is dropped.

§Examples
use futures_lite::{StreamExt, future};

let (tx, rx) = async_observe::channel(0);

let producer = async move {
//                   ^^^^
// Move tx into the future so it is dropped after the loop ends.
// Dropping the sender is important so the receiver can
// see it and stop the stream.

    for n in 1..10 {
        wait_long_time().await;
        _ = tx.send(n);
    }
};

// Create a stream from the receiver
let consumer = rx
    .into_stream()
    .for_each(|n| println!("{n}"));

future::zip(producer, consumer).await;
§Return type

Due to implementation details, the stream currently does not have a named type. For example, you cannot store this stream as a struct field (without a generic) or use it as an associated type in a trait implementation. As long as you don’t need that, just use the returned anonymous type directly.

However, if you need a named type, one solution is to box the stream:

use {
    async_observe::Receiver,
    futures_lite::{StreamExt, stream::Boxed},
};

fn named_stream(rx: Receiver<u32>) -> Boxed<u32> {
    rx.into_stream().boxed()
}

Trait Implementations§

Source§

impl<T> Clone for Receiver<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> Debug for Receiver<T>

Source§

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

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

impl<T> Drop for Receiver<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Receiver<T>

§

impl<T> RefUnwindSafe for Receiver<T>

§

impl<T> Send for Receiver<T>
where T: Send + Sync,

§

impl<T> Sync for Receiver<T>
where T: Send + Sync,

§

impl<T> Unpin for Receiver<T>

§

impl<T> UnwindSafe for Receiver<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<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, 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.