pub struct DataTracker<T, K>{ /* 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>
impl<T, K> DataTracker<T, K>
Sourcepub fn new(value: T) -> DataTracker<T, K>
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}
Sourcepub fn add_listener(
&mut self,
key: K,
callback: Box<dyn OnChanged<T>>,
) -> Option<Box<dyn OnChanged<T>>>
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}
Sourcepub fn remove_listener(&mut self, key: &K) -> Option<Box<dyn OnChanged<T>>>
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.
Sourcepub fn as_tracked_mut(&mut self) -> Modifier<'_, T, K>
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§
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>
impl<T, K> !Sync for DataTracker<T, K>
impl<T, K> Unpin for DataTracker<T, K>
impl<T, K> !UnwindSafe for DataTracker<T, K>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more