Skip to content

Commit 02b779e

Browse files
authored
sync: add watch::Receiver::mark_unchanged (#6252)
1 parent 48345d6 commit 02b779e

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

tokio/src/sync/watch.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,17 @@ impl<T> Receiver<T> {
669669
self.version.decrement();
670670
}
671671

672+
/// Marks the state as unchanged.
673+
///
674+
/// The current value will be considered seen by the receiver.
675+
///
676+
/// This is useful if you are not interested in the current value
677+
/// visible in the receiver.
678+
pub fn mark_unchanged(&mut self) {
679+
let current_version = self.shared.state.load().version();
680+
self.version = current_version;
681+
}
682+
672683
/// Waits for a change notification, then marks the newest value as seen.
673684
///
674685
/// If the newest value in the channel has not yet been marked seen when

tokio/tests/sync_watch.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,39 @@ fn rx_mark_changed() {
102102
assert_eq!(*rx.borrow(), "two");
103103
}
104104

105+
#[test]
106+
fn rx_mark_unchanged() {
107+
let (tx, mut rx) = watch::channel("one");
108+
109+
let mut rx2 = rx.clone();
110+
111+
{
112+
assert!(!rx.has_changed().unwrap());
113+
114+
rx.mark_changed();
115+
assert!(rx.has_changed().unwrap());
116+
117+
rx.mark_unchanged();
118+
assert!(!rx.has_changed().unwrap());
119+
120+
let mut t = spawn(rx.changed());
121+
assert_pending!(t.poll());
122+
}
123+
124+
{
125+
assert!(!rx2.has_changed().unwrap());
126+
127+
tx.send("two").unwrap();
128+
assert!(rx2.has_changed().unwrap());
129+
130+
rx2.mark_unchanged();
131+
assert!(!rx2.has_changed().unwrap());
132+
assert_eq!(*rx2.borrow_and_update(), "two");
133+
}
134+
135+
assert_eq!(*rx.borrow(), "two");
136+
}
137+
105138
#[test]
106139
fn multi_rx() {
107140
let (tx, mut rx1) = watch::channel("one");

0 commit comments

Comments
 (0)