Skip to content
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

Use Clone instead of Copy in Watcher #47

Merged
merged 1 commit into from
Jul 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions src/watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,18 @@ impl<T> Watcher<T> {
}
}

impl<T: Copy> Watcher<T> {
impl<T: Clone> Watcher<T> {
/// Updates the watcher with a new value. Returns the pair if the value
/// provided is not `None`.
/// provided is not [`None`].
pub fn update(&mut self, value: Option<T>) -> Option<&Pair<T>> {
match (&mut self.pair, value) {
(None, Some(value)) => {
self.pair = Some(Pair {
old: value,
old: value.clone(),
current: value,
});
}
(Some(pair), Some(value)) => {
pair.old = mem::replace(&mut pair.current, value);
}
(Some(pair), Some(value)) => pair.old = mem::replace(&mut pair.current, value),
_ => {
self.pair = None;
}
Expand All @@ -51,12 +49,16 @@ impl<T: Copy> Watcher<T> {
/// Updates the watcher with a new value that always exists. The pair is
/// then returned.
pub fn update_infallible(&mut self, value: T) -> &Pair<T> {
let pair = self.pair.get_or_insert(Pair {
old: value,
current: value,
});
pair.old = mem::replace(&mut pair.current, value);
pair
match &mut self.pair {
None => {
self.pair = Some(Pair {
old: value.clone(),
current: value,
});
}
Some(pair) => pair.old = mem::replace(&mut pair.current, value),
}
self.pair.as_ref().unwrap()
}
}

Expand Down