[−][src]Crate drying_paint
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.
The typical usage is as follows: you first define a structure to hold data, including some "watched" data.
#[derive(Default)] struct HelloData { name: Watched<String>, greeting: String, }
Implementing the trait WatcherInit for that structure gives you an place to set-up the code that should run when a watched value changes.
impl WatcherInit for HelloData { fn init(watcher: &mut WatcherMeta<Self>) { watcher.watch(|root| { root.greeting = format!("Hello, {}!", root.name); }); } }
Normally you need to wrap the data struct in a Watcher, so it's common to alias the watcher type to cleanup the syntax a bit:
type Hello = Watcher<HelloData>;
Creating watchers and setting watched data needs to happen within a WatchContext. WatchContext::update_current() will cause all the pending watcher code to run. WatchContext::allow_watcher_access() is used to create and access the Watcher, This is required in order to comply with aliasing rules.
fn main() { let mut ctx = WatchContext::new(); ctx = ctx.with(|| { let obj = WatchContext::allow_watcher_access((), |()| { let mut obj = Hello::new(); *obj.data_mut().name = "Rust".to_string(); obj }); WatchContext::update_current(); let obj = WatchContext::allow_watcher_access(obj, |obj| { assert_eq!(obj.data().greeting, "Hello, Rust!"); }); }).0; }
Structs
WatchContext | Most of the functions in this crate require that they are executing in a context. The context keeps track of some "global" state which enables the functionality in this crate. |
Watched | This represents some value which will be interesting to watch. Watcher functions that reference this value will be re-run when this value changes. |
WatchedCell | A Watched value which provides interior mutability. This provides correct
behavior (triggering watch functions when changed) where |
WatchedEvent | A WatchedEvent uses the watch system provided by this crate to implement an event disptacher. This is different from a watched value (Watched) in that events will fire for each value passed to WatchedEvent::dispatch() and will not "store" the data. |
WatchedMeta | 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. |
Watcher | Watcher is a structure designed to hold some data along with associated functions which will run when watched data changes. |
WatcherId | A type representing a unique id for a particular instance of a watcher. This value may outlive the watcher, and will never compare equal to a value returned by the id method of a Watcher other than this one. |
WatcherMeta | This structure is used internally by Watcher |
Traits
WatcherInit | This trait is required to be implemented by the data stored in Watchers. It provides a convient point to register watching functions. |