5 releases
0.2.2 | Feb 11, 2022 |
---|---|
0.2.1 | Feb 11, 2022 |
0.2.0 | Feb 11, 2022 |
0.1.1 | Feb 9, 2022 |
0.1.0 | Feb 9, 2022 |
#1628 in Data structures
4KB
Simple Observable
This simple library is made to notify you when your data changes
Adding a dependency: simple-observable = "0.2.2"
Changing observed values
use simple_observable::Observable;
fn main() {
let a = 1;
println!("Initial value: {}", a);
let mut obs1 = Observable::new(a);
obs1.add_listener(listener1);
obs1.add_listener(listener2);
obs1.add_listener(listener3);
obs1.change(my_change_function);
}
fn my_change_function(num: &mut i32) {
*num += 1;
}
fn listener1(num: &i32) {
println!("(listener1) New value after change: {}", num);
}
fn listener2(num: &i32) {
println!("(listener2) New value after change: {}", num);
}
fn listener3(num: &i32) {
println!("(listener3) New value after change: {}", num);
}
Observable
is a pointer that can be dereferenced and the data can be accessed directly. However, the data cannot be changed, but only read
It is important to understand that the Observable
owns the data and the only way to change the data is to call the change()
method.