-
-
Notifications
You must be signed in to change notification settings - Fork 682
/
patch.rs
244 lines (229 loc) · 6.41 KB
/
patch.rs
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
use crate::{path::StorePath, StoreField};
use itertools::{EitherOrBoth, Itertools};
use reactive_graph::traits::{Notify, UntrackableGuard};
use std::{
borrow::Cow,
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6},
num::{
NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8,
NonZeroIsize, NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64,
NonZeroU8, NonZeroUsize,
},
rc::Rc,
sync::Arc,
};
/// Allows updating a store or field in place with a new value.
pub trait Patch {
/// The type of the new value.
type Value;
/// Patches a store or field with a new value, only notifying fields that have changed.
fn patch(&self, new: Self::Value);
}
impl<T> Patch for T
where
T: StoreField,
T::Value: PatchField,
{
type Value = T::Value;
fn patch(&self, new: Self::Value) {
let path = self.path().into_iter().collect::<StorePath>();
if let Some(mut writer) = self.writer() {
// don't track the writer for the whole store
writer.untrack();
let mut notify = |path: &StorePath| {
self.get_trigger(path.to_owned()).this.notify();
self.get_trigger(path.to_owned()).children.notify();
};
writer.patch_field(new, &path, &mut notify);
}
}
}
/// Allows patching a store field with some new value.
pub trait PatchField {
/// Patches the field with some new value, only notifying if the value has changed.
fn patch_field(
&mut self,
new: Self,
path: &StorePath,
notify: &mut dyn FnMut(&StorePath),
);
}
macro_rules! patch_primitives {
($($ty:ty),*) => {
$(impl PatchField for $ty {
fn patch_field(
&mut self,
new: Self,
path: &StorePath,
notify: &mut dyn FnMut(&StorePath),
) {
if new != *self {
*self = new;
notify(path);
}
}
})*
};
}
patch_primitives! {
&str,
String,
Arc<str>,
Rc<str>,
Cow<'_, str>,
u8,
u16,
u32,
u64,
u128,
isize,
i8,
i16,
i32,
i64,
i128,
f32,
f64,
char,
bool,
IpAddr,
SocketAddr,
SocketAddrV4,
SocketAddrV6,
Ipv4Addr,
Ipv6Addr,
NonZeroI8,
NonZeroU8,
NonZeroI16,
NonZeroU16,
NonZeroI32,
NonZeroU32,
NonZeroI64,
NonZeroU64,
NonZeroI128,
NonZeroU128,
NonZeroIsize,
NonZeroUsize
}
impl<T> PatchField for Vec<T>
where
T: PatchField,
{
fn patch_field(
&mut self,
new: Self,
path: &StorePath,
notify: &mut dyn FnMut(&StorePath),
) {
if self.is_empty() && new.is_empty() {
return;
}
if new.is_empty() {
self.clear();
notify(path);
} else if self.is_empty() {
self.extend(new);
notify(path);
} else {
let mut adds = vec![];
let mut removes_at_end = 0;
let mut new_path = path.to_owned();
new_path.push(0);
for (idx, item) in
new.into_iter().zip_longest(self.iter_mut()).enumerate()
{
match item {
EitherOrBoth::Both(new, old) => {
old.patch_field(new, &new_path, notify);
}
EitherOrBoth::Left(new) => {
adds.push(new);
}
EitherOrBoth::Right(_) => {
removes_at_end += 1;
}
}
new_path.replace_last(idx + 1);
}
let length_changed = removes_at_end > 0 || !adds.is_empty();
self.truncate(self.len() - removes_at_end);
self.append(&mut adds);
if length_changed {
notify(path);
}
}
}
}
macro_rules! patch_tuple {
($($ty:ident),*) => {
impl<$($ty),*> PatchField for ($($ty,)*)
where
$($ty: PatchField),*,
{
fn patch_field(
&mut self,
new: Self,
path: &StorePath,
notify: &mut dyn FnMut(&StorePath),
) {
let mut idx = 0;
let mut new_path = path.to_owned();
new_path.push(0);
paste::paste! {
#[allow(non_snake_case)]
let ($($ty,)*) = self;
let ($([<new_ $ty:lower>],)*) = new;
$(
$ty.patch_field([<new_ $ty:lower>], &new_path, notify);
idx += 1;
new_path.replace_last(idx);
)*
}
}
}
}
}
impl PatchField for () {
fn patch_field(
&mut self,
_new: Self,
_path: &StorePath,
_notify: &mut dyn FnMut(&StorePath),
) {
}
}
patch_tuple!(A);
patch_tuple!(A, B);
patch_tuple!(A, B, C);
patch_tuple!(A, B, C, D);
patch_tuple!(A, B, C, D, E);
patch_tuple!(A, B, C, D, E, F);
patch_tuple!(A, B, C, D, E, F, G);
patch_tuple!(A, B, C, D, E, F, G, H);
patch_tuple!(A, B, C, D, E, F, G, H, I);
patch_tuple!(A, B, C, D, E, F, G, H, I, J);
patch_tuple!(A, B, C, D, E, F, G, H, I, J, K);
patch_tuple!(A, B, C, D, E, F, G, H, I, J, K, L);
patch_tuple!(A, B, C, D, E, F, G, H, I, J, K, L, M);
patch_tuple!(A, B, C, D, E, F, G, H, I, J, K, L, M, N);
patch_tuple!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O);
patch_tuple!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P);
patch_tuple!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q);
patch_tuple!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R);
patch_tuple!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S);
patch_tuple!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T);
patch_tuple!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U);
patch_tuple!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V);
patch_tuple!(
A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W
);
patch_tuple!(
A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X
);
patch_tuple!(
A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y
);
patch_tuple!(
A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y,
Z
);