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