1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use setting_tracker::Setting;
use std::cell::Cell;

#[derive(Default, Debug)]
struct Settings {
    str: Setting<String>,
    cell: Setting<Cell<u16>>,
    vec: Setting<Vec<u8>>,
}

fn main() {
    let mut settings = Settings::default();

    settings.cell.cb(|o, n| println!("{:?} -> {:?}", o, n));
    settings.vec.cb(|o, n| println!("{:?} -> {:?}", o, n));

    // Field name should be empty by default
    println!("name should be empty by default: {:?}", settings.str.get());

    // Change Settings struct, user callbacks should be called if they were are assigned
    settings.str.set("new_name".to_string());
    settings.cell.set(10.into());
    settings.cell.set(2222.into());

    // Changing Vec elements is a bit more tedious
    let mut vec = settings.vec.get();
    vec.extend([1, 2, 3, 4]);
    settings.vec.set(vec);

    println!("{:?}", settings);
}

/*
Output:
    name should be empty by default: ""
    Cell { value: 0 } -> Cell { value: 10 }
    Cell { value: 10 } -> Cell { value: 2222 }
    [] -> [1, 2, 3, 4]
    Settings { str: "new_name", cell: Cell { value: 2222 }, vec: [1, 2, 3, 4] }
 */