29
29
//! The [`Receiver`] half provides an asynchronous [`changed`] method. This
30
30
//! method is ready when a new, *unseen* value is sent via the [`Sender`] half.
31
31
//!
32
- //! * [`Receiver::changed()`] returns `Ok(())` on receiving a new value, or
33
- //! `Err(`[`error::RecvError`]`)` if all [`Sender`]s have been dropped.
32
+ //! * [`Receiver::changed()`] returns:
33
+ //! * `Ok(())` on receiving a new value.
34
+ //! * `Err(`[`RecvError`](error::RecvError)`)` if the
35
+ //! channel has been closed __AND__ the current value is *seen*.
34
36
//! * If the current value is *unseen* when calling [`changed`], then
35
37
//! [`changed`] will return immediately. If the current value is *seen*, then
36
38
//! it will sleep until either a new message is sent via the [`Sender`] half,
42
44
//! The current value at the time the [`Receiver`] is created is considered
43
45
//! *seen*.
44
46
//!
45
- //! ## `borrow_and_update` versus `borrow`
47
+ //! ## [`changed`] versus [`has_changed`]
48
+ //!
49
+ //! The [`Receiver`] half provides two methods for checking for changes
50
+ //! in the channel, [`has_changed`] and [`changed`].
51
+ //!
52
+ //! * [`has_changed`] is a *synchronous* method that checks whether the current
53
+ //! value is seen or not and returns a boolean. This method does __not__ mark the
54
+ //! value as seen.
55
+ //!
56
+ //! * [`changed`] is an *asynchronous* method that will return once an unseen
57
+ //! value is in the channel. This method does mark the value as seen.
58
+ //!
59
+ //! Note there are two behavioral differences on when these two methods return
60
+ //! an error.
61
+ //!
62
+ //! - [`has_changed`] errors if and only if the channel is closed.
63
+ //! - [`changed`] errors if the channel has been closed __AND__
64
+ //! the current value is seen.
65
+ //!
66
+ //! See the example below that shows how these methods have different fallibility.
67
+ //!
68
+ //! ## [`borrow_and_update`] versus [`borrow`]
46
69
//!
47
70
//! If the receiver intends to await notifications from [`changed`] in a loop,
48
71
//! [`Receiver::borrow_and_update()`] should be preferred over
84
107
//! # }
85
108
//! ```
86
109
//!
110
+ //! Difference on fallibility of [`changed`] versus [`has_changed`].
111
+ //! ```
112
+ //! use tokio::sync::watch;
113
+ //!
114
+ //! #[tokio::main]
115
+ //! async fn main() {
116
+ //! let (tx, mut rx) = watch::channel("hello");
117
+ //! tx.send("goodbye").unwrap();
118
+ //! drop(tx);
119
+ //!
120
+ //! // `has_changed` does not mark the value as seen and errors
121
+ //! // since the channel is closed.
122
+ //! assert!(rx.has_changed().is_err());
123
+ //!
124
+ //! // `changed` returns Ok since the value is not already marked as seen
125
+ //! // even if the channel is closed.
126
+ //! assert!(rx.changed().await.is_ok());
127
+ //!
128
+ //! // The `changed` call above marks the value as seen.
129
+ //! // The next `changed` call now returns an error as the channel is closed
130
+ //! // AND the current value is seen.
131
+ //! assert!(rx.changed().await.is_err());
132
+ //! }
133
+ //! ```
134
+ //!
87
135
//! # Closing
88
136
//!
89
137
//! [`Sender::is_closed`] and [`Sender::closed`] allow the producer to detect
102
150
//! [`Sender`]: crate::sync::watch::Sender
103
151
//! [`Receiver`]: crate::sync::watch::Receiver
104
152
//! [`changed`]: crate::sync::watch::Receiver::changed
153
+ //! [`has_changed`]: crate::sync::watch::Receiver::has_changed
154
+ //! [`borrow`]: crate::sync::watch::Receiver::borrow
155
+ //! [`borrow_and_update`]: crate::sync::watch::Receiver::borrow_and_update
105
156
//! [`Receiver::changed()`]: crate::sync::watch::Receiver::changed
106
157
//! [`Receiver::borrow()`]: crate::sync::watch::Receiver::borrow
107
158
//! [`Receiver::borrow_and_update()`]:
@@ -637,15 +688,20 @@ impl<T> Receiver<T> {
637
688
}
638
689
639
690
/// Checks if this channel contains a message that this receiver has not yet
640
- /// seen. The new value is not marked as seen.
691
+ /// seen. The current value will not be marked as seen.
692
+ ///
693
+ /// Although this method is called `has_changed`, it does not check
694
+ /// messages for equality, so this call will return true even if the current
695
+ /// message is equal to the previous message.
696
+ ///
697
+ /// # Errors
641
698
///
642
- /// Although this method is called `has_changed`, it does not check new
643
- /// messages for equality, so this call will return true even if the new
644
- /// message is equal to the old message.
699
+ /// Returns a [`RecvError`](error::RecvError) if and only if the channel has been closed.
645
700
///
646
- /// Returns an error if the channel has been closed.
647
701
/// # Examples
648
702
///
703
+ /// ## Basic usage
704
+ ///
649
705
/// ```
650
706
/// use tokio::sync::watch;
651
707
///
@@ -660,9 +716,22 @@ impl<T> Receiver<T> {
660
716
///
661
717
/// // The value has been marked as seen
662
718
/// assert!(!rx.has_changed().unwrap());
719
+ /// }
720
+ /// ```
721
+ ///
722
+ /// ## Closed channel example
723
+ ///
724
+ /// ```
725
+ /// use tokio::sync::watch;
726
+ ///
727
+ /// #[tokio::main]
728
+ /// async fn main() {
729
+ /// let (tx, rx) = watch::channel("hello");
730
+ /// tx.send("goodbye").unwrap();
663
731
///
664
732
/// drop(tx);
665
- /// // The `tx` handle has been dropped
733
+ ///
734
+ /// // The channel is closed
666
735
/// assert!(rx.has_changed().is_err());
667
736
/// }
668
737
/// ```
@@ -701,19 +770,22 @@ impl<T> Receiver<T> {
701
770
self . version = current_version;
702
771
}
703
772
704
- /// Waits for a change notification, then marks the newest value as seen.
773
+ /// Waits for a change notification, then marks the current value as seen.
705
774
///
706
- /// If the newest value in the channel has not yet been marked seen when
775
+ /// If the current value in the channel has not yet been marked seen when
707
776
/// this method is called, the method marks that value seen and returns
708
777
/// immediately. If the newest value has already been marked seen, then the
709
778
/// method sleeps until a new message is sent by a [`Sender`] connected to
710
779
/// this `Receiver`, or until all [`Sender`]s are dropped.
711
780
///
712
- /// This method returns an error if and only if all [`Sender`]s are dropped.
713
- ///
714
781
/// For more information, see
715
782
/// [*Change notifications*](self#change-notifications) in the module-level documentation.
716
783
///
784
+ /// # Errors
785
+ ///
786
+ /// Returns a [`RecvError`](error::RecvError) if the channel has been closed __AND__
787
+ /// the current value is seen.
788
+ ///
717
789
/// # Cancel safety
718
790
///
719
791
/// This method is cancel safe. If you use it as the event in a
0 commit comments