1use std::ops::Deref;
2
3use futures_signals::signal_vec::{MutableVec, MutableVecLockMut};
4
5pub struct Entry<'a, V> {
6 key: Option<usize>,
7 lock: MutableVecLockMut<'a, V>,
8}
9
10impl<'a, V: Copy> Entry<'a, V> {
11 fn existing(self, key: usize) -> Value<'a, V> {
12 let value = self.lock.get(key).copied().unwrap();
13 Value::existing(self, value)
14 }
15
16 pub fn is_vacant(&self) -> bool {
17 self.key.is_none()
18 }
19
20 pub fn is_occupied(&self) -> bool {
21 self.key.is_some()
22 }
23
24 pub fn key(&self) -> Option<usize> {
25 self.key
26 }
27
28 pub fn value(self) -> Option<Value<'a, V>> {
29 self.key.map(|key| self.existing(key))
30 }
31
32 pub fn or_insert(self, value: V) -> Value<'a, V> {
33 match self.key {
34 Some(key) => self.existing(key),
35 None => Value::new(self, value),
36 }
37 }
38
39 pub fn or_insert_with<F: FnOnce() -> V>(self, value: F) -> Value<'a, V> {
40 self.or_insert(value())
41 }
42
43 pub fn and_modify<F>(self, f: F) -> Self
44 where
45 F: FnOnce(&mut Value<'a, V>),
46 {
47 match self.key {
48 Some(key) => {
49 let mut existing = self.existing(key);
50 f(&mut existing);
51 existing.commit()
52 }
53 None => self,
54 }
55 }
56
57 pub fn and_set(mut self, value: V) -> Self {
58 match self.key {
59 Some(key) => {
60 self.lock.set(key, value);
61 self
62 }
63 None => self,
64 }
65 }
66
67 pub fn or_insert_entry(mut self, value: V) -> Self {
68 self.set(value);
69 self
70 }
71
72 fn set(&mut self, value: V) {
73 match self.key {
74 Some(index) => {
75 self.lock.set(index, value);
76 }
77 None => {
78 let index = self.lock.len();
79 self.lock.push(value);
80 self.key = Some(index);
81 }
82 }
83 }
84
85 pub fn remove(mut self) -> Option<V> {
86 self.key.map(|key| self.lock.remove(key))
87 }
88}
89
90impl<'a, V: Copy + Default> Entry<'a, V> {
91 pub fn or_default(self) -> Value<'a, V> {
92 self.or_insert(V::default())
93 }
94}
95
96pub struct Value<'a, V: Copy> {
97 entry: Option<Entry<'a, V>>,
98 value: V,
99 modified: bool,
100}
101
102impl<'a, V: Copy> Value<'a, V> {
103 fn new(entry: Entry<'a, V>, value: V) -> Self {
104 Self {
105 entry: Some(entry),
106 value,
107 modified: true,
108 }
109 }
110
111 fn existing(entry: Entry<'a, V>, value: V) -> Self {
112 Self {
113 entry: Some(entry),
114 value,
115 modified: false,
116 }
117 }
118
119 pub fn inspect_mut<F>(&mut self, f: F) -> bool
120 where
121 F: FnOnce(&mut V) -> bool,
122 {
123 self.modified = f(&mut self.value);
124 self.modified
125 }
126
127 pub fn set(&mut self, value: V) {
128 self.value = value;
129 self.modified = true;
130 }
131
132 pub fn set_neq(&mut self, value: V)
133 where
134 V: PartialEq,
135 {
136 if self.value != value {
137 self.value = value;
138 self.modified = true;
139 }
140 }
141
142 pub fn modified(self) -> bool {
143 self.modified
144 }
145
146 fn commit(mut self) -> Entry<'a, V> {
147 let mut entry = self.entry.take().unwrap();
148 if self.modified {
149 entry.set(self.value);
150 }
151 entry
152 }
153}
154
155impl<V: Copy> Deref for Value<'_, V> {
156 type Target = V;
157
158 fn deref(&self) -> &V {
159 &self.value
160 }
161}
162
163impl<V: Copy> Drop for Value<'_, V> {
164 fn drop(&mut self) {
165 if self.modified
166 && let Some(mut entry) = self.entry.take()
167 {
168 entry.set(self.value);
169 }
170 }
171}
172
173pub struct EntryCloned<'a, V> {
174 key: Option<usize>,
175 lock: MutableVecLockMut<'a, V>,
176}
177
178impl<'a, V: Clone> EntryCloned<'a, V> {
179 fn existing(self, key: usize) -> ValueCloned<'a, V> {
180 let value = self.lock.get(key).cloned().unwrap();
181 ValueCloned::existing(self, value)
182 }
183
184 pub fn is_vacant(&self) -> bool {
185 self.key.is_none()
186 }
187
188 pub fn is_occupied(&self) -> bool {
189 self.key.is_some()
190 }
191
192 pub fn key(&self) -> Option<usize> {
193 self.key
194 }
195
196 pub fn value(self) -> Option<ValueCloned<'a, V>> {
197 self.key.map(|key| self.existing(key))
198 }
199
200 pub fn or_insert(self, value: V) -> ValueCloned<'a, V> {
201 match self.key {
202 Some(key) => self.existing(key),
203 None => ValueCloned::new(self, value),
204 }
205 }
206
207 pub fn or_insert_with<F: FnOnce() -> V>(self, value: F) -> ValueCloned<'a, V> {
208 self.or_insert(value())
209 }
210
211 pub fn and_modify<F>(self, f: F) -> Self
212 where
213 F: FnOnce(&mut ValueCloned<'a, V>),
214 {
215 match self.key {
216 Some(key) => {
217 let mut existing = self.existing(key);
218 f(&mut existing);
219 existing.commit()
220 }
221 None => self,
222 }
223 }
224
225 pub fn and_set(mut self, value: V) -> Self {
226 match self.key {
227 Some(key) => {
228 self.lock.set_cloned(key, value);
229 self
230 }
231 None => self,
232 }
233 }
234
235 pub fn or_insert_entry(mut self, value: V) -> Self {
236 self.set(value);
237 self
238 }
239
240 fn set(&mut self, value: V) {
241 match self.key {
242 Some(index) => {
243 self.lock.set_cloned(index, value);
244 }
245 None => {
246 let index = self.lock.len();
247 self.lock.push_cloned(value);
248 self.key = Some(index);
249 }
250 }
251 }
252
253 pub fn remove(mut self) -> Option<V> {
254 self.key.map(|key| self.lock.remove(key))
255 }
256}
257
258impl<'a, V: Clone + Default> EntryCloned<'a, V> {
259 pub fn or_default(self) -> ValueCloned<'a, V> {
260 self.or_insert(V::default())
261 }
262}
263
264pub struct ValueCloned<'a, V: Clone> {
265 entry: Option<EntryCloned<'a, V>>,
266 value: V,
267 modified: bool,
268}
269
270impl<'a, V: Clone> ValueCloned<'a, V> {
271 fn new(entry: EntryCloned<'a, V>, value: V) -> Self {
272 Self {
273 entry: Some(entry),
274 value,
275 modified: true,
276 }
277 }
278
279 fn existing(entry: EntryCloned<'a, V>, value: V) -> Self {
280 Self {
281 entry: Some(entry),
282 value,
283 modified: false,
284 }
285 }
286
287 pub fn inspect_mut<F>(&mut self, f: F) -> bool
288 where
289 F: FnOnce(&mut V) -> bool,
290 {
291 self.modified = f(&mut self.value);
292 self.modified
293 }
294
295 pub fn set(&mut self, value: V) {
296 self.value = value;
297 self.modified = true;
298 }
299
300 pub fn set_neq(&mut self, value: V)
301 where
302 V: PartialEq,
303 {
304 if self.value != value {
305 self.value = value;
306 self.modified = true;
307 }
308 }
309
310 pub fn modified(self) -> bool {
311 self.modified
312 }
313
314 fn commit(mut self) -> EntryCloned<'a, V> {
315 let mut entry = self.entry.take().unwrap();
316 if self.modified {
317 entry.set(self.value.clone());
318 }
319 entry
320 }
321}
322
323impl<V: Clone> Deref for ValueCloned<'_, V> {
324 type Target = V;
325
326 fn deref(&self) -> &V {
327 &self.value
328 }
329}
330
331impl<V: Clone> Drop for ValueCloned<'_, V> {
332 fn drop(&mut self) {
333 if self.modified
334 && let Some(mut entry) = self.entry.take()
335 {
336 entry.set(self.value.clone());
337 }
338 }
339}
340
341pub trait MutableVecEntry<V> {
342 fn entry<F>(&self, f: F) -> Entry<V>
343 where
344 F: FnMut(&V) -> bool;
345
346 fn entry_cloned<F>(&self, f: F) -> EntryCloned<V>
347 where
348 F: FnMut(&V) -> bool;
349}
350
351impl<V> MutableVecEntry<V> for MutableVec<V> {
352 fn entry<F>(&self, f: F) -> Entry<V>
353 where
354 F: FnMut(&V) -> bool,
355 {
356 let lock = self.lock_mut();
357 let key = lock.iter().position(f);
358 Entry { key, lock }
359 }
360
361 fn entry_cloned<F>(&self, f: F) -> EntryCloned<V>
362 where
363 F: FnMut(&V) -> bool,
364 {
365 let lock = self.lock_mut();
366 let key = lock.iter().position(f);
367 EntryCloned { key, lock }
368 }
369}