mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-20 00:00:08 +02:00
sync: add watch::Receiver::mark_unchanged (#6252)
This commit is contained in:
@@ -669,6 +669,17 @@ impl<T> Receiver<T> {
|
||||
self.version.decrement();
|
||||
}
|
||||
|
||||
/// 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.
|
||||
pub fn mark_unchanged(&mut self) {
|
||||
let current_version = self.shared.state.load().version();
|
||||
self.version = current_version;
|
||||
}
|
||||
|
||||
/// 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
|
||||
|
||||
@@ -102,6 +102,39 @@ fn rx_mark_changed() {
|
||||
assert_eq!(*rx.borrow(), "two");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rx_mark_unchanged() {
|
||||
let (tx, mut rx) = watch::channel("one");
|
||||
|
||||
let mut rx2 = rx.clone();
|
||||
|
||||
{
|
||||
assert!(!rx.has_changed().unwrap());
|
||||
|
||||
rx.mark_changed();
|
||||
assert!(rx.has_changed().unwrap());
|
||||
|
||||
rx.mark_unchanged();
|
||||
assert!(!rx.has_changed().unwrap());
|
||||
|
||||
let mut t = spawn(rx.changed());
|
||||
assert_pending!(t.poll());
|
||||
}
|
||||
|
||||
{
|
||||
assert!(!rx2.has_changed().unwrap());
|
||||
|
||||
tx.send("two").unwrap();
|
||||
assert!(rx2.has_changed().unwrap());
|
||||
|
||||
rx2.mark_unchanged();
|
||||
assert!(!rx2.has_changed().unwrap());
|
||||
assert_eq!(*rx2.borrow_and_update(), "two");
|
||||
}
|
||||
|
||||
assert_eq!(*rx.borrow(), "two");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multi_rx() {
|
||||
let (tx, mut rx1) = watch::channel("one");
|
||||
|
||||
Reference in New Issue
Block a user