Crate drying_paint
source ·Expand description
The name ‘drying_paint’ comes from the expression “watching paint dry”. This module provides a system to “watch” some values for changes and run code whenever they change.
use std::{rc::Rc, cell::RefCell};
use drying_paint::{Watcher, Watched, WatcherInit, WatchContext};
// define a type to hold data
struct Content {
dest: i32,
source: Watched<i32>,
}
// define Watcher trait for the type
impl Watcher<'static> for Content {
fn init(mut init: impl WatcherInit<'static, Self>) {
// set up a callback that will be re-run when
// the Watched data changes
init.watch(|root| {
root.dest = *root.source;
});
}
}
// instantiate the content
let content = Rc::new(RefCell::new(Content {
dest: 0,
source: Watched::new(37),
}));
let weak = Rc::downgrade(&content);
// create the Context
let mut ctx = WatchContext::new();
// dest was 0 when instantiated
assert_eq!(content.borrow().dest, 0);
// after adding the watcher, the callback has run (once)
ctx.add_watcher(&weak);
assert_eq!(content.borrow().dest, 37);
// we can change the "watched" value
*content.borrow_mut().source = 43;
assert_eq!(content.borrow().dest, 37);
// and it will be updated when we call
// update on the context
ctx.update();
assert_eq!(content.borrow().dest, 43);
Structs
- A type which can be used from another thread to trigger watch functions watching an AtomicWatchedMeta.
- SyncWatchedMeta is like WatchedMeta, however allows you to create a trigger which may be sent to other threads.
- This represents some value which will be interesting to watch. Watcher functions that reference this value will be re-run when this value changes.
- A Watched value which provides interior mutability. This provides correct behavior (triggering watch functions when changed) where
Watched<Cell<T>>
would not, and should be slightly more performant thanRefCell<Watched<T>>
. - A Watched value which provides interior mutability. This provides correct behavior (triggering watch functions when changed) where
Watched<Cell<T>>
would not, and should be slightly more performant thanRefCell<Watched<T>>
. - This represents some value which will be interesting to watch. Watcher functions that reference this value will be re-run when this value changes.
- This provides the basic functionality behind watched values. You can use it to provide functionality using the watch system for cases where Watched and WatchedEvent are not appropriate.
- The sender half of a watched channel.