Struct just_watch::Receiver

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

Receives values from the associated Sender.

Instances are created by the channel function.

Implementations§

source§

impl<T> Receiver<T>

source

pub fn borrow(&self) -> Ref<'_, T>

Returns a reference to the most recently sent value.

Outstanding borrows hold a read lock. This means that long lived borrows could cause the send half to block. It is recommended to keep the borrow as short lived as possible.

Examples
let (_, rx) = just_watch::channel("hello");
assert_eq!(*rx.borrow(), "hello");
source

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

Wait for a change notification.

Returns when a new value has been sent by the Sender since the last time changed() was called. When the Sender half is dropped, Err is returned.

Examples
let (tx, mut rx) = just_watch::channel("hello");

let task = executor.spawn(async move {
    tx.send("goodbye").unwrap();
});

assert!(rx.changed().await.is_ok());
assert_eq!(*rx.borrow(), "goodbye");

// The `tx` handle has been dropped
assert!(rx.changed().await.is_err());

task.await;
});
source§

impl<T: Clone> Receiver<T>

source

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

A convenience helper which combines calling Receiver::changed() and Receiver::borrow() where the borrowed value is cloned and returned.

Note: If this is the first time the function is called on a Receiver instance, then the function will wait until a new value is sent into the channel.

None is returned if the Sender half is dropped.

Examples
let (tx, mut rx) = just_watch::channel("hello");

let task = executor.spawn(async move {
    tx.send("goodbye").unwrap();
});

assert_eq!(*rx.borrow(), "hello");

// Waits for the new task to spawn and send the value.
let v = rx.recv().await.unwrap();
assert_eq!(v, "goodbye");

let v = rx.recv().await;
assert!(v.is_err());

task.await;

Trait Implementations§

source§

impl<T> Clone for Receiver<T>

source§

fn clone(&self) -> Self

Returns a copy 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> 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 Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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 Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.