Struct Receiver
struct Receiver<T> { ... }
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
impl<T> Receiver<T>
fn borrow(self: &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
changedmay return immediately even if you have already seen the value with a call toborrow.Outstanding borrows hold a read lock on the inner value. This means that long-lived borrows could cause the producer half to block. It is recommended to keep the borrow as short-lived as possible. Additionally, if you are running in an environment that allows
!Sendfutures, you must ensure that the returnedReftype is never held alive across an.awaitpoint, otherwise, it can lead to a deadlock.The priority policy of the lock is dependent on the underlying lock implementation, and this type does not guarantee that any particular policy will be used. In particular, a producer which is waiting to acquire the lock in
sendmight or might not block concurrent calls toborrow, e.g.:Potential deadlock example
// Task 1 (on thread A) | // Task 2 (on thread B) let _ref1 = rx.borrow(); | | // will block | let _ = tx.send(()); // may deadlock | let _ref2 = rx.borrow(); |For more information on when to use this method versus
borrow_and_update, see here.Examples
use watch; let = channel; assert_eq!;fn borrow_and_update(self: &mut Self) -> Ref<'_, T>Returns a reference to the most recently sent value and marks that value as seen.
This method marks the current value as seen. Subsequent calls to
changedwill not return immediately until theSenderhas modified the shared value again.Outstanding borrows hold a read lock on the inner value. This means that long-lived borrows could cause the producer half to block. It is recommended to keep the borrow as short-lived as possible. Additionally, if you are running in an environment that allows
!Sendfutures, you must ensure that the returnedReftype is never held alive across an.awaitpoint, otherwise, it can lead to a deadlock.The priority policy of the lock is dependent on the underlying lock implementation, and this type does not guarantee that any particular policy will be used. In particular, a producer which is waiting to acquire the lock in
sendmight or might not block concurrent calls toborrow, e.g.:Potential deadlock example
// Task 1 (on thread A) | // Task 2 (on thread B) let _ref1 = rx1.borrow_and_update(); | | // will block | let _ = tx.send(()); // may deadlock | let _ref2 = rx2.borrow_and_update(); |For more information on when to use this method versus
borrow, see here.fn has_changed(self: &Self) -> Result<bool, error::RecvError>Checks if this channel contains a message that this receiver has not yet seen. The new value is not marked as seen.
Although this method is called
has_changed, it does not check new messages for equality, so this call will return true even if the new message is equal to the old message.Returns an error if the channel has been closed.
Examples
use watch; asyncfn mark_changed(self: &mut Self)Marks the state as changed.
After invoking this method
has_changed()returnstrueandchanged()returns immediately, regardless of whether a new value has been sent.This is useful for triggering an initial change notification after subscribing to synchronize new receivers.
fn mark_unchanged(self: &mut Self)Marks the state as unchanged.
The current value will be considered seen by the receiver.
This is useful if you are not interested in the current value visible in the receiver.
async fn changed(self: &mut Self) -> Result<(), error::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
Senderconnected to thisReceiver, or until theSenderis dropped.This method returns an error if and only if the
Senderis dropped.For more information, see Change notifications in the module-level documentation.
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 tochanged.Examples
use watch; asyncasync fn wait_for<impl FnMut(&T) -> bool: FnMut(&T) -> bool>(self: &mut Self, f: impl FnMut(&T) -> bool) -> Result<Ref<'_, T>, error::RecvError>Waits for a value that satisfies the provided condition.
This method will call the provided closure whenever something is sent on the channel. Once the closure returns
true, this method will return a reference to the value that was passed to the closure.Before
wait_forstarts waiting for changes, it will call the closure on the current value. If the closure returnstruewhen given the current value, thenwait_forwill immediately return a reference to the current value. This is the case even if the current value is already considered seen.The watch channel only keeps track of the most recent value, so if several messages are sent faster than
wait_foris able to call the closure, then it may skip some updates. Whenever the closure is called, it will be called with the most recent value.When this function returns, the value that was passed to the closure when it returned
truewill be considered seen.If the channel is closed, then
wait_forwill return aRecvError. Once this happens, no more messages can ever be sent on the channel. When an error is returned, it is guaranteed that the closure has been called on the last value, and that it returnedfalsefor that value. (If the closure returnedtrue, then the last value would have been returned instead of the error.)Like the
borrowmethod, the returned borrow holds a read lock on the inner value. This means that long-lived borrows could cause the producer half to block. It is recommended to keep the borrow as short-lived as possible. See the documentation ofborrowfor more information on this.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 the last seen valueval(if any) satisfiesf(val) == false.Panics
If and only if the closure
fpanics. In that case, no resource owned or shared by thisReceiverwill be poisoned.Examples
use watch; use ; asyncfn same_channel(self: &Self, other: &Self) -> boolReturns
trueif receivers belong to the same channel.Examples
let = channel; let rx2 = rx.clone; assert!; let = channel; assert!;
impl<T> Any for Receiver<T>
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for Receiver<T>
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for Receiver<T>
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> Clone for Receiver<T>
fn clone(self: &Self) -> Self
impl<T> CloneToUninit for Receiver<T>
unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)
impl<T> Drop for Receiver<T>
fn drop(self: &mut Self)
impl<T> Freeze for Receiver<T>
impl<T> From for Receiver<T>
fn from(t: T) -> TReturns the argument unchanged.
impl<T> RefUnwindSafe for Receiver<T>
impl<T> Send for Receiver<T>
impl<T> Sync for Receiver<T>
impl<T> ToOwned for Receiver<T>
fn to_owned(self: &Self) -> Tfn clone_into(self: &Self, target: &mut T)
impl<T> Unpin for Receiver<T>
impl<T> UnwindSafe for Receiver<T>
impl<T, U> Into for Receiver<T>
fn into(self: Self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom for Receiver<T>
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto for Receiver<T>
fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>
impl<T: $crate::fmt::Debug> Debug for Receiver<T>
fn fmt(self: &Self, f: &mut $crate::fmt::Formatter<'_>) -> $crate::fmt::Result