firewheel_core/diff/
memo.rs

1use super::{Diff, EventQueue, PathBuilder};
2
3/// A "memoized" parameters wrapper.
4///
5/// This type simplifies diffing management for
6/// standalone parameters.
7#[derive(Debug, Clone, Copy, Default)]
8pub struct Memo<T> {
9    value: T,
10    baseline: T,
11}
12
13impl<T: Diff + Clone> Memo<T> {
14    /// Construct a new [`Memo`].
15    ///
16    /// This clones the provided value to maintain
17    /// a baseline for diffing.
18    pub fn new(value: T) -> Self {
19        Self {
20            baseline: value.clone(),
21            value,
22        }
23    }
24
25    /// Generate events if the inner value has changed.
26    ///
27    /// This will also clone the inner value and assign it to the baseline.
28    /// This may be inneficient if cloning is slow.
29    pub fn update_memo<E: EventQueue>(&mut self, event_queue: &mut E) {
30        self.value
31            .diff(&self.baseline, PathBuilder::default(), event_queue);
32        self.baseline = self.value.clone();
33    }
34}
35
36impl<T> core::ops::Deref for Memo<T> {
37    type Target = T;
38
39    fn deref(&self) -> &Self::Target {
40        &self.value
41    }
42}
43
44impl<T> core::ops::DerefMut for Memo<T> {
45    fn deref_mut(&mut self) -> &mut Self::Target {
46        &mut self.value
47    }
48}