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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use std::fmt::{Debug, Display};

use crate::Event;

pub struct Property<T> {
    data:       T,
    pub on_set: Event,
    pub on_get: Event,
}

impl<T> Property<T> {
    pub fn set(&mut self, value: T) {
        self.data = value;
        self.on_set.trigger(());
    }

    pub fn get(&mut self) -> &mut T {
        self.on_get.trigger(());
        &mut self.data
    }
}

impl<T: Copy> Property<T> {
    pub fn copy(&self) -> T {
        self.data
    }
}

impl<T> From<T> for Property<T> {
    fn from(data: T) -> Self {
        Self {
            data,
            on_set: Default::default(),
            on_get: Default::default(),
        }
    }
}

impl<T: Debug> Debug for Property<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.data.fmt(f)
    }
}

impl<T: Default> Default for Property<T> {
    fn default() -> Self {
        Self {
            data:   T::default(),
            on_set: Default::default(),
            on_get: Default::default(),
        }
    }
}

impl<T: Display> Display for Property<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.data.fmt(f)
    }
}