Struct DataTracker

Source
pub struct DataTracker<T, K>
where T: Clone + PartialEq, K: Hash + Eq,
{ /* private fields */ }
Expand description

Tracks changes to data and notifies listeners.

The data to be tracked is type T.

Callbacks are stored in a HashMap with keys of type K.

See the module-level documentation for more details.

Implementations§

Source§

impl<T, K> DataTracker<T, K>
where T: Clone + PartialEq, K: Hash + Eq,

Source

pub fn new(value: T) -> DataTracker<T, K>

Create a new DataTracker which takes ownership of the data of type T.

Callbacks are registered via a key of type K.

Examples found in repository?
examples/simple.rs (line 11)
10fn main() {
11    let mut data = DataTracker::new(MyData { a: 1 });
12    let key = 0; // Keep the key to remove the callback later.
13    data.add_listener(key,
14                      Box::new(|old_value: &MyData, new_value: &MyData| {
15                          println!("changed {:?} -> {:?}", old_value, new_value);
16                      }));
17
18    {
19        // Create x, a (non-mutable) reference to original data.
20        let x = data.as_ref();
21        println!("x.a: {}", x.a);
22    }
23
24    {
25        // Create x, which allows modifying the original data and checks for changes when
26        // it goes out of scope.
27        let mut x = data.as_tracked_mut();
28        x.a = 10;
29        println!("x.a: {}", x.a);
30        // When we leave this scope, changes are detected and sent to the listeners.
31    }
32
33}
Source

pub fn add_listener( &mut self, key: K, callback: Box<dyn OnChanged<T>>, ) -> Option<Box<dyn OnChanged<T>>>

Add a callback that will be called just after a data change is detected.

If a previous callback exists with the key, the original callback is returned as Some(original_callback). Otherwise, None is returned.

Examples found in repository?
examples/simple.rs (lines 13-16)
10fn main() {
11    let mut data = DataTracker::new(MyData { a: 1 });
12    let key = 0; // Keep the key to remove the callback later.
13    data.add_listener(key,
14                      Box::new(|old_value: &MyData, new_value: &MyData| {
15                          println!("changed {:?} -> {:?}", old_value, new_value);
16                      }));
17
18    {
19        // Create x, a (non-mutable) reference to original data.
20        let x = data.as_ref();
21        println!("x.a: {}", x.a);
22    }
23
24    {
25        // Create x, which allows modifying the original data and checks for changes when
26        // it goes out of scope.
27        let mut x = data.as_tracked_mut();
28        x.a = 10;
29        println!("x.a: {}", x.a);
30        // When we leave this scope, changes are detected and sent to the listeners.
31    }
32
33}
Source

pub fn remove_listener(&mut self, key: &K) -> Option<Box<dyn OnChanged<T>>>

Remove callback.

If a callback exists with the key, it is removed and returned as Some(callback). Otherwise, None is returned.

Source

pub fn as_tracked_mut(&mut self) -> Modifier<'_, T, K>

Return a Modifier which can be used to modify the owned data.

Examples found in repository?
examples/simple.rs (line 27)
10fn main() {
11    let mut data = DataTracker::new(MyData { a: 1 });
12    let key = 0; // Keep the key to remove the callback later.
13    data.add_listener(key,
14                      Box::new(|old_value: &MyData, new_value: &MyData| {
15                          println!("changed {:?} -> {:?}", old_value, new_value);
16                      }));
17
18    {
19        // Create x, a (non-mutable) reference to original data.
20        let x = data.as_ref();
21        println!("x.a: {}", x.a);
22    }
23
24    {
25        // Create x, which allows modifying the original data and checks for changes when
26        // it goes out of scope.
27        let mut x = data.as_tracked_mut();
28        x.a = 10;
29        println!("x.a: {}", x.a);
30        // When we leave this scope, changes are detected and sent to the listeners.
31    }
32
33}

Trait Implementations§

Source§

impl<T, K> AsRef<T> for DataTracker<T, K>
where T: Clone + PartialEq, K: Hash + Eq,

Source§

fn as_ref(&self) -> &T

Converts this type into a shared reference of the (usually inferred) input type.

Auto Trait Implementations§

§

impl<T, K> Freeze for DataTracker<T, K>
where T: Freeze,

§

impl<T, K> !RefUnwindSafe for DataTracker<T, K>

§

impl<T, K> Send for DataTracker<T, K>
where T: Send, K: Send,

§

impl<T, K> !Sync for DataTracker<T, K>

§

impl<T, K> Unpin for DataTracker<T, K>
where T: Unpin, K: Unpin,

§

impl<T, K> !UnwindSafe for DataTracker<T, K>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.