Struct tokio::sync::watch::Receiver[][src]

pub struct Receiver<T> { /* fields omitted */ }
This is supported on crate feature sync only.
Expand description

Receives values from the associated Sender.

Instances are created by the channel function.

To turn this receiver into a Stream, you can use the WatchStream wrapper.

Implementations

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

use tokio::sync::watch;

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

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

use tokio::sync::watch;

#[tokio::main]
async fn main() {
    let (tx, mut rx) = watch::channel("hello");

    tokio::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());
}

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Executes the destructor for this type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.