pub struct Receiver<T> { /* private fields */ }
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
sourceimpl<T> Receiver<T>
impl<T> Receiver<T>
sourcepub fn borrow(&self) -> Ref<'_, T>
pub fn borrow(&self) -> Ref<'_, T>
Returns a reference to the most recently sent value.
This method does not mark the returned value as seen, so future calls to
changed
may return immediately even if you have already seen the
value with a call to borrow
.
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");
sourcepub fn borrow_and_update(&mut self) -> Ref<'_, T>
pub fn borrow_and_update(&mut self) -> Ref<'_, T>
Returns a reference to the most recently sent value and mark that value as seen.
This method marks the value as seen, so changed
will not return
immediately if the newest value is one previously returned by
borrow_and_update
.
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.
sourcepub async fn changed(&mut self) -> Result<(), RecvError>
pub async fn changed(&mut self) -> Result<(), RecvError>
Waits for a change notification, then marks the newest value as seen.
If the newest value in the channel has not yet been marked seen when
this method is called, the method marks that value seen and returns
immediately. If the newest value has already been marked seen, then the
method sleeps until a new message is sent by the Sender
connected to
this Receiver
, or until the Sender
is dropped.
This method returns an error if and only if the Sender
is dropped.
Cancel safety
This method is cancel safe. If you use it as the event in a
tokio::select!
statement and some other branch
completes first, then it is guaranteed that no values have been marked
seen by this call to changed
.
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
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
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more