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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use super::traits::*;
use super::releasable::*;
use super::binding_context::*;
use std::sync::*;
struct BoundValue<Value> {
value: Value,
when_changed: Vec<ReleasableNotifiable>
}
impl<Value: Clone+PartialEq> BoundValue<Value> {
pub fn new(val: Value) -> BoundValue<Value> {
BoundValue {
value: val,
when_changed: vec![]
}
}
pub fn set_without_notifying(&mut self, new_value: Value) -> bool {
let changed = self.value != new_value;
self.value = new_value;
changed
}
pub fn get_notifiable_items(&self) -> Vec<ReleasableNotifiable> {
self.when_changed
.iter()
.map(|item| item.clone_for_inspection())
.collect()
}
pub fn filter_unused_notifications(&mut self) {
self.when_changed.retain(|releasable| releasable.is_in_use());
}
fn get(&self) -> Value {
self.value.clone()
}
fn when_changed(&mut self, what: Arc<dyn Notifiable>) -> Box<dyn Releasable> {
let releasable = ReleasableNotifiable::new(what);
self.when_changed.push(releasable.clone_as_owned());
self.filter_unused_notifications();
Box::new(releasable)
}
}
#[derive(Clone)]
pub struct Binding<Value> {
value: Arc<Mutex<BoundValue<Value>>>
}
impl<Value: Clone+PartialEq> Binding<Value> {
pub fn new(value: Value) -> Binding<Value> {
Binding {
value: Arc::new(Mutex::new(BoundValue::new(value)))
}
}
}
impl<Value: 'static+Clone+PartialEq+Send> Changeable for Binding<Value> {
fn when_changed(&self, what: Arc<dyn Notifiable>) -> Box<dyn Releasable> {
self.value.lock().unwrap().when_changed(what)
}
}
impl<Value: 'static+Clone+PartialEq+Send> Bound<Value> for Binding<Value> {
fn get(&self) -> Value {
BindingContext::add_dependency(self.clone());
self.value.lock().unwrap().get()
}
}
impl<Value: 'static+Clone+PartialEq+Send> MutableBound<Value> for Binding<Value> {
fn set(&self, new_value: Value) {
let notifications = {
let mut cell = self.value.lock().unwrap();
let changed = cell.set_without_notifying(new_value);
if changed {
cell.get_notifiable_items()
} else {
vec![]
}
};
let mut needs_filtering = false;
for to_notify in notifications {
needs_filtering = !to_notify.mark_as_changed() || needs_filtering;
}
if needs_filtering {
let mut cell = self.value.lock().unwrap();
cell.filter_unused_notifications();
}
}
}
impl<Value: 'static+Clone+PartialEq+Send> From<Value> for Binding<Value> {
#[inline]
fn from(val: Value) -> Binding<Value> {
Binding::new(val)
}
}
impl<'a, Value: 'static+Clone+PartialEq+Send> From<&'a Binding<Value>> for Binding<Value> {
#[inline]
fn from(val: &'a Binding<Value>) -> Binding<Value> {
Binding::clone(val)
}
}
impl<'a, Value: 'static+Clone+PartialEq+Send> From<&'a Value> for Binding<Value> {
#[inline]
fn from(val: &'a Value) -> Binding<Value> {
Binding::new(val.clone())
}
}