-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sync: improve docs for watch channels #5954
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
68923a7
sync: improve docs for watch channels
sunshowers 6d7ec40
address review comment
sunshowers 0b7f413
also make it clearer that borrow doesn't change its state
sunshowers 7091060
address review comment
sunshowers cc0433d
more review comments
sunshowers 4caf956
add links to borrow_and_update-versus-borrow
sunshowers File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,24 +10,75 @@ | |
//! | ||
//! [`channel`] returns a [`Sender`] / [`Receiver`] pair. These are the producer | ||
//! and consumer halves of the channel. The channel is created with an initial | ||
//! value. The **latest** value stored in the channel is accessed with | ||
//! [`Receiver::borrow()`]. Awaiting [`Receiver::changed()`] waits for a new | ||
//! value to be sent by the [`Sender`] half. | ||
//! value. | ||
//! | ||
//! Each [`Receiver`] independently tracks the last value *seen* by its caller. | ||
//! | ||
//! To access the **current** value stored in the channel and mark it as *seen* | ||
//! by a given [`Receiver`], use [`Receiver::borrow_and_update()`]. | ||
//! | ||
//! To access the current value **without** marking it as *seen*, use | ||
//! [`Receiver::borrow()`]. (If the value has already been marked *seen*, | ||
//! [`Receiver::borrow()`] is equivalent to [`Receiver::borrow_and_update()`].) | ||
//! | ||
//! For more information on when to use these methods, see | ||
//! [here](#borrow_and_update-versus-borrow). | ||
//! | ||
//! ## Change notifications | ||
hawkw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
//! | ||
//! The [`Receiver`] half provides an asynchronous [`changed`] method. This | ||
//! method is ready when a new, *unseen* value is sent via the [`Sender`] half. | ||
//! | ||
//! * [`Receiver::changed()`] returns `Ok(())` on receiving a new value, or | ||
//! `Err(`[`error::RecvError`]`)` if the [`Sender`] has been dropped. | ||
//! * If the current value is *unseen* when calling [`changed`], then | ||
//! [`changed`] will return immediately. If the current value is *seen*, then | ||
//! it will sleep until either a new message is sent via the [`Sender`] half, | ||
//! or the [`Sender`] is dropped. | ||
//! * On completion, the [`changed`] method marks the new value as *seen*. | ||
//! * At creation, the initial value is considered *seen*. In other words, | ||
//! [`Receiver::changed()`] will not return until a subsequent value is sent. | ||
//! * New [`Receiver`] instances can be created with [`Sender::subscribe()`]. | ||
//! The current value at the time the [`Receiver`] is created is considered | ||
//! *seen*. | ||
//! | ||
//! ## `borrow_and_update` versus `borrow` | ||
//! | ||
//! If the receiver intends to await notifications from [`changed`] in a loop, | ||
//! [`Receiver::borrow_and_update()`] should be preferred over | ||
//! [`Receiver::borrow()`]. This avoids a potential race where a new value is | ||
//! sent between [`changed`] being ready and the value being read. (If | ||
//! [`Receiver::borrow()`] is used, the loop may run twice with the same value.) | ||
//! | ||
//! If the receiver is only interested in the current value, and does not intend | ||
//! to wait for changes, then [`Receiver::borrow()`] can be used. It may be more | ||
//! convenient to use [`borrow`](Receiver::borrow) since it's an `&self` | ||
//! method---[`borrow_and_update`](Receiver::borrow_and_update) requires `&mut | ||
//! self`. | ||
Comment on lines
+45
to
+57
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is fantastic --- but i think the doc comments for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
//! | ||
//! # Examples | ||
//! | ||
//! The following example prints `hello! world! `. | ||
//! | ||
//! ``` | ||
//! use tokio::sync::watch; | ||
//! use tokio::time::{Duration, sleep}; | ||
//! | ||
//! # async fn dox() -> Result<(), Box<dyn std::error::Error>> { | ||
//! let (tx, mut rx) = watch::channel("hello"); | ||
//! | ||
//! tokio::spawn(async move { | ||
//! while rx.changed().await.is_ok() { | ||
//! println!("received = {:?}", *rx.borrow()); | ||
//! // Use the equivalent of a "do-while" loop so the initial value is | ||
//! // processed before awaiting the `changed()` future. | ||
//! loop { | ||
//! println!("{}! ", *rx.borrow_and_update()); | ||
//! if rx.changed().await.is_err() { | ||
//! break; | ||
//! } | ||
//! } | ||
//! }); | ||
//! | ||
//! sleep(Duration::from_millis(100)).await; | ||
//! tx.send("world")?; | ||
//! # Ok(()) | ||
//! # } | ||
|
@@ -39,8 +90,8 @@ | |
//! when all [`Receiver`] handles have been dropped. This indicates that there | ||
//! is no further interest in the values being produced and work can be stopped. | ||
//! | ||
//! The value in the channel will not be dropped until the sender and all receivers | ||
//! have been dropped. | ||
//! The value in the channel will not be dropped until the sender and all | ||
//! receivers have been dropped. | ||
//! | ||
//! # Thread safety | ||
//! | ||
|
@@ -50,11 +101,15 @@ | |
//! | ||
//! [`Sender`]: crate::sync::watch::Sender | ||
//! [`Receiver`]: crate::sync::watch::Receiver | ||
//! [`changed`]: crate::sync::watch::Receiver::changed | ||
//! [`Receiver::changed()`]: crate::sync::watch::Receiver::changed | ||
//! [`Receiver::borrow()`]: crate::sync::watch::Receiver::borrow | ||
//! [`Receiver::borrow_and_update()`]: | ||
//! crate::sync::watch::Receiver::borrow_and_update | ||
//! [`channel`]: crate::sync::watch::channel | ||
//! [`Sender::is_closed`]: crate::sync::watch::Sender::is_closed | ||
//! [`Sender::closed`]: crate::sync::watch::Sender::closed | ||
//! [`Sender::subscribe()`]: crate::sync::watch::Sender::subscribe | ||
|
||
use crate::sync::notify::Notify; | ||
|
||
|
@@ -374,19 +429,28 @@ mod state { | |
/// | ||
/// # Examples | ||
/// | ||
/// The following example prints `hello! world! `. | ||
/// | ||
/// ``` | ||
/// use tokio::sync::watch; | ||
/// use tokio::time::{Duration, sleep}; | ||
/// | ||
/// # async fn dox() -> Result<(), Box<dyn std::error::Error>> { | ||
/// let (tx, mut rx) = watch::channel("hello"); | ||
/// let (tx, mut rx) = watch::channel("hello"); | ||
/// | ||
/// tokio::spawn(async move { | ||
/// while rx.changed().await.is_ok() { | ||
/// println!("received = {:?}", *rx.borrow()); | ||
/// tokio::spawn(async move { | ||
/// // Use the equivalent of a "do-while" loop so the initial value is | ||
/// // processed before awaiting the `changed()` future. | ||
/// loop { | ||
/// println!("{}! ", *rx.borrow_and_update()); | ||
/// if rx.changed().await.is_err() { | ||
/// break; | ||
/// } | ||
/// }); | ||
/// } | ||
/// }); | ||
/// | ||
/// tx.send("world")?; | ||
/// sleep(Duration::from_millis(100)).await; | ||
/// tx.send("world")?; | ||
/// # Ok(()) | ||
/// # } | ||
/// ``` | ||
|
@@ -453,7 +517,11 @@ impl<T> Receiver<T> { | |
/// ``` | ||
/// </details> | ||
/// | ||
/// For more information on when to use this method versus | ||
/// [`borrow_and_update`], see [here](self#borrow_and_update-versus-borrow). | ||
/// | ||
/// [`changed`]: Receiver::changed | ||
/// [`borrow_and_update`]: Receiver::borrow_and_update | ||
/// | ||
/// # Examples | ||
/// | ||
|
@@ -505,7 +573,11 @@ impl<T> Receiver<T> { | |
/// ``` | ||
/// </details> | ||
/// | ||
/// For more information on when to use this method versus [`borrow`], see | ||
/// [here](self#borrow_and_update-versus-borrow). | ||
/// | ||
/// [`changed`]: Receiver::changed | ||
/// [`borrow`]: Receiver::borrow | ||
pub fn borrow_and_update(&mut self) -> Ref<'_, T> { | ||
let inner = self.shared.value.read().unwrap(); | ||
|
||
|
@@ -572,6 +644,9 @@ impl<T> Receiver<T> { | |
/// | ||
/// This method returns an error if and only if the [`Sender`] is dropped. | ||
/// | ||
/// For more information, see | ||
/// [*Change notifications*](self#change-notifications) in the module-level documentation. | ||
/// | ||
/// # Cancel safety | ||
/// | ||
/// This method is cancel safe. If you use it as the event in a | ||
|
@@ -595,7 +670,7 @@ impl<T> Receiver<T> { | |
/// }); | ||
/// | ||
/// assert!(rx.changed().await.is_ok()); | ||
/// assert_eq!(*rx.borrow(), "goodbye"); | ||
/// assert_eq!(*rx.borrow_and_update(), "goodbye"); | ||
/// | ||
/// // The `tx` handle has been dropped | ||
/// assert!(rx.changed().await.is_err()); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you think it's worth explicitly stating something like "
borrow_and_update()
should be preferred if the receiver intends to await notifications fromchanged()
, but it is not required if the receiver is just looking at the current value of the shared state without waiting for changes"?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added this to the new section, and also mentioned why
borrow
can be more convenient (it's&self
).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you think we should maybe add a
or something?