Skip to main content

futures_signals_ext/
entry.rs

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