drying_paint

Crates.iodrying_paint
lib.rsdrying_paint
version0.5.6
created_at2019-11-30 22:54:48.649158+00
updated_at2025-11-28 19:34:33.11615+00
descriptionImplementation of observer pattern for Rust
homepagehttps://crates.io/crates/drying_paint
repositoryhttps://github.com/geeklint/drying_paint
max_upload_size
id185616
size100,726
Violet Leonard (geeklint)

documentation

README

crates.io docs.rs Build Status License

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.

Example

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);
Commit count: 144

cargo fmt