Skip to content

Commit

Permalink
Merge branch 'master' into fix-local-enter-guard
Browse files Browse the repository at this point in the history
  • Loading branch information
inq authored Sep 19, 2023
2 parents b887953 + 8045118 commit cdc5ca7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
13 changes: 10 additions & 3 deletions tokio/src/sync/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ mod state {
impl Version {
/// Get the initial version when creating the channel.
pub(super) fn initial() -> Self {
// The initial version is 1 so that `mark_unseen` can decrement by one.
// The initial version is 1 so that `mark_changed` can decrement by one.
// (The value is 2 due to the closed bit.)
Version(2)
}
Expand Down Expand Up @@ -644,8 +644,15 @@ impl<T> Receiver<T> {
Ok(self.version != new_version)
}

/// Marks the state as unseen.
pub fn mark_unseen(&mut self) {
/// Marks the state as changed.
///
/// After invoking this method [`has_changed()`](Self::has_changed)
/// returns `true` and [`changed()`](Self::changed) 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.
pub fn mark_changed(&mut self) {
self.version.decrement();
}

Expand Down
12 changes: 6 additions & 6 deletions tokio/tests/sync_watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,19 @@ fn rx_version_underflow() {
let (_tx, mut rx) = watch::channel("one");

// Version starts at 2, validate we do not underflow
rx.mark_unseen();
rx.mark_unseen();
rx.mark_changed();
rx.mark_changed();
}

#[test]
fn rx_mark_unseen() {
fn rx_mark_changed() {
let (tx, mut rx) = watch::channel("one");

let mut rx2 = rx.clone();
let mut rx3 = rx.clone();
let mut rx4 = rx.clone();
{
rx.mark_unseen();
rx.mark_changed();
assert!(rx.has_changed().unwrap());

let mut t = spawn(rx.changed());
Expand All @@ -76,7 +76,7 @@ fn rx_mark_unseen() {
}

{
rx3.mark_unseen();
rx3.mark_changed();
assert_eq!(*rx3.borrow(), "one");

assert!(rx3.has_changed().unwrap());
Expand All @@ -94,7 +94,7 @@ fn rx_mark_unseen() {
assert!(rx4.has_changed().unwrap());
assert_eq!(*rx4.borrow_and_update(), "two");

rx4.mark_unseen();
rx4.mark_changed();
assert!(rx4.has_changed().unwrap());
assert_eq!(*rx4.borrow_and_update(), "two")
}
Expand Down

0 comments on commit cdc5ca7

Please sign in to comment.