Implementation of the observer software design pattern (also known as event emitter) for rust.
See the documentation for details.
struct Model {
counter: usize,
}
impl Model {
fn increment(&mut self) {
self.counter += 1;
}
}
let model = Model { counter: 42 };
// create a subject that can be observed
let mut subject = Subject::new(model);
// add a function that is informed about changes
subject.register(|v: &mut Model| println!("new counter value: {}", v.counter));
subject.get_mut_notify().increment();