Skip to content

Commit

Permalink
Use Clone instead of Copy in Watcher (#47)
Browse files Browse the repository at this point in the history
This allows you to use it with various types that require allocations
for example.
  • Loading branch information
CryZe committed Jul 17, 2023
1 parent 15f3a85 commit 62e35f6
Showing 1 changed file with 14 additions and 12 deletions.
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

0 comments on commit 62e35f6

Please sign in to comment.