1use std::sync::{Arc, Mutex, RwLock};
6use std::marker::PhantomData;
7use std::any::{Any, TypeId};
8use std::rc::Rc;
9use std::cell::RefCell;
10use std::fmt;
12
13pub struct ArcMutexKeyPathChain<Root, MutexValue, InnerValue, SubValue, F, G>
26where
27 F: for<'r> Fn(&'r Root) -> &'r MutexValue,
28 G: for<'r> Fn(&'r InnerValue) -> &'r SubValue,
29{
30 outer_keypath: KeyPath<Root, MutexValue, F>,
31 inner_keypath: KeyPath<InnerValue, SubValue, G>,
32}
33
34impl<Root, MutexValue, InnerValue, SubValue, F, G> ArcMutexKeyPathChain<Root, MutexValue, InnerValue, SubValue, F, G>
35where
36 F: for<'r> Fn(&'r Root) -> &'r MutexValue,
37 G: for<'r> Fn(&'r InnerValue) -> &'r SubValue,
38 MutexValue: std::borrow::Borrow<Arc<Mutex<InnerValue>>>,
39{
40 pub fn get<Callback>(self, container: &Root, callback: Callback) -> Option<()>
43 where
44 Callback: FnOnce(&SubValue) -> (),
45 {
46 let arc_mutex_ref = self.outer_keypath.get(container);
47 arc_mutex_ref.borrow().lock().ok().map(|guard| {
48 let value = self.inner_keypath.get(&*guard);
49 callback(value)
50 })
51 }
52
53 pub fn then<NextValue, H>(
55 self,
56 next: KeyPath<SubValue, NextValue, H>,
57 ) -> ArcMutexKeyPathChain<Root, MutexValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r InnerValue) -> &'r NextValue + 'static>
58 where
59 H: for<'r> Fn(&'r SubValue) -> &'r NextValue,
60 G: 'static,
61 H: 'static,
62 InnerValue: 'static,
63 SubValue: 'static,
64 NextValue: 'static,
65 {
66 let first = self.inner_keypath;
67 let second = next;
68
69 let composed = KeyPath::new(move |inner: &InnerValue| {
70 let sub = first.get(inner);
71 second.get(sub)
72 });
73
74 ArcMutexKeyPathChain {
75 outer_keypath: self.outer_keypath,
76 inner_keypath: composed,
77 }
78 }
79
80 pub fn chain_optional<NextValue, H>(
82 self,
83 next: OptionalKeyPath<SubValue, NextValue, H>,
84 ) -> ArcMutexOptionalKeyPathChain<Root, MutexValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r InnerValue) -> Option<&'r NextValue> + 'static>
85 where
86 H: for<'r> Fn(&'r SubValue) -> Option<&'r NextValue>,
87 G: 'static,
88 H: 'static,
89 InnerValue: 'static,
90 SubValue: 'static,
91 NextValue: 'static,
92 {
93 let first = self.inner_keypath;
94 let second = next;
95
96 let composed = OptionalKeyPath::new(move |inner: &InnerValue| {
97 let sub = first.get(inner);
98 second.get(sub)
99 });
100
101 ArcMutexOptionalKeyPathChain {
102 outer_keypath: self.outer_keypath,
103 inner_keypath: composed,
104 }
105 }
106}
107
108pub struct ArcMutexWritableKeyPathChain<Root, MutexValue, InnerValue, SubValue, F, G>
113where
114 F: for<'r> Fn(&'r Root) -> &'r MutexValue,
115 G: for<'r> Fn(&'r mut InnerValue) -> &'r mut SubValue,
116{
117 outer_keypath: KeyPath<Root, MutexValue, F>,
118 inner_keypath: WritableKeyPath<InnerValue, SubValue, G>,
119}
120
121impl<Root, MutexValue, InnerValue, SubValue, F, G> ArcMutexWritableKeyPathChain<Root, MutexValue, InnerValue, SubValue, F, G>
122where
123 F: for<'r> Fn(&'r Root) -> &'r MutexValue,
124 G: for<'r> Fn(&'r mut InnerValue) -> &'r mut SubValue,
125 MutexValue: std::borrow::Borrow<Arc<Mutex<InnerValue>>>,
126{
127 pub fn get_mut<Callback, R>(self, container: &Root, callback: Callback) -> Option<R>
130 where
131 Callback: FnOnce(&mut SubValue) -> R,
132 {
133 let arc_mutex_ref = self.outer_keypath.get(container);
134 arc_mutex_ref.borrow().lock().ok().map(|mut guard| {
135 let value_ref = self.inner_keypath.get_mut(&mut *guard);
136 callback(value_ref)
137 })
138 }
139
140 pub fn then<NextValue, H>(
142 self,
143 next: WritableKeyPath<SubValue, NextValue, H>,
144 ) -> ArcMutexWritableKeyPathChain<Root, MutexValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r mut InnerValue) -> &'r mut NextValue + 'static>
145 where
146 H: for<'r> Fn(&'r mut SubValue) -> &'r mut NextValue,
147 G: 'static,
148 H: 'static,
149 InnerValue: 'static,
150 SubValue: 'static,
151 NextValue: 'static,
152 {
153 let first = self.inner_keypath;
154 let second = next;
155
156 let composed = WritableKeyPath::new(move |inner: &mut InnerValue| {
157 let sub = first.get_mut(inner);
158 second.get_mut(sub)
159 });
160
161 ArcMutexWritableKeyPathChain {
162 outer_keypath: self.outer_keypath,
163 inner_keypath: composed,
164 }
165 }
166
167 pub fn chain_optional<NextValue, H>(
169 self,
170 next: WritableOptionalKeyPath<SubValue, NextValue, H>,
171 ) -> ArcMutexWritableOptionalKeyPathChain<Root, MutexValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut NextValue> + 'static>
172 where
173 H: for<'r> Fn(&'r mut SubValue) -> Option<&'r mut NextValue>,
174 G: 'static,
175 H: 'static,
176 InnerValue: 'static,
177 SubValue: 'static,
178 NextValue: 'static,
179 {
180 let first = self.inner_keypath;
181 let second = next;
182
183 let composed = WritableOptionalKeyPath::new(move |inner: &mut InnerValue| {
184 let sub = first.get_mut(inner);
185 second.get_mut(sub)
186 });
187
188 ArcMutexWritableOptionalKeyPathChain {
189 outer_keypath: self.outer_keypath,
190 inner_keypath: composed,
191 }
192 }
193}
194
195pub struct ArcMutexWritableOptionalKeyPathChain<Root, MutexValue, InnerValue, SubValue, F, G>
197where
198 F: for<'r> Fn(&'r Root) -> &'r MutexValue,
199 G: for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut SubValue>,
200{
201 outer_keypath: KeyPath<Root, MutexValue, F>,
202 inner_keypath: WritableOptionalKeyPath<InnerValue, SubValue, G>,
203}
204
205impl<Root, MutexValue, InnerValue, SubValue, F, G> ArcMutexWritableOptionalKeyPathChain<Root, MutexValue, InnerValue, SubValue, F, G>
206where
207 F: for<'r> Fn(&'r Root) -> &'r MutexValue,
208 G: for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut SubValue>,
209 MutexValue: std::borrow::Borrow<Arc<Mutex<InnerValue>>>,
210{
211 pub fn get_mut<Callback, R>(self, container: &Root, callback: Callback) -> Option<R>
214 where
215 Callback: FnOnce(&mut SubValue) -> R,
216 {
217 let arc_mutex_ref = self.outer_keypath.get(container);
218 arc_mutex_ref.borrow().lock().ok().and_then(|mut guard| {
219 self.inner_keypath.get_mut(&mut *guard).map(|value_ref| callback(value_ref))
220 })
221 }
222
223 pub fn then<NextValue, H>(
225 self,
226 next: WritableKeyPath<SubValue, NextValue, H>,
227 ) -> ArcMutexWritableOptionalKeyPathChain<Root, MutexValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut NextValue> + 'static>
228 where
229 H: for<'r> Fn(&'r mut SubValue) -> &'r mut NextValue,
230 G: 'static,
231 H: 'static,
232 InnerValue: 'static,
233 SubValue: 'static,
234 NextValue: 'static,
235 {
236 let first = self.inner_keypath;
237 let second = next;
238
239 let composed = WritableOptionalKeyPath::new(move |inner: &mut InnerValue| {
240 first.get_mut(inner).map(|sub| second.get_mut(sub))
241 });
242
243 ArcMutexWritableOptionalKeyPathChain {
244 outer_keypath: self.outer_keypath,
245 inner_keypath: composed,
246 }
247 }
248
249 pub fn chain_optional<NextValue, H>(
251 self,
252 next: WritableOptionalKeyPath<SubValue, NextValue, H>,
253 ) -> ArcMutexWritableOptionalKeyPathChain<Root, MutexValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut NextValue> + 'static>
254 where
255 H: for<'r> Fn(&'r mut SubValue) -> Option<&'r mut NextValue>,
256 G: 'static,
257 H: 'static,
258 InnerValue: 'static,
259 SubValue: 'static,
260 NextValue: 'static,
261 {
262 let first = self.inner_keypath;
263 let second = next;
264
265 let composed = WritableOptionalKeyPath::new(move |inner: &mut InnerValue| {
266 first.get_mut(inner).and_then(|sub| second.get_mut(sub))
267 });
268
269 ArcMutexWritableOptionalKeyPathChain {
270 outer_keypath: self.outer_keypath,
271 inner_keypath: composed,
272 }
273 }
274}
275
276pub struct ArcMutexOptionalKeyPathChain<Root, MutexValue, InnerValue, SubValue, F, G>
278where
279 F: for<'r> Fn(&'r Root) -> &'r MutexValue,
280 G: for<'r> Fn(&'r InnerValue) -> Option<&'r SubValue>,
281{
282 outer_keypath: KeyPath<Root, MutexValue, F>,
283 inner_keypath: OptionalKeyPath<InnerValue, SubValue, G>,
284}
285
286impl<Root, MutexValue, InnerValue, SubValue, F, G> ArcMutexOptionalKeyPathChain<Root, MutexValue, InnerValue, SubValue, F, G>
287where
288 F: for<'r> Fn(&'r Root) -> &'r MutexValue,
289 G: for<'r> Fn(&'r InnerValue) -> Option<&'r SubValue>,
290 MutexValue: std::borrow::Borrow<Arc<Mutex<InnerValue>>>,
291{
292 pub fn get<Callback>(self, container: &Root, callback: Callback) -> Option<()>
295 where
296 Callback: FnOnce(&SubValue) -> (),
297 {
298 let arc_mutex_ref = self.outer_keypath.get(container);
299 arc_mutex_ref.borrow().lock().ok().and_then(|guard| {
300 self.inner_keypath.get(&*guard).map(|value| callback(value))
301 })
302 }
303
304 pub fn then<NextValue, H>(
306 self,
307 next: KeyPath<SubValue, NextValue, H>,
308 ) -> ArcMutexOptionalKeyPathChain<Root, MutexValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r InnerValue) -> Option<&'r NextValue> + 'static>
309 where
310 H: for<'r> Fn(&'r SubValue) -> &'r NextValue,
311 G: 'static,
312 H: 'static,
313 InnerValue: 'static,
314 SubValue: 'static,
315 NextValue: 'static,
316 {
317 let first = self.inner_keypath;
318 let second = next;
319
320 let composed = OptionalKeyPath::new(move |inner: &InnerValue| {
321 first.get(inner).map(|sub| second.get(sub))
322 });
323
324 ArcMutexOptionalKeyPathChain {
325 outer_keypath: self.outer_keypath,
326 inner_keypath: composed,
327 }
328 }
329
330 pub fn chain_optional<NextValue, H>(
332 self,
333 next: OptionalKeyPath<SubValue, NextValue, H>,
334 ) -> ArcMutexOptionalKeyPathChain<Root, MutexValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r InnerValue) -> Option<&'r NextValue> + 'static>
335 where
336 H: for<'r> Fn(&'r SubValue) -> Option<&'r NextValue>,
337 G: 'static,
338 H: 'static,
339 InnerValue: 'static,
340 SubValue: 'static,
341 NextValue: 'static,
342 {
343 let first = self.inner_keypath;
344 let second = next;
345
346 let composed = OptionalKeyPath::new(move |inner: &InnerValue| {
347 first.get(inner).and_then(|sub| second.get(sub))
348 });
349
350 ArcMutexOptionalKeyPathChain {
351 outer_keypath: self.outer_keypath,
352 inner_keypath: composed,
353 }
354 }
355}
356
357pub struct OptionalArcMutexKeyPathChain<Root, MutexValue, InnerValue, SubValue, F, G>
359where
360 F: for<'r> Fn(&'r Root) -> Option<&'r MutexValue>,
361 G: for<'r> Fn(&'r InnerValue) -> &'r SubValue,
362{
363 outer_keypath: OptionalKeyPath<Root, MutexValue, F>,
364 inner_keypath: KeyPath<InnerValue, SubValue, G>,
365}
366
367impl<Root, MutexValue, InnerValue, SubValue, F, G> OptionalArcMutexKeyPathChain<Root, MutexValue, InnerValue, SubValue, F, G>
368where
369 F: for<'r> Fn(&'r Root) -> Option<&'r MutexValue>,
370 G: for<'r> Fn(&'r InnerValue) -> &'r SubValue,
371 MutexValue: std::borrow::Borrow<Arc<Mutex<InnerValue>>>,
372{
373 pub fn get<Callback>(self, container: &Root, callback: Callback) -> Option<()>
376 where
377 Callback: FnOnce(&SubValue) -> (),
378 {
379 self.outer_keypath.get(container).and_then(|arc_mutex_ref| {
380 arc_mutex_ref.borrow().lock().ok().map(|guard| {
381 let value = self.inner_keypath.get(&*guard);
382 callback(value)
383 })
384 })
385 }
386
387 pub fn then<NextValue, H>(
389 self,
390 next: KeyPath<SubValue, NextValue, H>,
391 ) -> OptionalArcMutexKeyPathChain<Root, MutexValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r InnerValue) -> &'r NextValue + 'static>
392 where
393 H: for<'r> Fn(&'r SubValue) -> &'r NextValue,
394 G: 'static,
395 H: 'static,
396 InnerValue: 'static,
397 SubValue: 'static,
398 NextValue: 'static,
399 {
400 let first = self.inner_keypath;
401 let second = next;
402
403 let composed = KeyPath::new(move |inner: &InnerValue| {
404 let sub = first.get(inner);
405 second.get(sub)
406 });
407
408 OptionalArcMutexKeyPathChain {
409 outer_keypath: self.outer_keypath,
410 inner_keypath: composed,
411 }
412 }
413
414 pub fn chain_optional<NextValue, H>(
416 self,
417 next: OptionalKeyPath<SubValue, NextValue, H>,
418 ) -> OptionalArcMutexOptionalKeyPathChain<Root, MutexValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r InnerValue) -> Option<&'r NextValue> + 'static>
419 where
420 H: for<'r> Fn(&'r SubValue) -> Option<&'r NextValue>,
421 G: 'static,
422 H: 'static,
423 InnerValue: 'static,
424 SubValue: 'static,
425 NextValue: 'static,
426 {
427 let first = self.inner_keypath;
428 let second = next;
429
430 let composed = OptionalKeyPath::new(move |inner: &InnerValue| {
431 let sub = first.get(inner);
432 second.get(sub)
433 });
434
435 OptionalArcMutexOptionalKeyPathChain {
436 outer_keypath: self.outer_keypath,
437 inner_keypath: composed,
438 }
439 }
440}
441
442pub struct OptionalArcMutexOptionalKeyPathChain<Root, MutexValue, InnerValue, SubValue, F, G>
444where
445 F: for<'r> Fn(&'r Root) -> Option<&'r MutexValue>,
446 G: for<'r> Fn(&'r InnerValue) -> Option<&'r SubValue>,
447{
448 outer_keypath: OptionalKeyPath<Root, MutexValue, F>,
449 inner_keypath: OptionalKeyPath<InnerValue, SubValue, G>,
450}
451
452impl<Root, MutexValue, InnerValue, SubValue, F, G> OptionalArcMutexOptionalKeyPathChain<Root, MutexValue, InnerValue, SubValue, F, G>
453where
454 F: for<'r> Fn(&'r Root) -> Option<&'r MutexValue>,
455 G: for<'r> Fn(&'r InnerValue) -> Option<&'r SubValue>,
456 MutexValue: std::borrow::Borrow<Arc<Mutex<InnerValue>>>,
457{
458 pub fn get<Callback>(self, container: &Root, callback: Callback) -> Option<()>
461 where
462 Callback: FnOnce(&SubValue) -> (),
463 {
464 self.outer_keypath.get(container).and_then(|arc_mutex_ref| {
465 arc_mutex_ref.borrow().lock().ok().and_then(|guard| {
466 self.inner_keypath.get(&*guard).map(|value| callback(value))
467 })
468 })
469 }
470
471 pub fn then<NextValue, H>(
473 self,
474 next: KeyPath<SubValue, NextValue, H>,
475 ) -> OptionalArcMutexOptionalKeyPathChain<Root, MutexValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r InnerValue) -> Option<&'r NextValue> + 'static>
476 where
477 H: for<'r> Fn(&'r SubValue) -> &'r NextValue,
478 G: 'static,
479 H: 'static,
480 InnerValue: 'static,
481 SubValue: 'static,
482 NextValue: 'static,
483 {
484 let first = self.inner_keypath;
485 let second = next;
486
487 let composed = OptionalKeyPath::new(move |inner: &InnerValue| {
488 first.get(inner).map(|sub| second.get(sub))
489 });
490
491 OptionalArcMutexOptionalKeyPathChain {
492 outer_keypath: self.outer_keypath,
493 inner_keypath: composed,
494 }
495 }
496
497 pub fn chain_optional<NextValue, H>(
499 self,
500 next: OptionalKeyPath<SubValue, NextValue, H>,
501 ) -> OptionalArcMutexOptionalKeyPathChain<Root, MutexValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r InnerValue) -> Option<&'r NextValue> + 'static>
502 where
503 H: for<'r> Fn(&'r SubValue) -> Option<&'r NextValue>,
504 G: 'static,
505 H: 'static,
506 InnerValue: 'static,
507 SubValue: 'static,
508 NextValue: 'static,
509 {
510 let first = self.inner_keypath;
511 let second = next;
512
513 let composed = OptionalKeyPath::new(move |inner: &InnerValue| {
514 first.get(inner).and_then(|sub| second.get(sub))
515 });
516
517 OptionalArcMutexOptionalKeyPathChain {
518 outer_keypath: self.outer_keypath,
519 inner_keypath: composed,
520 }
521 }
522}
523
524pub struct ArcRwLockKeyPathChain<Root, RwLockValue, InnerValue, SubValue, F, G>
537where
538 F: for<'r> Fn(&'r Root) -> &'r RwLockValue,
539 G: for<'r> Fn(&'r InnerValue) -> &'r SubValue,
540{
541 outer_keypath: KeyPath<Root, RwLockValue, F>,
542 inner_keypath: KeyPath<InnerValue, SubValue, G>,
543}
544
545impl<Root, RwLockValue, InnerValue, SubValue, F, G> ArcRwLockKeyPathChain<Root, RwLockValue, InnerValue, SubValue, F, G>
546where
547 F: for<'r> Fn(&'r Root) -> &'r RwLockValue,
548 G: for<'r> Fn(&'r InnerValue) -> &'r SubValue,
549 RwLockValue: std::borrow::Borrow<Arc<RwLock<InnerValue>>>,
550{
551 pub fn get<Callback>(self, container: &Root, callback: Callback) -> Option<()>
554 where
555 Callback: FnOnce(&SubValue) -> (),
556 {
557 let arc_rwlock_ref = self.outer_keypath.get(container);
558 arc_rwlock_ref.borrow().read().ok().map(|guard| {
559 let value = self.inner_keypath.get(&*guard);
560 callback(value)
561 })
562 }
563
564 pub fn then<NextValue, H>(
567 self,
568 next: KeyPath<SubValue, NextValue, H>,
569 ) -> ArcRwLockKeyPathChain<Root, RwLockValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r InnerValue) -> &'r NextValue + 'static>
570 where
571 H: for<'r> Fn(&'r SubValue) -> &'r NextValue,
572 G: 'static,
573 H: 'static,
574 InnerValue: 'static,
575 SubValue: 'static,
576 NextValue: 'static,
577 {
578 let first = self.inner_keypath;
579 let second = next;
580
581 let composed = KeyPath::new(move |inner: &InnerValue| {
582 let sub = first.get(inner);
583 second.get(sub)
584 });
585
586 ArcRwLockKeyPathChain {
587 outer_keypath: self.outer_keypath,
588 inner_keypath: composed,
589 }
590 }
591
592 pub fn chain_optional<NextValue, H>(
594 self,
595 next: OptionalKeyPath<SubValue, NextValue, H>,
596 ) -> ArcRwLockOptionalKeyPathChain<Root, RwLockValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r InnerValue) -> Option<&'r NextValue> + 'static>
597 where
598 H: for<'r> Fn(&'r SubValue) -> Option<&'r NextValue>,
599 G: 'static,
600 H: 'static,
601 InnerValue: 'static,
602 SubValue: 'static,
603 NextValue: 'static,
604 {
605 let first = self.inner_keypath;
606 let second = next;
607
608 let composed = OptionalKeyPath::new(move |inner: &InnerValue| {
609 let sub = first.get(inner);
610 second.get(sub)
611 });
612
613 ArcRwLockOptionalKeyPathChain {
614 outer_keypath: self.outer_keypath,
615 inner_keypath: composed,
616 }
617 }
618}
619
620pub struct ArcRwLockOptionalKeyPathChain<Root, RwLockValue, InnerValue, SubValue, F, G>
622where
623 F: for<'r> Fn(&'r Root) -> &'r RwLockValue,
624 G: for<'r> Fn(&'r InnerValue) -> Option<&'r SubValue>,
625{
626 outer_keypath: KeyPath<Root, RwLockValue, F>,
627 inner_keypath: OptionalKeyPath<InnerValue, SubValue, G>,
628}
629
630impl<Root, RwLockValue, InnerValue, SubValue, F, G> ArcRwLockOptionalKeyPathChain<Root, RwLockValue, InnerValue, SubValue, F, G>
631where
632 F: for<'r> Fn(&'r Root) -> &'r RwLockValue,
633 G: for<'r> Fn(&'r InnerValue) -> Option<&'r SubValue>,
634 RwLockValue: std::borrow::Borrow<Arc<RwLock<InnerValue>>>,
635{
636 pub fn get<Callback>(self, container: &Root, callback: Callback) -> Option<()>
639 where
640 Callback: FnOnce(&SubValue) -> (),
641 {
642 let arc_rwlock_ref = self.outer_keypath.get(container);
643 arc_rwlock_ref.borrow().read().ok().and_then(|guard| {
644 self.inner_keypath.get(&*guard).map(|value| callback(value))
645 })
646 }
647}
648
649pub struct OptionalArcRwLockKeyPathChain<Root, RwLockValue, InnerValue, SubValue, F, G>
651where
652 F: for<'r> Fn(&'r Root) -> Option<&'r RwLockValue>,
653 G: for<'r> Fn(&'r InnerValue) -> &'r SubValue,
654{
655 outer_keypath: OptionalKeyPath<Root, RwLockValue, F>,
656 inner_keypath: KeyPath<InnerValue, SubValue, G>,
657}
658
659impl<Root, RwLockValue, InnerValue, SubValue, F, G> OptionalArcRwLockKeyPathChain<Root, RwLockValue, InnerValue, SubValue, F, G>
660where
661 F: for<'r> Fn(&'r Root) -> Option<&'r RwLockValue>,
662 G: for<'r> Fn(&'r InnerValue) -> &'r SubValue,
663 RwLockValue: std::borrow::Borrow<Arc<RwLock<InnerValue>>>,
664{
665 pub fn get<Callback>(self, container: &Root, callback: Callback) -> Option<()>
668 where
669 Callback: FnOnce(&SubValue) -> (),
670 {
671 self.outer_keypath.get(container).and_then(|arc_rwlock_ref| {
672 arc_rwlock_ref.borrow().read().ok().map(|guard| {
673 let value = self.inner_keypath.get(&*guard);
674 callback(value)
675 })
676 })
677 }
678
679 pub fn then<NextValue, H>(
681 self,
682 next: KeyPath<SubValue, NextValue, H>,
683 ) -> OptionalArcRwLockKeyPathChain<Root, RwLockValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r InnerValue) -> &'r NextValue + 'static>
684 where
685 H: for<'r> Fn(&'r SubValue) -> &'r NextValue,
686 G: 'static,
687 H: 'static,
688 InnerValue: 'static,
689 SubValue: 'static,
690 NextValue: 'static,
691 {
692 let first = self.inner_keypath;
693 let second = next;
694
695 let composed = KeyPath::new(move |inner: &InnerValue| {
696 let sub = first.get(inner);
697 second.get(sub)
698 });
699
700 OptionalArcRwLockKeyPathChain {
701 outer_keypath: self.outer_keypath,
702 inner_keypath: composed,
703 }
704 }
705
706 pub fn chain_optional<NextValue, H>(
708 self,
709 next: OptionalKeyPath<SubValue, NextValue, H>,
710 ) -> OptionalArcRwLockOptionalKeyPathChain<Root, RwLockValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r InnerValue) -> Option<&'r NextValue> + 'static>
711 where
712 H: for<'r> Fn(&'r SubValue) -> Option<&'r NextValue>,
713 G: 'static,
714 H: 'static,
715 InnerValue: 'static,
716 SubValue: 'static,
717 NextValue: 'static,
718 {
719 let first = self.inner_keypath;
720 let second = next;
721
722 let composed = OptionalKeyPath::new(move |inner: &InnerValue| {
723 let sub = first.get(inner);
724 second.get(sub)
725 });
726
727 OptionalArcRwLockOptionalKeyPathChain {
728 outer_keypath: self.outer_keypath,
729 inner_keypath: composed,
730 }
731 }
732}
733
734pub struct OptionalArcRwLockOptionalKeyPathChain<Root, RwLockValue, InnerValue, SubValue, F, G>
736where
737 F: for<'r> Fn(&'r Root) -> Option<&'r RwLockValue>,
738 G: for<'r> Fn(&'r InnerValue) -> Option<&'r SubValue>,
739{
740 outer_keypath: OptionalKeyPath<Root, RwLockValue, F>,
741 inner_keypath: OptionalKeyPath<InnerValue, SubValue, G>,
742}
743
744impl<Root, RwLockValue, InnerValue, SubValue, F, G> OptionalArcRwLockOptionalKeyPathChain<Root, RwLockValue, InnerValue, SubValue, F, G>
745where
746 F: for<'r> Fn(&'r Root) -> Option<&'r RwLockValue>,
747 G: for<'r> Fn(&'r InnerValue) -> Option<&'r SubValue>,
748 RwLockValue: std::borrow::Borrow<Arc<RwLock<InnerValue>>>,
749{
750 pub fn get<Callback>(self, container: &Root, callback: Callback) -> Option<()>
753 where
754 Callback: FnOnce(&SubValue) -> (),
755 {
756 self.outer_keypath.get(container).and_then(|arc_rwlock_ref| {
757 arc_rwlock_ref.borrow().read().ok().and_then(|guard| {
758 self.inner_keypath.get(&*guard).map(|value| callback(value))
759 })
760 })
761 }
762
763 pub fn then<NextValue, H>(
765 self,
766 next: KeyPath<SubValue, NextValue, H>,
767 ) -> OptionalArcRwLockOptionalKeyPathChain<Root, RwLockValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r InnerValue) -> Option<&'r NextValue> + 'static>
768 where
769 H: for<'r> Fn(&'r SubValue) -> &'r NextValue,
770 G: 'static,
771 H: 'static,
772 InnerValue: 'static,
773 SubValue: 'static,
774 NextValue: 'static,
775 {
776 let first = self.inner_keypath;
777 let second = next;
778
779 let composed = OptionalKeyPath::new(move |inner: &InnerValue| {
780 first.get(inner).map(|sub| second.get(sub))
781 });
782
783 OptionalArcRwLockOptionalKeyPathChain {
784 outer_keypath: self.outer_keypath,
785 inner_keypath: composed,
786 }
787 }
788
789 pub fn chain_optional<NextValue, H>(
791 self,
792 next: OptionalKeyPath<SubValue, NextValue, H>,
793 ) -> OptionalArcRwLockOptionalKeyPathChain<Root, RwLockValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r InnerValue) -> Option<&'r NextValue> + 'static>
794 where
795 H: for<'r> Fn(&'r SubValue) -> Option<&'r NextValue>,
796 G: 'static,
797 H: 'static,
798 InnerValue: 'static,
799 SubValue: 'static,
800 NextValue: 'static,
801 {
802 let first = self.inner_keypath;
803 let second = next;
804
805 let composed = OptionalKeyPath::new(move |inner: &InnerValue| {
806 first.get(inner).and_then(|sub| second.get(sub))
807 });
808
809 OptionalArcRwLockOptionalKeyPathChain {
810 outer_keypath: self.outer_keypath,
811 inner_keypath: composed,
812 }
813 }
814}
815
816pub struct OptionalArcMutexWritableKeyPathChain<Root, MutexValue, InnerValue, SubValue, F, G>
821where
822 F: for<'r> Fn(&'r Root) -> Option<&'r MutexValue>,
823 G: for<'r> Fn(&'r mut InnerValue) -> &'r mut SubValue,
824{
825 outer_keypath: OptionalKeyPath<Root, MutexValue, F>,
826 inner_keypath: WritableKeyPath<InnerValue, SubValue, G>,
827}
828
829impl<Root, MutexValue, InnerValue, SubValue, F, G> OptionalArcMutexWritableKeyPathChain<Root, MutexValue, InnerValue, SubValue, F, G>
830where
831 F: for<'r> Fn(&'r Root) -> Option<&'r MutexValue>,
832 G: for<'r> Fn(&'r mut InnerValue) -> &'r mut SubValue,
833 MutexValue: std::borrow::Borrow<Arc<Mutex<InnerValue>>>,
834{
835 pub fn get_mut<Callback, R>(self, container: &Root, callback: Callback) -> Option<R>
838 where
839 Callback: FnOnce(&mut SubValue) -> R,
840 {
841 self.outer_keypath.get(container).and_then(|arc_mutex_ref| {
842 arc_mutex_ref.borrow().lock().ok().map(|mut guard| {
843 let value_ref = self.inner_keypath.get_mut(&mut *guard);
844 callback(value_ref)
845 })
846 })
847 }
848
849 pub fn then<NextValue, H>(
851 self,
852 next: WritableKeyPath<SubValue, NextValue, H>,
853 ) -> OptionalArcMutexWritableKeyPathChain<Root, MutexValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r mut InnerValue) -> &'r mut NextValue + 'static>
854 where
855 H: for<'r> Fn(&'r mut SubValue) -> &'r mut NextValue,
856 G: 'static,
857 H: 'static,
858 InnerValue: 'static,
859 SubValue: 'static,
860 NextValue: 'static,
861 {
862 let first = self.inner_keypath;
863 let second = next;
864
865 let composed = WritableKeyPath::new(move |inner: &mut InnerValue| {
866 let sub = first.get_mut(inner);
867 second.get_mut(sub)
868 });
869
870 OptionalArcMutexWritableKeyPathChain {
871 outer_keypath: self.outer_keypath,
872 inner_keypath: composed,
873 }
874 }
875
876 pub fn chain_optional<NextValue, H>(
878 self,
879 next: WritableOptionalKeyPath<SubValue, NextValue, H>,
880 ) -> OptionalArcMutexWritableOptionalKeyPathChain<Root, MutexValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut NextValue> + 'static>
881 where
882 H: for<'r> Fn(&'r mut SubValue) -> Option<&'r mut NextValue>,
883 G: 'static,
884 H: 'static,
885 InnerValue: 'static,
886 SubValue: 'static,
887 NextValue: 'static,
888 {
889 let first = self.inner_keypath;
890 let second = next;
891
892 let composed = WritableOptionalKeyPath::new(move |inner: &mut InnerValue| {
893 let sub = first.get_mut(inner);
894 second.get_mut(sub)
895 });
896
897 OptionalArcMutexWritableOptionalKeyPathChain {
898 outer_keypath: self.outer_keypath,
899 inner_keypath: composed,
900 }
901 }
902}
903
904pub struct OptionalArcMutexWritableOptionalKeyPathChain<Root, MutexValue, InnerValue, SubValue, F, G>
907where
908 F: for<'r> Fn(&'r Root) -> Option<&'r MutexValue>,
909 G: for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut SubValue>,
910{
911 outer_keypath: OptionalKeyPath<Root, MutexValue, F>,
912 inner_keypath: WritableOptionalKeyPath<InnerValue, SubValue, G>,
913}
914
915impl<Root, MutexValue, InnerValue, SubValue, F, G> OptionalArcMutexWritableOptionalKeyPathChain<Root, MutexValue, InnerValue, SubValue, F, G>
916where
917 F: for<'r> Fn(&'r Root) -> Option<&'r MutexValue>,
918 G: for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut SubValue>,
919 MutexValue: std::borrow::Borrow<Arc<Mutex<InnerValue>>>,
920{
921 pub fn get_mut<Callback, R>(self, container: &Root, callback: Callback) -> Option<R>
924 where
925 Callback: FnOnce(&mut SubValue) -> R,
926 {
927 self.outer_keypath.get(container).and_then(|arc_mutex_ref| {
928 arc_mutex_ref.borrow().lock().ok().and_then(|mut guard| {
929 self.inner_keypath.get_mut(&mut *guard).map(|value_ref| callback(value_ref))
930 })
931 })
932 }
933
934 pub fn then<NextValue, H>(
936 self,
937 next: WritableKeyPath<SubValue, NextValue, H>,
938 ) -> OptionalArcMutexWritableOptionalKeyPathChain<Root, MutexValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut NextValue> + 'static>
939 where
940 H: for<'r> Fn(&'r mut SubValue) -> &'r mut NextValue,
941 G: 'static,
942 H: 'static,
943 InnerValue: 'static,
944 SubValue: 'static,
945 NextValue: 'static,
946 {
947 let first = self.inner_keypath;
948 let second = next;
949
950 let composed = WritableOptionalKeyPath::new(move |inner: &mut InnerValue| {
951 first.get_mut(inner).map(|sub| second.get_mut(sub))
952 });
953
954 OptionalArcMutexWritableOptionalKeyPathChain {
955 outer_keypath: self.outer_keypath,
956 inner_keypath: composed,
957 }
958 }
959
960 pub fn chain_optional<NextValue, H>(
962 self,
963 next: WritableOptionalKeyPath<SubValue, NextValue, H>,
964 ) -> OptionalArcMutexWritableOptionalKeyPathChain<Root, MutexValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut NextValue> + 'static>
965 where
966 H: for<'r> Fn(&'r mut SubValue) -> Option<&'r mut NextValue>,
967 G: 'static,
968 H: 'static,
969 InnerValue: 'static,
970 SubValue: 'static,
971 NextValue: 'static,
972 {
973 let first = self.inner_keypath;
974 let second = next;
975
976 let composed = WritableOptionalKeyPath::new(move |inner: &mut InnerValue| {
977 first.get_mut(inner).and_then(|sub| second.get_mut(sub))
978 });
979
980 OptionalArcMutexWritableOptionalKeyPathChain {
981 outer_keypath: self.outer_keypath,
982 inner_keypath: composed,
983 }
984 }
985}
986
987pub struct OptionalArcRwLockWritableKeyPathChain<Root, RwLockValue, InnerValue, SubValue, F, G>
992where
993 F: for<'r> Fn(&'r Root) -> Option<&'r RwLockValue>,
994 G: for<'r> Fn(&'r mut InnerValue) -> &'r mut SubValue,
995{
996 outer_keypath: OptionalKeyPath<Root, RwLockValue, F>,
997 inner_keypath: WritableKeyPath<InnerValue, SubValue, G>,
998}
999
1000impl<Root, RwLockValue, InnerValue, SubValue, F, G> OptionalArcRwLockWritableKeyPathChain<Root, RwLockValue, InnerValue, SubValue, F, G>
1001where
1002 F: for<'r> Fn(&'r Root) -> Option<&'r RwLockValue>,
1003 G: for<'r> Fn(&'r mut InnerValue) -> &'r mut SubValue,
1004 RwLockValue: std::borrow::Borrow<Arc<RwLock<InnerValue>>>,
1005{
1006 pub fn get_mut<Callback, R>(self, container: &Root, callback: Callback) -> Option<R>
1009 where
1010 Callback: FnOnce(&mut SubValue) -> R,
1011 {
1012 self.outer_keypath.get(container).and_then(|arc_rwlock_ref| {
1013 arc_rwlock_ref.borrow().write().ok().map(|mut guard| {
1014 let value_ref = self.inner_keypath.get_mut(&mut *guard);
1015 callback(value_ref)
1016 })
1017 })
1018 }
1019
1020 pub fn then<NextValue, H>(
1022 self,
1023 next: WritableKeyPath<SubValue, NextValue, H>,
1024 ) -> OptionalArcRwLockWritableKeyPathChain<Root, RwLockValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r mut InnerValue) -> &'r mut NextValue + 'static>
1025 where
1026 H: for<'r> Fn(&'r mut SubValue) -> &'r mut NextValue,
1027 G: 'static,
1028 H: 'static,
1029 InnerValue: 'static,
1030 SubValue: 'static,
1031 NextValue: 'static,
1032 {
1033 let first = self.inner_keypath;
1034 let second = next;
1035
1036 let composed = WritableKeyPath::new(move |inner: &mut InnerValue| {
1037 let sub = first.get_mut(inner);
1038 second.get_mut(sub)
1039 });
1040
1041 OptionalArcRwLockWritableKeyPathChain {
1042 outer_keypath: self.outer_keypath,
1043 inner_keypath: composed,
1044 }
1045 }
1046
1047 pub fn chain_optional<NextValue, H>(
1049 self,
1050 next: WritableOptionalKeyPath<SubValue, NextValue, H>,
1051 ) -> OptionalArcRwLockWritableOptionalKeyPathChain<Root, RwLockValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut NextValue> + 'static>
1052 where
1053 H: for<'r> Fn(&'r mut SubValue) -> Option<&'r mut NextValue>,
1054 G: 'static,
1055 H: 'static,
1056 InnerValue: 'static,
1057 SubValue: 'static,
1058 NextValue: 'static,
1059 {
1060 let first = self.inner_keypath;
1061 let second = next;
1062
1063 let composed = WritableOptionalKeyPath::new(move |inner: &mut InnerValue| {
1064 let sub = first.get_mut(inner);
1065 second.get_mut(sub)
1066 });
1067
1068 OptionalArcRwLockWritableOptionalKeyPathChain {
1069 outer_keypath: self.outer_keypath,
1070 inner_keypath: composed,
1071 }
1072 }
1073}
1074
1075pub struct OptionalArcRwLockWritableOptionalKeyPathChain<Root, RwLockValue, InnerValue, SubValue, F, G>
1078where
1079 F: for<'r> Fn(&'r Root) -> Option<&'r RwLockValue>,
1080 G: for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut SubValue>,
1081{
1082 outer_keypath: OptionalKeyPath<Root, RwLockValue, F>,
1083 inner_keypath: WritableOptionalKeyPath<InnerValue, SubValue, G>,
1084}
1085
1086impl<Root, RwLockValue, InnerValue, SubValue, F, G> OptionalArcRwLockWritableOptionalKeyPathChain<Root, RwLockValue, InnerValue, SubValue, F, G>
1087where
1088 F: for<'r> Fn(&'r Root) -> Option<&'r RwLockValue>,
1089 G: for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut SubValue>,
1090 RwLockValue: std::borrow::Borrow<Arc<RwLock<InnerValue>>>,
1091{
1092 pub fn get_mut<Callback, R>(self, container: &Root, callback: Callback) -> Option<R>
1095 where
1096 Callback: FnOnce(&mut SubValue) -> R,
1097 {
1098 self.outer_keypath.get(container).and_then(|arc_rwlock_ref| {
1099 arc_rwlock_ref.borrow().write().ok().and_then(|mut guard| {
1100 self.inner_keypath.get_mut(&mut *guard).map(|value_ref| callback(value_ref))
1101 })
1102 })
1103 }
1104
1105 pub fn then<NextValue, H>(
1107 self,
1108 next: WritableKeyPath<SubValue, NextValue, H>,
1109 ) -> OptionalArcRwLockWritableOptionalKeyPathChain<Root, RwLockValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut NextValue> + 'static>
1110 where
1111 H: for<'r> Fn(&'r mut SubValue) -> &'r mut NextValue,
1112 G: 'static,
1113 H: 'static,
1114 InnerValue: 'static,
1115 SubValue: 'static,
1116 NextValue: 'static,
1117 {
1118 let first = self.inner_keypath;
1119 let second = next;
1120
1121 let composed = WritableOptionalKeyPath::new(move |inner: &mut InnerValue| {
1122 first.get_mut(inner).map(|sub| second.get_mut(sub))
1123 });
1124
1125 OptionalArcRwLockWritableOptionalKeyPathChain {
1126 outer_keypath: self.outer_keypath,
1127 inner_keypath: composed,
1128 }
1129 }
1130
1131 pub fn chain_optional<NextValue, H>(
1133 self,
1134 next: WritableOptionalKeyPath<SubValue, NextValue, H>,
1135 ) -> OptionalArcRwLockWritableOptionalKeyPathChain<Root, RwLockValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut NextValue> + 'static>
1136 where
1137 H: for<'r> Fn(&'r mut SubValue) -> Option<&'r mut NextValue>,
1138 G: 'static,
1139 H: 'static,
1140 InnerValue: 'static,
1141 SubValue: 'static,
1142 NextValue: 'static,
1143 {
1144 let first = self.inner_keypath;
1145 let second = next;
1146
1147 let composed = WritableOptionalKeyPath::new(move |inner: &mut InnerValue| {
1148 first.get_mut(inner).and_then(|sub| second.get_mut(sub))
1149 });
1150
1151 OptionalArcRwLockWritableOptionalKeyPathChain {
1152 outer_keypath: self.outer_keypath,
1153 inner_keypath: composed,
1154 }
1155 }
1156}
1157
1158pub struct ArcRwLockWritableKeyPathChain<Root, RwLockValue, InnerValue, SubValue, F, G>
1163where
1164 F: for<'r> Fn(&'r Root) -> &'r RwLockValue,
1165 G: for<'r> Fn(&'r mut InnerValue) -> &'r mut SubValue,
1166{
1167 outer_keypath: KeyPath<Root, RwLockValue, F>,
1168 inner_keypath: WritableKeyPath<InnerValue, SubValue, G>,
1169}
1170
1171impl<Root, RwLockValue, InnerValue, SubValue, F, G> ArcRwLockWritableKeyPathChain<Root, RwLockValue, InnerValue, SubValue, F, G>
1172where
1173 F: for<'r> Fn(&'r Root) -> &'r RwLockValue,
1174 G: for<'r> Fn(&'r mut InnerValue) -> &'r mut SubValue,
1175 RwLockValue: std::borrow::Borrow<Arc<RwLock<InnerValue>>>,
1176{
1177 pub fn get_mut<Callback, R>(self, container: &Root, callback: Callback) -> Option<R>
1180 where
1181 Callback: FnOnce(&mut SubValue) -> R,
1182 {
1183 let arc_rwlock_ref = self.outer_keypath.get(container);
1184 arc_rwlock_ref.borrow().write().ok().map(|mut guard| {
1185 let value_ref = self.inner_keypath.get_mut(&mut *guard);
1186 callback(value_ref)
1187 })
1188 }
1189
1190 pub fn then<NextValue, H>(
1201 self,
1202 next: WritableKeyPath<SubValue, NextValue, H>,
1203 ) -> ArcRwLockWritableKeyPathChain<Root, RwLockValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r mut InnerValue) -> &'r mut NextValue + 'static>
1204 where
1205 H: for<'r> Fn(&'r mut SubValue) -> &'r mut NextValue,
1206 G: 'static,
1207 H: 'static,
1208 InnerValue: 'static,
1209 SubValue: 'static,
1210 NextValue: 'static,
1211 {
1212 let first = self.inner_keypath;
1213 let second = next;
1214
1215 let composed = WritableKeyPath::new(move |inner: &mut InnerValue| {
1217 let sub = first.get_mut(inner);
1218 second.get_mut(sub)
1219 });
1220
1221 ArcRwLockWritableKeyPathChain {
1222 outer_keypath: self.outer_keypath,
1223 inner_keypath: composed,
1224 }
1225 }
1226
1227 pub fn chain_optional<NextValue, H>(
1238 self,
1239 next: WritableOptionalKeyPath<SubValue, NextValue, H>,
1240 ) -> ArcRwLockWritableOptionalKeyPathChain<Root, RwLockValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut NextValue> + 'static>
1241 where
1242 H: for<'r> Fn(&'r mut SubValue) -> Option<&'r mut NextValue>,
1243 G: 'static,
1244 H: 'static,
1245 InnerValue: 'static,
1246 SubValue: 'static,
1247 NextValue: 'static,
1248 {
1249 let first = self.inner_keypath;
1250 let second = next;
1251
1252 let composed = WritableOptionalKeyPath::new(move |inner: &mut InnerValue| {
1254 let sub = first.get_mut(inner);
1255 second.get_mut(sub)
1256 });
1257
1258 ArcRwLockWritableOptionalKeyPathChain {
1259 outer_keypath: self.outer_keypath,
1260 inner_keypath: composed,
1261 }
1262 }
1263}
1264
1265pub struct ArcRwLockWritableOptionalKeyPathChain<Root, RwLockValue, InnerValue, SubValue, F, G>
1268where
1269 F: for<'r> Fn(&'r Root) -> &'r RwLockValue,
1270 G: for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut SubValue>,
1271{
1272 outer_keypath: KeyPath<Root, RwLockValue, F>,
1273 inner_keypath: WritableOptionalKeyPath<InnerValue, SubValue, G>,
1274}
1275
1276impl<Root, RwLockValue, InnerValue, SubValue, F, G> ArcRwLockWritableOptionalKeyPathChain<Root, RwLockValue, InnerValue, SubValue, F, G>
1277where
1278 F: for<'r> Fn(&'r Root) -> &'r RwLockValue,
1279 G: for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut SubValue>,
1280 RwLockValue: std::borrow::Borrow<Arc<RwLock<InnerValue>>>,
1281{
1282 pub fn get_mut<Callback, R>(self, container: &Root, callback: Callback) -> Option<R>
1285 where
1286 Callback: FnOnce(&mut SubValue) -> R,
1287 {
1288 let arc_rwlock_ref = self.outer_keypath.get(container);
1289 arc_rwlock_ref.borrow().write().ok().and_then(|mut guard| {
1290 self.inner_keypath.get_mut(&mut *guard).map(|value_ref| callback(value_ref))
1291 })
1292 }
1293
1294 pub fn then<NextValue, H>(
1297 self,
1298 next: WritableKeyPath<SubValue, NextValue, H>,
1299 ) -> ArcRwLockWritableOptionalKeyPathChain<Root, RwLockValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut NextValue> + 'static>
1300 where
1301 H: for<'r> Fn(&'r mut SubValue) -> &'r mut NextValue,
1302 G: 'static,
1303 H: 'static,
1304 InnerValue: 'static,
1305 SubValue: 'static,
1306 NextValue: 'static,
1307 {
1308 let first = self.inner_keypath;
1309 let second = next;
1310
1311 let composed = WritableOptionalKeyPath::new(move |inner: &mut InnerValue| {
1313 if let Some(sub) = first.get_mut(inner) {
1314 Some(second.get_mut(sub))
1315 } else {
1316 None
1317 }
1318 });
1319
1320 ArcRwLockWritableOptionalKeyPathChain {
1321 outer_keypath: self.outer_keypath,
1322 inner_keypath: composed,
1323 }
1324 }
1325
1326 pub fn chain_optional<NextValue, H>(
1329 self,
1330 next: WritableOptionalKeyPath<SubValue, NextValue, H>,
1331 ) -> ArcRwLockWritableOptionalKeyPathChain<Root, RwLockValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut NextValue> + 'static>
1332 where
1333 H: for<'r> Fn(&'r mut SubValue) -> Option<&'r mut NextValue>,
1334 G: 'static,
1335 H: 'static,
1336 InnerValue: 'static,
1337 SubValue: 'static,
1338 NextValue: 'static,
1339 {
1340 let first = self.inner_keypath;
1341 let second = next;
1342
1343 let composed = WritableOptionalKeyPath::new(move |inner: &mut InnerValue| {
1345 first.get_mut(inner).and_then(|sub| second.get_mut(sub))
1346 });
1347
1348 ArcRwLockWritableOptionalKeyPathChain {
1349 outer_keypath: self.outer_keypath,
1350 inner_keypath: composed,
1351 }
1352 }
1353}
1354
1355#[cfg(feature = "parking_lot")]
1358use parking_lot::{Mutex as ParkingMutex, RwLock as ParkingRwLock};
1359
1360#[cfg(feature = "parking_lot")]
1362pub struct ArcParkingMutexKeyPathChain<Root, InnerValue, SubValue, F, G>
1363where
1364 F: for<'r> Fn(&'r Root) -> &'r Arc<parking_lot::Mutex<InnerValue>>,
1365 G: for<'r> Fn(&'r InnerValue) -> &'r SubValue,
1366{
1367 outer_keypath: KeyPath<Root, Arc<parking_lot::Mutex<InnerValue>>, F>,
1368 inner_keypath: KeyPath<InnerValue, SubValue, G>,
1369}
1370
1371#[cfg(feature = "parking_lot")]
1372impl<Root, InnerValue, SubValue, F, G> ArcParkingMutexKeyPathChain<Root, InnerValue, SubValue, F, G>
1373where
1374 F: for<'r> Fn(&'r Root) -> &'r Arc<parking_lot::Mutex<InnerValue>>,
1375 G: for<'r> Fn(&'r InnerValue) -> &'r SubValue,
1376{
1377 pub fn get<Callback>(self, container: &Root, callback: Callback)
1379 where
1380 Callback: FnOnce(&SubValue) -> (),
1381 {
1382 let arc_mutex_ref = self.outer_keypath.get(container);
1383 let guard = arc_mutex_ref.lock();
1384 let value = self.inner_keypath.get(&*guard);
1385 callback(value);
1386 }
1387
1388 pub fn then<NextValue, H>(
1390 self,
1391 next: KeyPath<SubValue, NextValue, H>,
1392 ) -> ArcParkingMutexKeyPathChain<Root, InnerValue, NextValue, F, impl for<'r> Fn(&'r InnerValue) -> &'r NextValue + 'static>
1393 where
1394 H: for<'r> Fn(&'r SubValue) -> &'r NextValue,
1395 G: 'static,
1396 H: 'static,
1397 InnerValue: 'static,
1398 SubValue: 'static,
1399 NextValue: 'static,
1400 {
1401 let first = self.inner_keypath;
1402 let second = next;
1403
1404 let composed = KeyPath::new(move |inner: &InnerValue| {
1405 let sub = first.get(inner);
1406 second.get(sub)
1407 });
1408
1409 ArcParkingMutexKeyPathChain {
1410 outer_keypath: self.outer_keypath,
1411 inner_keypath: composed,
1412 }
1413 }
1414
1415 pub fn chain_optional<NextValue, H>(
1417 self,
1418 next: OptionalKeyPath<SubValue, NextValue, H>,
1419 ) -> ArcParkingMutexOptionalKeyPathChain<Root, InnerValue, NextValue, F, impl for<'r> Fn(&'r InnerValue) -> Option<&'r NextValue> + 'static>
1420 where
1421 H: for<'r> Fn(&'r SubValue) -> Option<&'r NextValue>,
1422 G: 'static,
1423 H: 'static,
1424 InnerValue: 'static,
1425 SubValue: 'static,
1426 NextValue: 'static,
1427 {
1428 let first = self.inner_keypath;
1429 let second = next;
1430
1431 let composed = OptionalKeyPath::new(move |inner: &InnerValue| {
1432 let sub = first.get(inner);
1433 second.get(sub)
1434 });
1435
1436 ArcParkingMutexOptionalKeyPathChain {
1437 outer_keypath: self.outer_keypath,
1438 inner_keypath: composed,
1439 }
1440 }
1441}
1442
1443#[cfg(feature = "parking_lot")]
1445pub struct ArcParkingMutexOptionalKeyPathChain<Root, InnerValue, SubValue, F, G>
1446where
1447 F: for<'r> Fn(&'r Root) -> &'r Arc<parking_lot::Mutex<InnerValue>>,
1448 G: for<'r> Fn(&'r InnerValue) -> Option<&'r SubValue>,
1449{
1450 outer_keypath: KeyPath<Root, Arc<parking_lot::Mutex<InnerValue>>, F>,
1451 inner_keypath: OptionalKeyPath<InnerValue, SubValue, G>,
1452}
1453
1454#[cfg(feature = "parking_lot")]
1455impl<Root, InnerValue, SubValue, F, G> ArcParkingMutexOptionalKeyPathChain<Root, InnerValue, SubValue, F, G>
1456where
1457 F: for<'r> Fn(&'r Root) -> &'r Arc<parking_lot::Mutex<InnerValue>>,
1458 G: for<'r> Fn(&'r InnerValue) -> Option<&'r SubValue>,
1459{
1460 pub fn get<Callback>(self, container: &Root, callback: Callback) -> Option<()>
1462 where
1463 Callback: FnOnce(&SubValue) -> (),
1464 {
1465 let arc_mutex_ref = self.outer_keypath.get(container);
1466 let guard = arc_mutex_ref.lock();
1467 self.inner_keypath.get(&*guard).map(|value| callback(value))
1468 }
1469
1470 pub fn then<NextValue, H>(
1472 self,
1473 next: KeyPath<SubValue, NextValue, H>,
1474 ) -> ArcParkingMutexOptionalKeyPathChain<Root, InnerValue, NextValue, F, impl for<'r> Fn(&'r InnerValue) -> Option<&'r NextValue> + 'static>
1475 where
1476 H: for<'r> Fn(&'r SubValue) -> &'r NextValue,
1477 G: 'static,
1478 H: 'static,
1479 InnerValue: 'static,
1480 SubValue: 'static,
1481 NextValue: 'static,
1482 {
1483 let first = self.inner_keypath;
1484 let second = next;
1485
1486 let composed = OptionalKeyPath::new(move |inner: &InnerValue| {
1487 first.get(inner).map(|sub| second.get(sub))
1488 });
1489
1490 ArcParkingMutexOptionalKeyPathChain {
1491 outer_keypath: self.outer_keypath,
1492 inner_keypath: composed,
1493 }
1494 }
1495
1496 pub fn chain_optional<NextValue, H>(
1498 self,
1499 next: OptionalKeyPath<SubValue, NextValue, H>,
1500 ) -> ArcParkingMutexOptionalKeyPathChain<Root, InnerValue, NextValue, F, impl for<'r> Fn(&'r InnerValue) -> Option<&'r NextValue> + 'static>
1501 where
1502 H: for<'r> Fn(&'r SubValue) -> Option<&'r NextValue>,
1503 G: 'static,
1504 H: 'static,
1505 InnerValue: 'static,
1506 SubValue: 'static,
1507 NextValue: 'static,
1508 {
1509 let first = self.inner_keypath;
1510 let second = next;
1511
1512 let composed = OptionalKeyPath::new(move |inner: &InnerValue| {
1513 first.get(inner).and_then(|sub| second.get(sub))
1514 });
1515
1516 ArcParkingMutexOptionalKeyPathChain {
1517 outer_keypath: self.outer_keypath,
1518 inner_keypath: composed,
1519 }
1520 }
1521}
1522
1523#[cfg(feature = "parking_lot")]
1525pub struct ArcParkingMutexWritableKeyPathChain<Root, InnerValue, SubValue, F, G>
1526where
1527 F: for<'r> Fn(&'r Root) -> &'r Arc<parking_lot::Mutex<InnerValue>>,
1528 G: for<'r> Fn(&'r mut InnerValue) -> &'r mut SubValue,
1529{
1530 outer_keypath: KeyPath<Root, Arc<parking_lot::Mutex<InnerValue>>, F>,
1531 inner_keypath: WritableKeyPath<InnerValue, SubValue, G>,
1532}
1533
1534#[cfg(feature = "parking_lot")]
1535impl<Root, InnerValue, SubValue, F, G> ArcParkingMutexWritableKeyPathChain<Root, InnerValue, SubValue, F, G>
1536where
1537 F: for<'r> Fn(&'r Root) -> &'r Arc<parking_lot::Mutex<InnerValue>>,
1538 G: for<'r> Fn(&'r mut InnerValue) -> &'r mut SubValue,
1539{
1540 pub fn get_mut<Callback, R>(self, container: &Root, callback: Callback) -> R
1542 where
1543 Callback: FnOnce(&mut SubValue) -> R,
1544 {
1545 let arc_mutex_ref = self.outer_keypath.get(container);
1546 let mut guard = arc_mutex_ref.lock();
1547 let value_ref = self.inner_keypath.get_mut(&mut *guard);
1548 callback(value_ref)
1549 }
1550
1551 pub fn then<NextValue, H>(
1553 self,
1554 next: WritableKeyPath<SubValue, NextValue, H>,
1555 ) -> ArcParkingMutexWritableKeyPathChain<Root, InnerValue, NextValue, F, impl for<'r> Fn(&'r mut InnerValue) -> &'r mut NextValue + 'static>
1556 where
1557 H: for<'r> Fn(&'r mut SubValue) -> &'r mut NextValue,
1558 G: 'static,
1559 H: 'static,
1560 InnerValue: 'static,
1561 SubValue: 'static,
1562 NextValue: 'static,
1563 {
1564 let first = self.inner_keypath;
1565 let second = next;
1566
1567 let composed = WritableKeyPath::new(move |inner: &mut InnerValue| {
1568 let sub = first.get_mut(inner);
1569 second.get_mut(sub)
1570 });
1571
1572 ArcParkingMutexWritableKeyPathChain {
1573 outer_keypath: self.outer_keypath,
1574 inner_keypath: composed,
1575 }
1576 }
1577
1578 pub fn chain_optional<NextValue, H>(
1580 self,
1581 next: WritableOptionalKeyPath<SubValue, NextValue, H>,
1582 ) -> ArcParkingMutexWritableOptionalKeyPathChain<Root, InnerValue, NextValue, F, impl for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut NextValue> + 'static>
1583 where
1584 H: for<'r> Fn(&'r mut SubValue) -> Option<&'r mut NextValue>,
1585 G: 'static,
1586 H: 'static,
1587 InnerValue: 'static,
1588 SubValue: 'static,
1589 NextValue: 'static,
1590 {
1591 let first = self.inner_keypath;
1592 let second = next;
1593
1594 let composed = WritableOptionalKeyPath::new(move |inner: &mut InnerValue| {
1595 let sub = first.get_mut(inner);
1596 second.get_mut(sub)
1597 });
1598
1599 ArcParkingMutexWritableOptionalKeyPathChain {
1600 outer_keypath: self.outer_keypath,
1601 inner_keypath: composed,
1602 }
1603 }
1604}
1605
1606#[cfg(feature = "parking_lot")]
1608pub struct ArcParkingMutexWritableOptionalKeyPathChain<Root, InnerValue, SubValue, F, G>
1609where
1610 F: for<'r> Fn(&'r Root) -> &'r Arc<parking_lot::Mutex<InnerValue>>,
1611 G: for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut SubValue>,
1612{
1613 outer_keypath: KeyPath<Root, Arc<parking_lot::Mutex<InnerValue>>, F>,
1614 inner_keypath: WritableOptionalKeyPath<InnerValue, SubValue, G>,
1615}
1616
1617#[cfg(feature = "parking_lot")]
1618impl<Root, InnerValue, SubValue, F, G> ArcParkingMutexWritableOptionalKeyPathChain<Root, InnerValue, SubValue, F, G>
1619where
1620 F: for<'r> Fn(&'r Root) -> &'r Arc<parking_lot::Mutex<InnerValue>>,
1621 G: for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut SubValue>,
1622{
1623 pub fn get_mut<Callback, R>(self, container: &Root, callback: Callback) -> Option<R>
1625 where
1626 Callback: FnOnce(&mut SubValue) -> R,
1627 {
1628 let arc_mutex_ref = self.outer_keypath.get(container);
1629 let mut guard = arc_mutex_ref.lock();
1630 self.inner_keypath.get_mut(&mut *guard).map(|value_ref| callback(value_ref))
1631 }
1632
1633 pub fn then<NextValue, H>(
1635 self,
1636 next: WritableKeyPath<SubValue, NextValue, H>,
1637 ) -> ArcParkingMutexWritableOptionalKeyPathChain<Root, InnerValue, NextValue, F, impl for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut NextValue> + 'static>
1638 where
1639 H: for<'r> Fn(&'r mut SubValue) -> &'r mut NextValue,
1640 G: 'static,
1641 H: 'static,
1642 InnerValue: 'static,
1643 SubValue: 'static,
1644 NextValue: 'static,
1645 {
1646 let first = self.inner_keypath;
1647 let second = next;
1648
1649 let composed = WritableOptionalKeyPath::new(move |inner: &mut InnerValue| {
1650 first.get_mut(inner).map(|sub| second.get_mut(sub))
1651 });
1652
1653 ArcParkingMutexWritableOptionalKeyPathChain {
1654 outer_keypath: self.outer_keypath,
1655 inner_keypath: composed,
1656 }
1657 }
1658
1659 pub fn chain_optional<NextValue, H>(
1661 self,
1662 next: WritableOptionalKeyPath<SubValue, NextValue, H>,
1663 ) -> ArcParkingMutexWritableOptionalKeyPathChain<Root, InnerValue, NextValue, F, impl for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut NextValue> + 'static>
1664 where
1665 H: for<'r> Fn(&'r mut SubValue) -> Option<&'r mut NextValue>,
1666 G: 'static,
1667 H: 'static,
1668 InnerValue: 'static,
1669 SubValue: 'static,
1670 NextValue: 'static,
1671 {
1672 let first = self.inner_keypath;
1673 let second = next;
1674
1675 let composed = WritableOptionalKeyPath::new(move |inner: &mut InnerValue| {
1676 first.get_mut(inner).and_then(|sub| second.get_mut(sub))
1677 });
1678
1679 ArcParkingMutexWritableOptionalKeyPathChain {
1680 outer_keypath: self.outer_keypath,
1681 inner_keypath: composed,
1682 }
1683 }
1684}
1685
1686#[cfg(feature = "parking_lot")]
1688pub struct ArcParkingRwLockKeyPathChain<Root, InnerValue, SubValue, F, G>
1689where
1690 F: for<'r> Fn(&'r Root) -> &'r Arc<parking_lot::RwLock<InnerValue>>,
1691 G: for<'r> Fn(&'r InnerValue) -> &'r SubValue,
1692{
1693 outer_keypath: KeyPath<Root, Arc<parking_lot::RwLock<InnerValue>>, F>,
1694 inner_keypath: KeyPath<InnerValue, SubValue, G>,
1695}
1696
1697#[cfg(feature = "parking_lot")]
1698impl<Root, InnerValue, SubValue, F, G> ArcParkingRwLockKeyPathChain<Root, InnerValue, SubValue, F, G>
1699where
1700 F: for<'r> Fn(&'r Root) -> &'r Arc<parking_lot::RwLock<InnerValue>>,
1701 G: for<'r> Fn(&'r InnerValue) -> &'r SubValue,
1702{
1703 pub fn get<Callback>(self, container: &Root, callback: Callback)
1705 where
1706 Callback: FnOnce(&SubValue) -> (),
1707 {
1708 let arc_rwlock_ref = self.outer_keypath.get(container);
1709 let guard = arc_rwlock_ref.read();
1710 let value = self.inner_keypath.get(&*guard);
1711 callback(value);
1712 }
1713
1714 pub fn then<NextValue, H>(
1716 self,
1717 next: KeyPath<SubValue, NextValue, H>,
1718 ) -> ArcParkingRwLockKeyPathChain<Root, InnerValue, NextValue, F, impl for<'r> Fn(&'r InnerValue) -> &'r NextValue + 'static>
1719 where
1720 H: for<'r> Fn(&'r SubValue) -> &'r NextValue,
1721 G: 'static,
1722 H: 'static,
1723 InnerValue: 'static,
1724 SubValue: 'static,
1725 NextValue: 'static,
1726 {
1727 let first = self.inner_keypath;
1728 let second = next;
1729
1730 let composed = KeyPath::new(move |inner: &InnerValue| {
1731 let sub = first.get(inner);
1732 second.get(sub)
1733 });
1734
1735 ArcParkingRwLockKeyPathChain {
1736 outer_keypath: self.outer_keypath,
1737 inner_keypath: composed,
1738 }
1739 }
1740
1741 pub fn chain_optional<NextValue, H>(
1743 self,
1744 next: OptionalKeyPath<SubValue, NextValue, H>,
1745 ) -> ArcParkingRwLockOptionalKeyPathChain<Root, InnerValue, NextValue, F, impl for<'r> Fn(&'r InnerValue) -> Option<&'r NextValue> + 'static>
1746 where
1747 H: for<'r> Fn(&'r SubValue) -> Option<&'r NextValue>,
1748 G: 'static,
1749 H: 'static,
1750 InnerValue: 'static,
1751 SubValue: 'static,
1752 NextValue: 'static,
1753 {
1754 let first = self.inner_keypath;
1755 let second = next;
1756
1757 let composed = OptionalKeyPath::new(move |inner: &InnerValue| {
1758 let sub = first.get(inner);
1759 second.get(sub)
1760 });
1761
1762 ArcParkingRwLockOptionalKeyPathChain {
1763 outer_keypath: self.outer_keypath,
1764 inner_keypath: composed,
1765 }
1766 }
1767}
1768
1769#[cfg(feature = "parking_lot")]
1771pub struct ArcParkingRwLockOptionalKeyPathChain<Root, InnerValue, SubValue, F, G>
1772where
1773 F: for<'r> Fn(&'r Root) -> &'r Arc<parking_lot::RwLock<InnerValue>>,
1774 G: for<'r> Fn(&'r InnerValue) -> Option<&'r SubValue>,
1775{
1776 outer_keypath: KeyPath<Root, Arc<parking_lot::RwLock<InnerValue>>, F>,
1777 inner_keypath: OptionalKeyPath<InnerValue, SubValue, G>,
1778}
1779
1780#[cfg(feature = "parking_lot")]
1781impl<Root, InnerValue, SubValue, F, G> ArcParkingRwLockOptionalKeyPathChain<Root, InnerValue, SubValue, F, G>
1782where
1783 F: for<'r> Fn(&'r Root) -> &'r Arc<parking_lot::RwLock<InnerValue>>,
1784 G: for<'r> Fn(&'r InnerValue) -> Option<&'r SubValue>,
1785{
1786 pub fn get<Callback>(self, container: &Root, callback: Callback) -> Option<()>
1788 where
1789 Callback: FnOnce(&SubValue) -> (),
1790 {
1791 let arc_rwlock_ref = self.outer_keypath.get(container);
1792 let guard = arc_rwlock_ref.read();
1793 self.inner_keypath.get(&*guard).map(|value| callback(value))
1794 }
1795
1796 pub fn then<NextValue, H>(
1798 self,
1799 next: KeyPath<SubValue, NextValue, H>,
1800 ) -> ArcParkingRwLockOptionalKeyPathChain<Root, InnerValue, NextValue, F, impl for<'r> Fn(&'r InnerValue) -> Option<&'r NextValue> + 'static>
1801 where
1802 H: for<'r> Fn(&'r SubValue) -> &'r NextValue,
1803 G: 'static,
1804 H: 'static,
1805 InnerValue: 'static,
1806 SubValue: 'static,
1807 NextValue: 'static,
1808 {
1809 let first = self.inner_keypath;
1810 let second = next;
1811
1812 let composed = OptionalKeyPath::new(move |inner: &InnerValue| {
1813 first.get(inner).map(|sub| second.get(sub))
1814 });
1815
1816 ArcParkingRwLockOptionalKeyPathChain {
1817 outer_keypath: self.outer_keypath,
1818 inner_keypath: composed,
1819 }
1820 }
1821
1822 pub fn chain_optional<NextValue, H>(
1824 self,
1825 next: OptionalKeyPath<SubValue, NextValue, H>,
1826 ) -> ArcParkingRwLockOptionalKeyPathChain<Root, InnerValue, NextValue, F, impl for<'r> Fn(&'r InnerValue) -> Option<&'r NextValue> + 'static>
1827 where
1828 H: for<'r> Fn(&'r SubValue) -> Option<&'r NextValue>,
1829 G: 'static,
1830 H: 'static,
1831 InnerValue: 'static,
1832 SubValue: 'static,
1833 NextValue: 'static,
1834 {
1835 let first = self.inner_keypath;
1836 let second = next;
1837
1838 let composed = OptionalKeyPath::new(move |inner: &InnerValue| {
1839 first.get(inner).and_then(|sub| second.get(sub))
1840 });
1841
1842 ArcParkingRwLockOptionalKeyPathChain {
1843 outer_keypath: self.outer_keypath,
1844 inner_keypath: composed,
1845 }
1846 }
1847}
1848
1849#[cfg(feature = "parking_lot")]
1851pub struct ArcParkingRwLockWritableKeyPathChain<Root, InnerValue, SubValue, F, G>
1852where
1853 F: for<'r> Fn(&'r Root) -> &'r Arc<parking_lot::RwLock<InnerValue>>,
1854 G: for<'r> Fn(&'r mut InnerValue) -> &'r mut SubValue,
1855{
1856 outer_keypath: KeyPath<Root, Arc<parking_lot::RwLock<InnerValue>>, F>,
1857 inner_keypath: WritableKeyPath<InnerValue, SubValue, G>,
1858}
1859
1860#[cfg(feature = "parking_lot")]
1861impl<Root, InnerValue, SubValue, F, G> ArcParkingRwLockWritableKeyPathChain<Root, InnerValue, SubValue, F, G>
1862where
1863 F: for<'r> Fn(&'r Root) -> &'r Arc<parking_lot::RwLock<InnerValue>>,
1864 G: for<'r> Fn(&'r mut InnerValue) -> &'r mut SubValue,
1865{
1866 pub fn get_mut<Callback, R>(self, container: &Root, callback: Callback) -> R
1868 where
1869 Callback: FnOnce(&mut SubValue) -> R,
1870 {
1871 let arc_rwlock_ref = self.outer_keypath.get(container);
1872 let mut guard = arc_rwlock_ref.write();
1873 let value_ref = self.inner_keypath.get_mut(&mut *guard);
1874 callback(value_ref)
1875 }
1876
1877 pub fn then<NextValue, H>(
1880 self,
1881 next: WritableKeyPath<SubValue, NextValue, H>,
1882 ) -> ArcParkingRwLockWritableKeyPathChain<Root, InnerValue, NextValue, F, impl for<'r> Fn(&'r mut InnerValue) -> &'r mut NextValue + 'static>
1883 where
1884 H: for<'r> Fn(&'r mut SubValue) -> &'r mut NextValue,
1885 G: 'static,
1886 H: 'static,
1887 InnerValue: 'static,
1888 SubValue: 'static,
1889 NextValue: 'static,
1890 {
1891 let first = self.inner_keypath;
1892 let second = next;
1893
1894 let composed = WritableKeyPath::new(move |inner: &mut InnerValue| {
1896 let sub = first.get_mut(inner);
1897 second.get_mut(sub)
1898 });
1899
1900 ArcParkingRwLockWritableKeyPathChain {
1901 outer_keypath: self.outer_keypath,
1902 inner_keypath: composed,
1903 }
1904 }
1905
1906 pub fn chain_optional<NextValue, H>(
1909 self,
1910 next: WritableOptionalKeyPath<SubValue, NextValue, H>,
1911 ) -> ArcParkingRwLockWritableOptionalKeyPathChain<Root, InnerValue, NextValue, F, impl for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut NextValue> + 'static>
1912 where
1913 H: for<'r> Fn(&'r mut SubValue) -> Option<&'r mut NextValue>,
1914 G: 'static,
1915 H: 'static,
1916 InnerValue: 'static,
1917 SubValue: 'static,
1918 NextValue: 'static,
1919 {
1920 let first = self.inner_keypath;
1921 let second = next;
1922
1923 let composed = WritableOptionalKeyPath::new(move |inner: &mut InnerValue| {
1926 let sub = first.get_mut(inner);
1927 second.get_mut(sub)
1928 });
1929
1930 ArcParkingRwLockWritableOptionalKeyPathChain {
1931 outer_keypath: self.outer_keypath,
1932 inner_keypath: composed,
1933 }
1934 }
1935}
1936
1937#[cfg(feature = "parking_lot")]
1939pub struct ArcParkingRwLockWritableOptionalKeyPathChain<Root, InnerValue, SubValue, F, G>
1940where
1941 F: for<'r> Fn(&'r Root) -> &'r Arc<parking_lot::RwLock<InnerValue>>,
1942 G: for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut SubValue>,
1943{
1944 outer_keypath: KeyPath<Root, Arc<parking_lot::RwLock<InnerValue>>, F>,
1945 inner_keypath: WritableOptionalKeyPath<InnerValue, SubValue, G>,
1946}
1947
1948#[cfg(feature = "parking_lot")]
1949impl<Root, InnerValue, SubValue, F, G> ArcParkingRwLockWritableOptionalKeyPathChain<Root, InnerValue, SubValue, F, G>
1950where
1951 F: for<'r> Fn(&'r Root) -> &'r Arc<parking_lot::RwLock<InnerValue>>,
1952 G: for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut SubValue>,
1953{
1954 pub fn get_mut<Callback, R>(self, container: &Root, callback: Callback) -> Option<R>
1956 where
1957 Callback: FnOnce(&mut SubValue) -> R,
1958 {
1959 let arc_rwlock_ref = self.outer_keypath.get(container);
1960 let mut guard = arc_rwlock_ref.write();
1961 self.inner_keypath.get_mut(&mut *guard).map(|value_ref| callback(value_ref))
1962 }
1963
1964 pub fn then<NextValue, H>(
1967 self,
1968 next: WritableKeyPath<SubValue, NextValue, H>,
1969 ) -> ArcParkingRwLockWritableOptionalKeyPathChain<Root, InnerValue, NextValue, F, impl for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut NextValue> + 'static>
1970 where
1971 H: for<'r> Fn(&'r mut SubValue) -> &'r mut NextValue,
1972 G: 'static,
1973 H: 'static,
1974 InnerValue: 'static,
1975 SubValue: 'static,
1976 NextValue: 'static,
1977 {
1978 let first_keypath = self.inner_keypath;
1979 let second_keypath = next;
1980
1981 let composed = WritableOptionalKeyPath::new(move |inner: &mut InnerValue| {
1984 first_keypath.get_mut(inner).map(|sub| second_keypath.get_mut(sub))
1985 });
1986
1987 ArcParkingRwLockWritableOptionalKeyPathChain {
1988 outer_keypath: self.outer_keypath,
1989 inner_keypath: composed,
1990 }
1991 }
1992
1993 pub fn chain_optional<NextValue, H>(
1996 self,
1997 next: WritableOptionalKeyPath<SubValue, NextValue, H>,
1998 ) -> ArcParkingRwLockWritableOptionalKeyPathChain<Root, InnerValue, NextValue, F, impl for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut NextValue> + 'static>
1999 where
2000 H: for<'r> Fn(&'r mut SubValue) -> Option<&'r mut NextValue>,
2001 G: 'static,
2002 H: 'static,
2003 InnerValue: 'static,
2004 SubValue: 'static,
2005 NextValue: 'static,
2006 {
2007 let first = self.inner_keypath;
2008 let second = next;
2009
2010 let composed = WritableOptionalKeyPath::new(move |inner: &mut InnerValue| {
2012 first.get_mut(inner).and_then(|sub| second.get_mut(sub))
2013 });
2014
2015 ArcParkingRwLockWritableOptionalKeyPathChain {
2016 outer_keypath: self.outer_keypath,
2017 inner_keypath: composed,
2018 }
2019 }
2020}
2021
2022#[cfg(feature = "parking_lot")]
2026pub struct OptionalArcParkingMutexKeyPathChain<Root, InnerValue, SubValue, F, G>
2027where
2028 F: for<'r> Fn(&'r Root) -> Option<&'r Arc<parking_lot::Mutex<InnerValue>>>,
2029 G: for<'r> Fn(&'r InnerValue) -> &'r SubValue,
2030{
2031 outer_keypath: OptionalKeyPath<Root, Arc<parking_lot::Mutex<InnerValue>>, F>,
2032 inner_keypath: KeyPath<InnerValue, SubValue, G>,
2033}
2034
2035#[cfg(feature = "parking_lot")]
2036impl<Root, InnerValue, SubValue, F, G> OptionalArcParkingMutexKeyPathChain<Root, InnerValue, SubValue, F, G>
2037where
2038 F: for<'r> Fn(&'r Root) -> Option<&'r Arc<parking_lot::Mutex<InnerValue>>>,
2039 G: for<'r> Fn(&'r InnerValue) -> &'r SubValue,
2040{
2041 pub fn get<Callback>(self, container: &Root, callback: Callback) -> Option<()>
2043 where
2044 Callback: FnOnce(&SubValue) -> (),
2045 {
2046 self.outer_keypath.get(container).map(|arc_mutex_ref| {
2047 let guard = arc_mutex_ref.lock();
2048 let value = self.inner_keypath.get(&*guard);
2049 callback(value)
2050 })
2051 }
2052
2053 pub fn then<NextValue, H>(
2055 self,
2056 next: KeyPath<SubValue, NextValue, H>,
2057 ) -> OptionalArcParkingMutexKeyPathChain<Root, InnerValue, NextValue, F, impl for<'r> Fn(&'r InnerValue) -> &'r NextValue + 'static>
2058 where
2059 H: for<'r> Fn(&'r SubValue) -> &'r NextValue,
2060 G: 'static,
2061 H: 'static,
2062 InnerValue: 'static,
2063 SubValue: 'static,
2064 NextValue: 'static,
2065 {
2066 let first = self.inner_keypath;
2067 let second = next;
2068
2069 let composed = KeyPath::new(move |inner: &InnerValue| {
2070 let sub = first.get(inner);
2071 second.get(sub)
2072 });
2073
2074 OptionalArcParkingMutexKeyPathChain {
2075 outer_keypath: self.outer_keypath,
2076 inner_keypath: composed,
2077 }
2078 }
2079
2080 pub fn chain_optional<NextValue, H>(
2082 self,
2083 next: OptionalKeyPath<SubValue, NextValue, H>,
2084 ) -> OptionalArcParkingMutexOptionalKeyPathChain<Root, InnerValue, NextValue, F, impl for<'r> Fn(&'r InnerValue) -> Option<&'r NextValue> + 'static>
2085 where
2086 H: for<'r> Fn(&'r SubValue) -> Option<&'r NextValue>,
2087 G: 'static,
2088 H: 'static,
2089 InnerValue: 'static,
2090 SubValue: 'static,
2091 NextValue: 'static,
2092 {
2093 let first = self.inner_keypath;
2094 let second = next;
2095
2096 let composed = OptionalKeyPath::new(move |inner: &InnerValue| {
2097 let sub = first.get(inner);
2098 second.get(sub)
2099 });
2100
2101 OptionalArcParkingMutexOptionalKeyPathChain {
2102 outer_keypath: self.outer_keypath,
2103 inner_keypath: composed,
2104 }
2105 }
2106}
2107
2108#[cfg(feature = "parking_lot")]
2110pub struct OptionalArcParkingMutexOptionalKeyPathChain<Root, InnerValue, SubValue, F, G>
2111where
2112 F: for<'r> Fn(&'r Root) -> Option<&'r Arc<parking_lot::Mutex<InnerValue>>>,
2113 G: for<'r> Fn(&'r InnerValue) -> Option<&'r SubValue>,
2114{
2115 outer_keypath: OptionalKeyPath<Root, Arc<parking_lot::Mutex<InnerValue>>, F>,
2116 inner_keypath: OptionalKeyPath<InnerValue, SubValue, G>,
2117}
2118
2119#[cfg(feature = "parking_lot")]
2120impl<Root, InnerValue, SubValue, F, G> OptionalArcParkingMutexOptionalKeyPathChain<Root, InnerValue, SubValue, F, G>
2121where
2122 F: for<'r> Fn(&'r Root) -> Option<&'r Arc<parking_lot::Mutex<InnerValue>>>,
2123 G: for<'r> Fn(&'r InnerValue) -> Option<&'r SubValue>,
2124{
2125 pub fn get<Callback>(self, container: &Root, callback: Callback) -> Option<()>
2127 where
2128 Callback: FnOnce(&SubValue) -> (),
2129 {
2130 self.outer_keypath.get(container).and_then(|arc_mutex_ref| {
2131 let guard = arc_mutex_ref.lock();
2132 self.inner_keypath.get(&*guard).map(|value| callback(value))
2133 })
2134 }
2135
2136 pub fn then<NextValue, H>(
2138 self,
2139 next: KeyPath<SubValue, NextValue, H>,
2140 ) -> OptionalArcParkingMutexOptionalKeyPathChain<Root, InnerValue, NextValue, F, impl for<'r> Fn(&'r InnerValue) -> Option<&'r NextValue> + 'static>
2141 where
2142 H: for<'r> Fn(&'r SubValue) -> &'r NextValue,
2143 G: 'static,
2144 H: 'static,
2145 InnerValue: 'static,
2146 SubValue: 'static,
2147 NextValue: 'static,
2148 {
2149 let first = self.inner_keypath;
2150 let second = next;
2151
2152 let composed = OptionalKeyPath::new(move |inner: &InnerValue| {
2153 first.get(inner).map(|sub| second.get(sub))
2154 });
2155
2156 OptionalArcParkingMutexOptionalKeyPathChain {
2157 outer_keypath: self.outer_keypath,
2158 inner_keypath: composed,
2159 }
2160 }
2161
2162 pub fn chain_optional<NextValue, H>(
2164 self,
2165 next: OptionalKeyPath<SubValue, NextValue, H>,
2166 ) -> OptionalArcParkingMutexOptionalKeyPathChain<Root, InnerValue, NextValue, F, impl for<'r> Fn(&'r InnerValue) -> Option<&'r NextValue> + 'static>
2167 where
2168 H: for<'r> Fn(&'r SubValue) -> Option<&'r NextValue>,
2169 G: 'static,
2170 H: 'static,
2171 InnerValue: 'static,
2172 SubValue: 'static,
2173 NextValue: 'static,
2174 {
2175 let first = self.inner_keypath;
2176 let second = next;
2177
2178 let composed = OptionalKeyPath::new(move |inner: &InnerValue| {
2179 first.get(inner).and_then(|sub| second.get(sub))
2180 });
2181
2182 OptionalArcParkingMutexOptionalKeyPathChain {
2183 outer_keypath: self.outer_keypath,
2184 inner_keypath: composed,
2185 }
2186 }
2187}
2188
2189#[cfg(feature = "parking_lot")]
2191pub struct OptionalArcParkingMutexWritableKeyPathChain<Root, InnerValue, SubValue, F, G>
2192where
2193 F: for<'r> Fn(&'r Root) -> Option<&'r Arc<parking_lot::Mutex<InnerValue>>>,
2194 G: for<'r> Fn(&'r mut InnerValue) -> &'r mut SubValue,
2195{
2196 outer_keypath: OptionalKeyPath<Root, Arc<parking_lot::Mutex<InnerValue>>, F>,
2197 inner_keypath: WritableKeyPath<InnerValue, SubValue, G>,
2198}
2199
2200#[cfg(feature = "parking_lot")]
2201impl<Root, InnerValue, SubValue, F, G> OptionalArcParkingMutexWritableKeyPathChain<Root, InnerValue, SubValue, F, G>
2202where
2203 F: for<'r> Fn(&'r Root) -> Option<&'r Arc<parking_lot::Mutex<InnerValue>>>,
2204 G: for<'r> Fn(&'r mut InnerValue) -> &'r mut SubValue,
2205{
2206 pub fn get_mut<Callback, R>(self, container: &Root, callback: Callback) -> Option<R>
2208 where
2209 Callback: FnOnce(&mut SubValue) -> R,
2210 {
2211 self.outer_keypath.get(container).map(|arc_mutex_ref| {
2212 let mut guard = arc_mutex_ref.lock();
2213 let value_ref = self.inner_keypath.get_mut(&mut *guard);
2214 callback(value_ref)
2215 })
2216 }
2217
2218 pub fn then<NextValue, H>(
2220 self,
2221 next: WritableKeyPath<SubValue, NextValue, H>,
2222 ) -> OptionalArcParkingMutexWritableKeyPathChain<Root, InnerValue, NextValue, F, impl for<'r> Fn(&'r mut InnerValue) -> &'r mut NextValue + 'static>
2223 where
2224 H: for<'r> Fn(&'r mut SubValue) -> &'r mut NextValue,
2225 G: 'static,
2226 H: 'static,
2227 InnerValue: 'static,
2228 SubValue: 'static,
2229 NextValue: 'static,
2230 {
2231 let first = self.inner_keypath;
2232 let second = next;
2233
2234 let composed = WritableKeyPath::new(move |inner: &mut InnerValue| {
2235 let sub = first.get_mut(inner);
2236 second.get_mut(sub)
2237 });
2238
2239 OptionalArcParkingMutexWritableKeyPathChain {
2240 outer_keypath: self.outer_keypath,
2241 inner_keypath: composed,
2242 }
2243 }
2244
2245 pub fn chain_optional<NextValue, H>(
2247 self,
2248 next: WritableOptionalKeyPath<SubValue, NextValue, H>,
2249 ) -> OptionalArcParkingMutexWritableOptionalKeyPathChain<Root, InnerValue, NextValue, F, impl for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut NextValue> + 'static>
2250 where
2251 H: for<'r> Fn(&'r mut SubValue) -> Option<&'r mut NextValue>,
2252 G: 'static,
2253 H: 'static,
2254 InnerValue: 'static,
2255 SubValue: 'static,
2256 NextValue: 'static,
2257 {
2258 let first = self.inner_keypath;
2259 let second = next;
2260
2261 let composed = WritableOptionalKeyPath::new(move |inner: &mut InnerValue| {
2262 let sub = first.get_mut(inner);
2263 second.get_mut(sub)
2264 });
2265
2266 OptionalArcParkingMutexWritableOptionalKeyPathChain {
2267 outer_keypath: self.outer_keypath,
2268 inner_keypath: composed,
2269 }
2270 }
2271}
2272
2273#[cfg(feature = "parking_lot")]
2275pub struct OptionalArcParkingMutexWritableOptionalKeyPathChain<Root, InnerValue, SubValue, F, G>
2276where
2277 F: for<'r> Fn(&'r Root) -> Option<&'r Arc<parking_lot::Mutex<InnerValue>>>,
2278 G: for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut SubValue>,
2279{
2280 outer_keypath: OptionalKeyPath<Root, Arc<parking_lot::Mutex<InnerValue>>, F>,
2281 inner_keypath: WritableOptionalKeyPath<InnerValue, SubValue, G>,
2282}
2283
2284#[cfg(feature = "parking_lot")]
2285impl<Root, InnerValue, SubValue, F, G> OptionalArcParkingMutexWritableOptionalKeyPathChain<Root, InnerValue, SubValue, F, G>
2286where
2287 F: for<'r> Fn(&'r Root) -> Option<&'r Arc<parking_lot::Mutex<InnerValue>>>,
2288 G: for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut SubValue>,
2289{
2290 pub fn get_mut<Callback, R>(self, container: &Root, callback: Callback) -> Option<R>
2292 where
2293 Callback: FnOnce(&mut SubValue) -> R,
2294 {
2295 self.outer_keypath.get(container).and_then(|arc_mutex_ref| {
2296 let mut guard = arc_mutex_ref.lock();
2297 self.inner_keypath.get_mut(&mut *guard).map(|value_ref| callback(value_ref))
2298 })
2299 }
2300
2301 pub fn then<NextValue, H>(
2303 self,
2304 next: WritableKeyPath<SubValue, NextValue, H>,
2305 ) -> OptionalArcParkingMutexWritableOptionalKeyPathChain<Root, InnerValue, NextValue, F, impl for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut NextValue> + 'static>
2306 where
2307 H: for<'r> Fn(&'r mut SubValue) -> &'r mut NextValue,
2308 G: 'static,
2309 H: 'static,
2310 InnerValue: 'static,
2311 SubValue: 'static,
2312 NextValue: 'static,
2313 {
2314 let first = self.inner_keypath;
2315 let second = next;
2316
2317 let composed = WritableOptionalKeyPath::new(move |inner: &mut InnerValue| {
2318 first.get_mut(inner).map(|sub| second.get_mut(sub))
2319 });
2320
2321 OptionalArcParkingMutexWritableOptionalKeyPathChain {
2322 outer_keypath: self.outer_keypath,
2323 inner_keypath: composed,
2324 }
2325 }
2326
2327 pub fn chain_optional<NextValue, H>(
2329 self,
2330 next: WritableOptionalKeyPath<SubValue, NextValue, H>,
2331 ) -> OptionalArcParkingMutexWritableOptionalKeyPathChain<Root, InnerValue, NextValue, F, impl for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut NextValue> + 'static>
2332 where
2333 H: for<'r> Fn(&'r mut SubValue) -> Option<&'r mut NextValue>,
2334 G: 'static,
2335 H: 'static,
2336 InnerValue: 'static,
2337 SubValue: 'static,
2338 NextValue: 'static,
2339 {
2340 let first = self.inner_keypath;
2341 let second = next;
2342
2343 let composed = WritableOptionalKeyPath::new(move |inner: &mut InnerValue| {
2344 first.get_mut(inner).and_then(|sub| second.get_mut(sub))
2345 });
2346
2347 OptionalArcParkingMutexWritableOptionalKeyPathChain {
2348 outer_keypath: self.outer_keypath,
2349 inner_keypath: composed,
2350 }
2351 }
2352}
2353
2354#[cfg(feature = "parking_lot")]
2358pub struct OptionalArcParkingRwLockKeyPathChain<Root, InnerValue, SubValue, F, G>
2359where
2360 F: for<'r> Fn(&'r Root) -> Option<&'r Arc<parking_lot::RwLock<InnerValue>>>,
2361 G: for<'r> Fn(&'r InnerValue) -> &'r SubValue,
2362{
2363 outer_keypath: OptionalKeyPath<Root, Arc<parking_lot::RwLock<InnerValue>>, F>,
2364 inner_keypath: KeyPath<InnerValue, SubValue, G>,
2365}
2366
2367#[cfg(feature = "parking_lot")]
2368impl<Root, InnerValue, SubValue, F, G> OptionalArcParkingRwLockKeyPathChain<Root, InnerValue, SubValue, F, G>
2369where
2370 F: for<'r> Fn(&'r Root) -> Option<&'r Arc<parking_lot::RwLock<InnerValue>>>,
2371 G: for<'r> Fn(&'r InnerValue) -> &'r SubValue,
2372{
2373 pub fn get<Callback>(self, container: &Root, callback: Callback) -> Option<()>
2375 where
2376 Callback: FnOnce(&SubValue) -> (),
2377 {
2378 self.outer_keypath.get(container).map(|arc_rwlock_ref| {
2379 let guard = arc_rwlock_ref.read();
2380 let value = self.inner_keypath.get(&*guard);
2381 callback(value)
2382 })
2383 }
2384
2385 pub fn then<NextValue, H>(
2387 self,
2388 next: KeyPath<SubValue, NextValue, H>,
2389 ) -> OptionalArcParkingRwLockKeyPathChain<Root, InnerValue, NextValue, F, impl for<'r> Fn(&'r InnerValue) -> &'r NextValue + 'static>
2390 where
2391 H: for<'r> Fn(&'r SubValue) -> &'r NextValue,
2392 G: 'static,
2393 H: 'static,
2394 InnerValue: 'static,
2395 SubValue: 'static,
2396 NextValue: 'static,
2397 {
2398 let first = self.inner_keypath;
2399 let second = next;
2400
2401 let composed = KeyPath::new(move |inner: &InnerValue| {
2402 let sub = first.get(inner);
2403 second.get(sub)
2404 });
2405
2406 OptionalArcParkingRwLockKeyPathChain {
2407 outer_keypath: self.outer_keypath,
2408 inner_keypath: composed,
2409 }
2410 }
2411
2412 pub fn chain_optional<NextValue, H>(
2414 self,
2415 next: OptionalKeyPath<SubValue, NextValue, H>,
2416 ) -> OptionalArcParkingRwLockOptionalKeyPathChain<Root, InnerValue, NextValue, F, impl for<'r> Fn(&'r InnerValue) -> Option<&'r NextValue> + 'static>
2417 where
2418 H: for<'r> Fn(&'r SubValue) -> Option<&'r NextValue>,
2419 G: 'static,
2420 H: 'static,
2421 InnerValue: 'static,
2422 SubValue: 'static,
2423 NextValue: 'static,
2424 {
2425 let first = self.inner_keypath;
2426 let second = next;
2427
2428 let composed = OptionalKeyPath::new(move |inner: &InnerValue| {
2429 let sub = first.get(inner);
2430 second.get(sub)
2431 });
2432
2433 OptionalArcParkingRwLockOptionalKeyPathChain {
2434 outer_keypath: self.outer_keypath,
2435 inner_keypath: composed,
2436 }
2437 }
2438}
2439
2440#[cfg(feature = "parking_lot")]
2442pub struct OptionalArcParkingRwLockOptionalKeyPathChain<Root, InnerValue, SubValue, F, G>
2443where
2444 F: for<'r> Fn(&'r Root) -> Option<&'r Arc<parking_lot::RwLock<InnerValue>>>,
2445 G: for<'r> Fn(&'r InnerValue) -> Option<&'r SubValue>,
2446{
2447 outer_keypath: OptionalKeyPath<Root, Arc<parking_lot::RwLock<InnerValue>>, F>,
2448 inner_keypath: OptionalKeyPath<InnerValue, SubValue, G>,
2449}
2450
2451#[cfg(feature = "parking_lot")]
2452impl<Root, InnerValue, SubValue, F, G> OptionalArcParkingRwLockOptionalKeyPathChain<Root, InnerValue, SubValue, F, G>
2453where
2454 F: for<'r> Fn(&'r Root) -> Option<&'r Arc<parking_lot::RwLock<InnerValue>>>,
2455 G: for<'r> Fn(&'r InnerValue) -> Option<&'r SubValue>,
2456{
2457 pub fn get<Callback>(self, container: &Root, callback: Callback) -> Option<()>
2459 where
2460 Callback: FnOnce(&SubValue) -> (),
2461 {
2462 self.outer_keypath.get(container).and_then(|arc_rwlock_ref| {
2463 let guard = arc_rwlock_ref.read();
2464 self.inner_keypath.get(&*guard).map(|value| callback(value))
2465 })
2466 }
2467
2468 pub fn then<NextValue, H>(
2470 self,
2471 next: KeyPath<SubValue, NextValue, H>,
2472 ) -> OptionalArcParkingRwLockOptionalKeyPathChain<Root, InnerValue, NextValue, F, impl for<'r> Fn(&'r InnerValue) -> Option<&'r NextValue> + 'static>
2473 where
2474 H: for<'r> Fn(&'r SubValue) -> &'r NextValue,
2475 G: 'static,
2476 H: 'static,
2477 InnerValue: 'static,
2478 SubValue: 'static,
2479 NextValue: 'static,
2480 {
2481 let first = self.inner_keypath;
2482 let second = next;
2483
2484 let composed = OptionalKeyPath::new(move |inner: &InnerValue| {
2485 first.get(inner).map(|sub| second.get(sub))
2486 });
2487
2488 OptionalArcParkingRwLockOptionalKeyPathChain {
2489 outer_keypath: self.outer_keypath,
2490 inner_keypath: composed,
2491 }
2492 }
2493
2494 pub fn chain_optional<NextValue, H>(
2496 self,
2497 next: OptionalKeyPath<SubValue, NextValue, H>,
2498 ) -> OptionalArcParkingRwLockOptionalKeyPathChain<Root, InnerValue, NextValue, F, impl for<'r> Fn(&'r InnerValue) -> Option<&'r NextValue> + 'static>
2499 where
2500 H: for<'r> Fn(&'r SubValue) -> Option<&'r NextValue>,
2501 G: 'static,
2502 H: 'static,
2503 InnerValue: 'static,
2504 SubValue: 'static,
2505 NextValue: 'static,
2506 {
2507 let first = self.inner_keypath;
2508 let second = next;
2509
2510 let composed = OptionalKeyPath::new(move |inner: &InnerValue| {
2511 first.get(inner).and_then(|sub| second.get(sub))
2512 });
2513
2514 OptionalArcParkingRwLockOptionalKeyPathChain {
2515 outer_keypath: self.outer_keypath,
2516 inner_keypath: composed,
2517 }
2518 }
2519}
2520
2521#[cfg(feature = "parking_lot")]
2523pub struct OptionalArcParkingRwLockWritableKeyPathChain<Root, InnerValue, SubValue, F, G>
2524where
2525 F: for<'r> Fn(&'r Root) -> Option<&'r Arc<parking_lot::RwLock<InnerValue>>>,
2526 G: for<'r> Fn(&'r mut InnerValue) -> &'r mut SubValue,
2527{
2528 outer_keypath: OptionalKeyPath<Root, Arc<parking_lot::RwLock<InnerValue>>, F>,
2529 inner_keypath: WritableKeyPath<InnerValue, SubValue, G>,
2530}
2531
2532#[cfg(feature = "parking_lot")]
2533impl<Root, InnerValue, SubValue, F, G> OptionalArcParkingRwLockWritableKeyPathChain<Root, InnerValue, SubValue, F, G>
2534where
2535 F: for<'r> Fn(&'r Root) -> Option<&'r Arc<parking_lot::RwLock<InnerValue>>>,
2536 G: for<'r> Fn(&'r mut InnerValue) -> &'r mut SubValue,
2537{
2538 pub fn get_mut<Callback, R>(self, container: &Root, callback: Callback) -> Option<R>
2540 where
2541 Callback: FnOnce(&mut SubValue) -> R,
2542 {
2543 self.outer_keypath.get(container).map(|arc_rwlock_ref| {
2544 let mut guard = arc_rwlock_ref.write();
2545 let value_ref = self.inner_keypath.get_mut(&mut *guard);
2546 callback(value_ref)
2547 })
2548 }
2549
2550 pub fn then<NextValue, H>(
2552 self,
2553 next: WritableKeyPath<SubValue, NextValue, H>,
2554 ) -> OptionalArcParkingRwLockWritableKeyPathChain<Root, InnerValue, NextValue, F, impl for<'r> Fn(&'r mut InnerValue) -> &'r mut NextValue + 'static>
2555 where
2556 H: for<'r> Fn(&'r mut SubValue) -> &'r mut NextValue,
2557 G: 'static,
2558 H: 'static,
2559 InnerValue: 'static,
2560 SubValue: 'static,
2561 NextValue: 'static,
2562 {
2563 let first = self.inner_keypath;
2564 let second = next;
2565
2566 let composed = WritableKeyPath::new(move |inner: &mut InnerValue| {
2567 let sub = first.get_mut(inner);
2568 second.get_mut(sub)
2569 });
2570
2571 OptionalArcParkingRwLockWritableKeyPathChain {
2572 outer_keypath: self.outer_keypath,
2573 inner_keypath: composed,
2574 }
2575 }
2576
2577 pub fn chain_optional<NextValue, H>(
2579 self,
2580 next: WritableOptionalKeyPath<SubValue, NextValue, H>,
2581 ) -> OptionalArcParkingRwLockWritableOptionalKeyPathChain<Root, InnerValue, NextValue, F, impl for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut NextValue> + 'static>
2582 where
2583 H: for<'r> Fn(&'r mut SubValue) -> Option<&'r mut NextValue>,
2584 G: 'static,
2585 H: 'static,
2586 InnerValue: 'static,
2587 SubValue: 'static,
2588 NextValue: 'static,
2589 {
2590 let first = self.inner_keypath;
2591 let second = next;
2592
2593 let composed = WritableOptionalKeyPath::new(move |inner: &mut InnerValue| {
2594 let sub = first.get_mut(inner);
2595 second.get_mut(sub)
2596 });
2597
2598 OptionalArcParkingRwLockWritableOptionalKeyPathChain {
2599 outer_keypath: self.outer_keypath,
2600 inner_keypath: composed,
2601 }
2602 }
2603}
2604
2605#[cfg(feature = "parking_lot")]
2607pub struct OptionalArcParkingRwLockWritableOptionalKeyPathChain<Root, InnerValue, SubValue, F, G>
2608where
2609 F: for<'r> Fn(&'r Root) -> Option<&'r Arc<parking_lot::RwLock<InnerValue>>>,
2610 G: for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut SubValue>,
2611{
2612 outer_keypath: OptionalKeyPath<Root, Arc<parking_lot::RwLock<InnerValue>>, F>,
2613 inner_keypath: WritableOptionalKeyPath<InnerValue, SubValue, G>,
2614}
2615
2616#[cfg(feature = "parking_lot")]
2617impl<Root, InnerValue, SubValue, F, G> OptionalArcParkingRwLockWritableOptionalKeyPathChain<Root, InnerValue, SubValue, F, G>
2618where
2619 F: for<'r> Fn(&'r Root) -> Option<&'r Arc<parking_lot::RwLock<InnerValue>>>,
2620 G: for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut SubValue>,
2621{
2622 pub fn get_mut<Callback, R>(self, container: &Root, callback: Callback) -> Option<R>
2624 where
2625 Callback: FnOnce(&mut SubValue) -> R,
2626 {
2627 self.outer_keypath.get(container).and_then(|arc_rwlock_ref| {
2628 let mut guard = arc_rwlock_ref.write();
2629 self.inner_keypath.get_mut(&mut *guard).map(|value_ref| callback(value_ref))
2630 })
2631 }
2632
2633 pub fn then<NextValue, H>(
2635 self,
2636 next: WritableKeyPath<SubValue, NextValue, H>,
2637 ) -> OptionalArcParkingRwLockWritableOptionalKeyPathChain<Root, InnerValue, NextValue, F, impl for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut NextValue> + 'static>
2638 where
2639 H: for<'r> Fn(&'r mut SubValue) -> &'r mut NextValue,
2640 G: 'static,
2641 H: 'static,
2642 InnerValue: 'static,
2643 SubValue: 'static,
2644 NextValue: 'static,
2645 {
2646 let first = self.inner_keypath;
2647 let second = next;
2648
2649 let composed = WritableOptionalKeyPath::new(move |inner: &mut InnerValue| {
2650 first.get_mut(inner).map(|sub| second.get_mut(sub))
2651 });
2652
2653 OptionalArcParkingRwLockWritableOptionalKeyPathChain {
2654 outer_keypath: self.outer_keypath,
2655 inner_keypath: composed,
2656 }
2657 }
2658
2659 pub fn chain_optional<NextValue, H>(
2661 self,
2662 next: WritableOptionalKeyPath<SubValue, NextValue, H>,
2663 ) -> OptionalArcParkingRwLockWritableOptionalKeyPathChain<Root, InnerValue, NextValue, F, impl for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut NextValue> + 'static>
2664 where
2665 H: for<'r> Fn(&'r mut SubValue) -> Option<&'r mut NextValue>,
2666 G: 'static,
2667 H: 'static,
2668 InnerValue: 'static,
2669 SubValue: 'static,
2670 NextValue: 'static,
2671 {
2672 let first = self.inner_keypath;
2673 let second = next;
2674
2675 let composed = WritableOptionalKeyPath::new(move |inner: &mut InnerValue| {
2676 first.get_mut(inner).and_then(|sub| second.get_mut(sub))
2677 });
2678
2679 OptionalArcParkingRwLockWritableOptionalKeyPathChain {
2680 outer_keypath: self.outer_keypath,
2681 inner_keypath: composed,
2682 }
2683 }
2684}
2685
2686#[cfg(feature = "tokio")]
2689use tokio::sync::{Mutex as TokioMutex, RwLock as TokioRwLock};
2690
2691#[cfg(feature = "tokio")]
2693pub struct ArcTokioMutexKeyPathChain<Root, MutexValue, InnerValue, SubValue, F, G>
2694where
2695 F: for<'r> Fn(&'r Root) -> &'r MutexValue,
2696 G: for<'r> Fn(&'r InnerValue) -> &'r SubValue,
2697{
2698 outer_keypath: KeyPath<Root, MutexValue, F>,
2699 inner_keypath: KeyPath<InnerValue, SubValue, G>,
2700}
2701
2702#[cfg(feature = "tokio")]
2703impl<Root, MutexValue, InnerValue, SubValue, F, G> ArcTokioMutexKeyPathChain<Root, MutexValue, InnerValue, SubValue, F, G>
2704where
2705 MutexValue: std::borrow::Borrow<Arc<TokioMutex<InnerValue>>>,
2706 F: for<'r> Fn(&'r Root) -> &'r MutexValue,
2707 G: for<'r> Fn(&'r InnerValue) -> &'r SubValue,
2708{
2709 pub async fn get<Callback>(self, container: &Root, callback: Callback)
2711 where
2712 Callback: FnOnce(&SubValue) -> (),
2713 {
2714 let arc_mutex_ref = self.outer_keypath.get(container).borrow();
2715 let guard = arc_mutex_ref.lock().await;
2716 let value = self.inner_keypath.get(&*guard);
2717 callback(value);
2718 }
2719
2720 pub fn then<NextValue, H>(
2722 self,
2723 next: KeyPath<SubValue, NextValue, H>,
2724 ) -> ArcTokioMutexKeyPathChain<Root, MutexValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r InnerValue) -> &'r NextValue + 'static>
2725 where
2726 H: for<'r> Fn(&'r SubValue) -> &'r NextValue,
2727 G: 'static,
2728 H: 'static,
2729 InnerValue: 'static,
2730 SubValue: 'static,
2731 NextValue: 'static,
2732 {
2733 let first = self.inner_keypath;
2734 let second = next;
2735
2736 let composed = KeyPath::new(move |inner: &InnerValue| {
2737 let sub = first.get(inner);
2738 second.get(sub)
2739 });
2740
2741 ArcTokioMutexKeyPathChain {
2742 outer_keypath: self.outer_keypath,
2743 inner_keypath: composed,
2744 }
2745 }
2746
2747 pub fn chain_optional<NextValue, H>(
2749 self,
2750 next: OptionalKeyPath<SubValue, NextValue, H>,
2751 ) -> ArcTokioMutexOptionalKeyPathChain<Root, MutexValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r InnerValue) -> Option<&'r NextValue> + 'static>
2752 where
2753 H: for<'r> Fn(&'r SubValue) -> Option<&'r NextValue>,
2754 G: 'static,
2755 H: 'static,
2756 InnerValue: 'static,
2757 SubValue: 'static,
2758 NextValue: 'static,
2759 {
2760 let first = self.inner_keypath;
2761 let second = next;
2762
2763 let composed = OptionalKeyPath::new(move |inner: &InnerValue| {
2764 let sub = first.get(inner);
2765 second.get(sub)
2766 });
2767
2768 ArcTokioMutexOptionalKeyPathChain {
2769 outer_keypath: self.outer_keypath,
2770 inner_keypath: composed,
2771 }
2772 }
2773}
2774
2775#[cfg(feature = "tokio")]
2777pub struct ArcTokioMutexOptionalKeyPathChain<Root, MutexValue, InnerValue, SubValue, F, G>
2778where
2779 F: for<'r> Fn(&'r Root) -> &'r MutexValue,
2780 G: for<'r> Fn(&'r InnerValue) -> Option<&'r SubValue>,
2781{
2782 outer_keypath: KeyPath<Root, MutexValue, F>,
2783 inner_keypath: OptionalKeyPath<InnerValue, SubValue, G>,
2784}
2785
2786#[cfg(feature = "tokio")]
2787impl<Root, MutexValue, InnerValue, SubValue, F, G> ArcTokioMutexOptionalKeyPathChain<Root, MutexValue, InnerValue, SubValue, F, G>
2788where
2789 MutexValue: std::borrow::Borrow<Arc<TokioMutex<InnerValue>>>,
2790 F: for<'r> Fn(&'r Root) -> &'r MutexValue,
2791 G: for<'r> Fn(&'r InnerValue) -> Option<&'r SubValue>,
2792{
2793 pub async fn get<Callback>(self, container: &Root, callback: Callback) -> Option<()>
2795 where
2796 Callback: FnOnce(&SubValue) -> (),
2797 {
2798 let arc_mutex_ref = self.outer_keypath.get(container).borrow();
2799 let guard = arc_mutex_ref.lock().await;
2800 self.inner_keypath.get(&*guard).map(|value| callback(value))
2801 }
2802
2803 pub fn then<NextValue, H>(
2805 self,
2806 next: KeyPath<SubValue, NextValue, H>,
2807 ) -> ArcTokioMutexOptionalKeyPathChain<Root, MutexValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r InnerValue) -> Option<&'r NextValue> + 'static>
2808 where
2809 H: for<'r> Fn(&'r SubValue) -> &'r NextValue,
2810 G: 'static,
2811 H: 'static,
2812 InnerValue: 'static,
2813 SubValue: 'static,
2814 NextValue: 'static,
2815 {
2816 let first = self.inner_keypath;
2817 let second = next;
2818
2819 let composed = OptionalKeyPath::new(move |inner: &InnerValue| {
2820 first.get(inner).map(|sub| second.get(sub))
2821 });
2822
2823 ArcTokioMutexOptionalKeyPathChain {
2824 outer_keypath: self.outer_keypath,
2825 inner_keypath: composed,
2826 }
2827 }
2828
2829 pub fn chain_optional<NextValue, H>(
2831 self,
2832 next: OptionalKeyPath<SubValue, NextValue, H>,
2833 ) -> ArcTokioMutexOptionalKeyPathChain<Root, MutexValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r InnerValue) -> Option<&'r NextValue> + 'static>
2834 where
2835 H: for<'r> Fn(&'r SubValue) -> Option<&'r NextValue>,
2836 G: 'static,
2837 H: 'static,
2838 InnerValue: 'static,
2839 SubValue: 'static,
2840 NextValue: 'static,
2841 {
2842 let first = self.inner_keypath;
2843 let second = next;
2844
2845 let composed = OptionalKeyPath::new(move |inner: &InnerValue| {
2846 first.get(inner).and_then(|sub| second.get(sub))
2847 });
2848
2849 ArcTokioMutexOptionalKeyPathChain {
2850 outer_keypath: self.outer_keypath,
2851 inner_keypath: composed,
2852 }
2853 }
2854}
2855
2856#[cfg(feature = "tokio")]
2858pub struct ArcTokioMutexWritableKeyPathChain<Root, MutexValue, InnerValue, SubValue, F, G>
2859where
2860 F: for<'r> Fn(&'r Root) -> &'r MutexValue,
2861 G: for<'r> Fn(&'r mut InnerValue) -> &'r mut SubValue,
2862{
2863 outer_keypath: KeyPath<Root, MutexValue, F>,
2864 inner_keypath: WritableKeyPath<InnerValue, SubValue, G>,
2865}
2866
2867#[cfg(feature = "tokio")]
2868impl<Root, MutexValue, InnerValue, SubValue, F, G> ArcTokioMutexWritableKeyPathChain<Root, MutexValue, InnerValue, SubValue, F, G>
2869where
2870 MutexValue: std::borrow::Borrow<Arc<TokioMutex<InnerValue>>>,
2871 F: for<'r> Fn(&'r Root) -> &'r MutexValue,
2872 G: for<'r> Fn(&'r mut InnerValue) -> &'r mut SubValue,
2873{
2874 pub async fn get_mut<Callback, R>(self, container: &Root, callback: Callback) -> R
2876 where
2877 Callback: FnOnce(&mut SubValue) -> R,
2878 {
2879 let arc_mutex_ref = self.outer_keypath.get(container).borrow();
2880 let mut guard = arc_mutex_ref.lock().await;
2881 let value_ref = self.inner_keypath.get_mut(&mut *guard);
2882 callback(value_ref)
2883 }
2884
2885 pub fn then<NextValue, H>(
2887 self,
2888 next: WritableKeyPath<SubValue, NextValue, H>,
2889 ) -> ArcTokioMutexWritableKeyPathChain<Root, MutexValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r mut InnerValue) -> &'r mut NextValue + 'static>
2890 where
2891 H: for<'r> Fn(&'r mut SubValue) -> &'r mut NextValue,
2892 G: 'static,
2893 H: 'static,
2894 InnerValue: 'static,
2895 SubValue: 'static,
2896 NextValue: 'static,
2897 {
2898 let first = self.inner_keypath;
2899 let second = next;
2900
2901 let composed = WritableKeyPath::new(move |inner: &mut InnerValue| {
2902 let sub = first.get_mut(inner);
2903 second.get_mut(sub)
2904 });
2905
2906 ArcTokioMutexWritableKeyPathChain {
2907 outer_keypath: self.outer_keypath,
2908 inner_keypath: composed,
2909 }
2910 }
2911
2912 pub fn chain_optional<NextValue, H>(
2914 self,
2915 next: WritableOptionalKeyPath<SubValue, NextValue, H>,
2916 ) -> ArcTokioMutexWritableOptionalKeyPathChain<Root, MutexValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut NextValue> + 'static>
2917 where
2918 H: for<'r> Fn(&'r mut SubValue) -> Option<&'r mut NextValue>,
2919 G: 'static,
2920 H: 'static,
2921 InnerValue: 'static,
2922 SubValue: 'static,
2923 NextValue: 'static,
2924 {
2925 let first = self.inner_keypath;
2926 let second = next;
2927
2928 let composed = WritableOptionalKeyPath::new(move |inner: &mut InnerValue| {
2929 let sub = first.get_mut(inner);
2930 second.get_mut(sub)
2931 });
2932
2933 ArcTokioMutexWritableOptionalKeyPathChain {
2934 outer_keypath: self.outer_keypath,
2935 inner_keypath: composed,
2936 }
2937 }
2938}
2939
2940#[cfg(feature = "tokio")]
2942pub struct ArcTokioMutexWritableOptionalKeyPathChain<Root, MutexValue, InnerValue, SubValue, F, G>
2943where
2944 F: for<'r> Fn(&'r Root) -> &'r MutexValue,
2945 G: for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut SubValue>,
2946{
2947 outer_keypath: KeyPath<Root, MutexValue, F>,
2948 inner_keypath: WritableOptionalKeyPath<InnerValue, SubValue, G>,
2949}
2950
2951#[cfg(feature = "tokio")]
2952impl<Root, MutexValue, InnerValue, SubValue, F, G> ArcTokioMutexWritableOptionalKeyPathChain<Root, MutexValue, InnerValue, SubValue, F, G>
2953where
2954 MutexValue: std::borrow::Borrow<Arc<TokioMutex<InnerValue>>>,
2955 F: for<'r> Fn(&'r Root) -> &'r MutexValue,
2956 G: for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut SubValue>,
2957{
2958 pub async fn get_mut<Callback, R>(self, container: &Root, callback: Callback) -> Option<R>
2960 where
2961 Callback: FnOnce(&mut SubValue) -> R,
2962 {
2963 let arc_mutex_ref = self.outer_keypath.get(container).borrow();
2964 let mut guard = arc_mutex_ref.lock().await;
2965 self.inner_keypath.get_mut(&mut *guard).map(|value_ref| callback(value_ref))
2966 }
2967
2968 pub fn then<NextValue, H>(
2970 self,
2971 next: WritableKeyPath<SubValue, NextValue, H>,
2972 ) -> ArcTokioMutexWritableOptionalKeyPathChain<Root, MutexValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut NextValue> + 'static>
2973 where
2974 H: for<'r> Fn(&'r mut SubValue) -> &'r mut NextValue,
2975 G: 'static,
2976 H: 'static,
2977 InnerValue: 'static,
2978 SubValue: 'static,
2979 NextValue: 'static,
2980 {
2981 let first = self.inner_keypath;
2982 let second = next;
2983
2984 let composed = WritableOptionalKeyPath::new(move |inner: &mut InnerValue| {
2985 first.get_mut(inner).map(|sub| second.get_mut(sub))
2986 });
2987
2988 ArcTokioMutexWritableOptionalKeyPathChain {
2989 outer_keypath: self.outer_keypath,
2990 inner_keypath: composed,
2991 }
2992 }
2993
2994 pub fn chain_optional<NextValue, H>(
2996 self,
2997 next: WritableOptionalKeyPath<SubValue, NextValue, H>,
2998 ) -> ArcTokioMutexWritableOptionalKeyPathChain<Root, MutexValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut NextValue> + 'static>
2999 where
3000 H: for<'r> Fn(&'r mut SubValue) -> Option<&'r mut NextValue>,
3001 G: 'static,
3002 H: 'static,
3003 InnerValue: 'static,
3004 SubValue: 'static,
3005 NextValue: 'static,
3006 {
3007 let first = self.inner_keypath;
3008 let second = next;
3009
3010 let composed = WritableOptionalKeyPath::new(move |inner: &mut InnerValue| {
3011 first.get_mut(inner).and_then(|sub| second.get_mut(sub))
3012 });
3013
3014 ArcTokioMutexWritableOptionalKeyPathChain {
3015 outer_keypath: self.outer_keypath,
3016 inner_keypath: composed,
3017 }
3018 }
3019}
3020
3021#[cfg(feature = "tokio")]
3023pub struct ArcTokioRwLockKeyPathChain<Root, RwLockValue, InnerValue, SubValue, F, G>
3024where
3025 RwLockValue: std::borrow::Borrow<Arc<TokioRwLock<InnerValue>>>,
3026 F: for<'r> Fn(&'r Root) -> &'r RwLockValue,
3027 G: for<'r> Fn(&'r InnerValue) -> &'r SubValue,
3028{
3029 outer_keypath: KeyPath<Root, RwLockValue, F>,
3030 inner_keypath: KeyPath<InnerValue, SubValue, G>,
3031}
3032
3033#[cfg(feature = "tokio")]
3034impl<Root, RwLockValue, InnerValue, SubValue, F, G> ArcTokioRwLockKeyPathChain<Root, RwLockValue, InnerValue, SubValue, F, G>
3035where
3036 RwLockValue: std::borrow::Borrow<Arc<TokioRwLock<InnerValue>>>,
3037 F: for<'r> Fn(&'r Root) -> &'r RwLockValue,
3038 G: for<'r> Fn(&'r InnerValue) -> &'r SubValue,
3039{
3040 pub async fn get<Callback>(self, container: &Root, callback: Callback)
3042 where
3043 Callback: FnOnce(&SubValue) -> (),
3044 {
3045 let arc_rwlock_ref = self.outer_keypath.get(container).borrow();
3046 let guard = arc_rwlock_ref.read().await;
3047 let value = self.inner_keypath.get(&*guard);
3048 callback(value);
3049 }
3050
3051 pub fn then<NextValue, H>(
3053 self,
3054 next: KeyPath<SubValue, NextValue, H>,
3055 ) -> ArcTokioRwLockKeyPathChain<Root, RwLockValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r InnerValue) -> &'r NextValue + 'static>
3056 where
3057 H: for<'r> Fn(&'r SubValue) -> &'r NextValue,
3058 G: 'static,
3059 H: 'static,
3060 InnerValue: 'static,
3061 SubValue: 'static,
3062 NextValue: 'static,
3063 {
3064 let first = self.inner_keypath;
3065 let second = next;
3066
3067 let composed = KeyPath::new(move |inner: &InnerValue| {
3068 let sub = first.get(inner);
3069 second.get(sub)
3070 });
3071
3072 ArcTokioRwLockKeyPathChain {
3073 outer_keypath: self.outer_keypath,
3074 inner_keypath: composed,
3075 }
3076 }
3077
3078 pub fn chain_optional<NextValue, H>(
3080 self,
3081 next: OptionalKeyPath<SubValue, NextValue, H>,
3082 ) -> ArcTokioRwLockOptionalKeyPathChain<Root, RwLockValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r InnerValue) -> Option<&'r NextValue> + 'static>
3083 where
3084 H: for<'r> Fn(&'r SubValue) -> Option<&'r NextValue>,
3085 G: 'static,
3086 H: 'static,
3087 InnerValue: 'static,
3088 SubValue: 'static,
3089 NextValue: 'static,
3090 {
3091 let first = self.inner_keypath;
3092 let second = next;
3093
3094 let composed = OptionalKeyPath::new(move |inner: &InnerValue| {
3095 let sub = first.get(inner);
3096 second.get(sub)
3097 });
3098
3099 ArcTokioRwLockOptionalKeyPathChain {
3100 outer_keypath: self.outer_keypath,
3101 inner_keypath: composed,
3102 }
3103 }
3104}
3105
3106#[cfg(feature = "tokio")]
3108pub struct ArcTokioRwLockOptionalKeyPathChain<Root, RwLockValue, InnerValue, SubValue, F, G>
3109where
3110 RwLockValue: std::borrow::Borrow<Arc<TokioRwLock<InnerValue>>>,
3111 F: for<'r> Fn(&'r Root) -> &'r RwLockValue,
3112 G: for<'r> Fn(&'r InnerValue) -> Option<&'r SubValue>,
3113{
3114 outer_keypath: KeyPath<Root, RwLockValue, F>,
3115 inner_keypath: OptionalKeyPath<InnerValue, SubValue, G>,
3116}
3117
3118#[cfg(feature = "tokio")]
3119impl<Root, RwLockValue, InnerValue, SubValue, F, G> ArcTokioRwLockOptionalKeyPathChain<Root, RwLockValue, InnerValue, SubValue, F, G>
3120where
3121 RwLockValue: std::borrow::Borrow<Arc<TokioRwLock<InnerValue>>>,
3122 F: for<'r> Fn(&'r Root) -> &'r RwLockValue,
3123 G: for<'r> Fn(&'r InnerValue) -> Option<&'r SubValue>,
3124{
3125 pub async fn get<Callback>(self, container: &Root, callback: Callback) -> Option<()>
3127 where
3128 Callback: FnOnce(&SubValue) -> (),
3129 {
3130 let arc_rwlock_ref = self.outer_keypath.get(container).borrow();
3131 let guard = arc_rwlock_ref.read().await;
3132 self.inner_keypath.get(&*guard).map(|value| callback(value))
3133 }
3134
3135 pub fn then<NextValue, H>(
3137 self,
3138 next: KeyPath<SubValue, NextValue, H>,
3139 ) -> ArcTokioRwLockOptionalKeyPathChain<Root, RwLockValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r InnerValue) -> Option<&'r NextValue> + 'static>
3140 where
3141 H: for<'r> Fn(&'r SubValue) -> &'r NextValue,
3142 G: 'static,
3143 H: 'static,
3144 InnerValue: 'static,
3145 SubValue: 'static,
3146 NextValue: 'static,
3147 {
3148 let first = self.inner_keypath;
3149 let second = next;
3150
3151 let composed = OptionalKeyPath::new(move |inner: &InnerValue| {
3152 first.get(inner).map(|sub| second.get(sub))
3153 });
3154
3155 ArcTokioRwLockOptionalKeyPathChain {
3156 outer_keypath: self.outer_keypath,
3157 inner_keypath: composed,
3158 }
3159 }
3160
3161 pub fn chain_optional<NextValue, H>(
3163 self,
3164 next: OptionalKeyPath<SubValue, NextValue, H>,
3165 ) -> ArcTokioRwLockOptionalKeyPathChain<Root, RwLockValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r InnerValue) -> Option<&'r NextValue> + 'static>
3166 where
3167 H: for<'r> Fn(&'r SubValue) -> Option<&'r NextValue>,
3168 G: 'static,
3169 H: 'static,
3170 InnerValue: 'static,
3171 SubValue: 'static,
3172 NextValue: 'static,
3173 {
3174 let first = self.inner_keypath;
3175 let second = next;
3176
3177 let composed = OptionalKeyPath::new(move |inner: &InnerValue| {
3178 first.get(inner).and_then(|sub| second.get(sub))
3179 });
3180
3181 ArcTokioRwLockOptionalKeyPathChain {
3182 outer_keypath: self.outer_keypath,
3183 inner_keypath: composed,
3184 }
3185 }
3186}
3187
3188#[cfg(feature = "tokio")]
3190pub struct ArcTokioRwLockWritableKeyPathChain<Root, RwLockValue, InnerValue, SubValue, F, G>
3191where
3192 RwLockValue: std::borrow::Borrow<Arc<TokioRwLock<InnerValue>>>,
3193 F: for<'r> Fn(&'r Root) -> &'r RwLockValue,
3194 G: for<'r> Fn(&'r mut InnerValue) -> &'r mut SubValue,
3195{
3196 outer_keypath: KeyPath<Root, RwLockValue, F>,
3197 inner_keypath: WritableKeyPath<InnerValue, SubValue, G>,
3198}
3199
3200#[cfg(feature = "tokio")]
3201impl<Root, RwLockValue, InnerValue, SubValue, F, G> ArcTokioRwLockWritableKeyPathChain<Root, RwLockValue, InnerValue, SubValue, F, G>
3202where
3203 RwLockValue: std::borrow::Borrow<Arc<TokioRwLock<InnerValue>>>,
3204 F: for<'r> Fn(&'r Root) -> &'r RwLockValue,
3205 G: for<'r> Fn(&'r mut InnerValue) -> &'r mut SubValue,
3206{
3207 pub async fn get_mut<Callback, R>(self, container: &Root, callback: Callback) -> R
3209 where
3210 Callback: FnOnce(&mut SubValue) -> R,
3211 {
3212 let arc_rwlock_ref = self.outer_keypath.get(container).borrow();
3213 let mut guard = arc_rwlock_ref.write().await;
3214 let value_ref = self.inner_keypath.get_mut(&mut *guard);
3215 callback(value_ref)
3216 }
3217
3218 pub fn then<NextValue, H>(
3220 self,
3221 next: WritableKeyPath<SubValue, NextValue, H>,
3222 ) -> ArcTokioRwLockWritableKeyPathChain<Root, RwLockValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r mut InnerValue) -> &'r mut NextValue + 'static>
3223 where
3224 H: for<'r> Fn(&'r mut SubValue) -> &'r mut NextValue,
3225 G: 'static,
3226 H: 'static,
3227 InnerValue: 'static,
3228 SubValue: 'static,
3229 NextValue: 'static,
3230 {
3231 let first = self.inner_keypath;
3232 let second = next;
3233
3234 let composed = WritableKeyPath::new(move |inner: &mut InnerValue| {
3235 let sub = first.get_mut(inner);
3236 second.get_mut(sub)
3237 });
3238
3239 ArcTokioRwLockWritableKeyPathChain {
3240 outer_keypath: self.outer_keypath,
3241 inner_keypath: composed,
3242 }
3243 }
3244
3245 pub fn chain_optional<NextValue, H>(
3247 self,
3248 next: WritableOptionalKeyPath<SubValue, NextValue, H>,
3249 ) -> ArcTokioRwLockWritableOptionalKeyPathChain<Root, RwLockValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut NextValue> + 'static>
3250 where
3251 H: for<'r> Fn(&'r mut SubValue) -> Option<&'r mut NextValue>,
3252 G: 'static,
3253 H: 'static,
3254 InnerValue: 'static,
3255 SubValue: 'static,
3256 NextValue: 'static,
3257 {
3258 let first = self.inner_keypath;
3259 let second = next;
3260
3261 let composed = WritableOptionalKeyPath::new(move |inner: &mut InnerValue| {
3262 let sub = first.get_mut(inner);
3263 second.get_mut(sub)
3264 });
3265
3266 ArcTokioRwLockWritableOptionalKeyPathChain {
3267 outer_keypath: self.outer_keypath,
3268 inner_keypath: composed,
3269 }
3270 }
3271}
3272
3273#[cfg(feature = "tokio")]
3275pub struct ArcTokioRwLockWritableOptionalKeyPathChain<Root, RwLockValue, InnerValue, SubValue, F, G>
3276where
3277 RwLockValue: std::borrow::Borrow<Arc<TokioRwLock<InnerValue>>>,
3278 F: for<'r> Fn(&'r Root) -> &'r RwLockValue,
3279 G: for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut SubValue>,
3280{
3281 outer_keypath: KeyPath<Root, RwLockValue, F>,
3282 inner_keypath: WritableOptionalKeyPath<InnerValue, SubValue, G>,
3283}
3284
3285#[cfg(feature = "tokio")]
3286impl<Root, RwLockValue, InnerValue, SubValue, F, G> ArcTokioRwLockWritableOptionalKeyPathChain<Root, RwLockValue, InnerValue, SubValue, F, G>
3287where
3288 RwLockValue: std::borrow::Borrow<Arc<TokioRwLock<InnerValue>>>,
3289 F: for<'r> Fn(&'r Root) -> &'r RwLockValue,
3290 G: for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut SubValue>,
3291{
3292 pub async fn get_mut<Callback, R>(self, container: &Root, callback: Callback) -> Option<R>
3294 where
3295 Callback: FnOnce(&mut SubValue) -> R,
3296 {
3297 let arc_rwlock_ref = self.outer_keypath.get(container).borrow();
3298 let mut guard = arc_rwlock_ref.write().await;
3299 self.inner_keypath.get_mut(&mut *guard).map(|value_ref| callback(value_ref))
3300 }
3301
3302 pub fn then<NextValue, H>(
3304 self,
3305 next: WritableKeyPath<SubValue, NextValue, H>,
3306 ) -> ArcTokioRwLockWritableOptionalKeyPathChain<Root, RwLockValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut NextValue> + 'static>
3307 where
3308 H: for<'r> Fn(&'r mut SubValue) -> &'r mut NextValue,
3309 G: 'static,
3310 H: 'static,
3311 InnerValue: 'static,
3312 SubValue: 'static,
3313 NextValue: 'static,
3314 {
3315 let first = self.inner_keypath;
3316 let second = next;
3317
3318 let composed = WritableOptionalKeyPath::new(move |inner: &mut InnerValue| {
3319 first.get_mut(inner).map(|sub| second.get_mut(sub))
3320 });
3321
3322 ArcTokioRwLockWritableOptionalKeyPathChain {
3323 outer_keypath: self.outer_keypath,
3324 inner_keypath: composed,
3325 }
3326 }
3327
3328 pub fn chain_optional<NextValue, H>(
3330 self,
3331 next: WritableOptionalKeyPath<SubValue, NextValue, H>,
3332 ) -> ArcTokioRwLockWritableOptionalKeyPathChain<Root, RwLockValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut NextValue> + 'static>
3333 where
3334 H: for<'r> Fn(&'r mut SubValue) -> Option<&'r mut NextValue>,
3335 G: 'static,
3336 H: 'static,
3337 InnerValue: 'static,
3338 SubValue: 'static,
3339 NextValue: 'static,
3340 {
3341 let first = self.inner_keypath;
3342 let second = next;
3343
3344 let composed = WritableOptionalKeyPath::new(move |inner: &mut InnerValue| {
3345 first.get_mut(inner).and_then(|sub| second.get_mut(sub))
3346 });
3347
3348 ArcTokioRwLockWritableOptionalKeyPathChain {
3349 outer_keypath: self.outer_keypath,
3350 inner_keypath: composed,
3351 }
3352 }
3353}
3354
3355#[cfg(feature = "tokio")]
3359pub struct OptionalArcTokioMutexKeyPathChain<Root, MutexValue, InnerValue, SubValue, F, G>
3360where
3361 MutexValue: std::borrow::Borrow<Arc<TokioMutex<InnerValue>>>,
3362 F: for<'r> Fn(&'r Root) -> Option<&'r MutexValue>,
3363 G: for<'r> Fn(&'r InnerValue) -> &'r SubValue,
3364{
3365 outer_keypath: OptionalKeyPath<Root, MutexValue, F>,
3366 inner_keypath: KeyPath<InnerValue, SubValue, G>,
3367}
3368
3369#[cfg(feature = "tokio")]
3370impl<Root, MutexValue, InnerValue, SubValue, F, G> OptionalArcTokioMutexKeyPathChain<Root, MutexValue, InnerValue, SubValue, F, G>
3371where
3372 MutexValue: std::borrow::Borrow<Arc<TokioMutex<InnerValue>>>,
3373 F: for<'r> Fn(&'r Root) -> Option<&'r MutexValue>,
3374 G: for<'r> Fn(&'r InnerValue) -> &'r SubValue,
3375{
3376 pub async fn get<Callback>(self, container: &Root, callback: Callback) -> Option<()>
3378 where
3379 Callback: FnOnce(&SubValue) -> (),
3380 {
3381 if let Some(arc_mutex_ref) = self.outer_keypath.get(container) { let arc_mutex_ref = arc_mutex_ref.borrow();
3382 let guard = arc_mutex_ref.lock().await;
3383 let value = self.inner_keypath.get(&*guard);
3384 callback(value);
3385 Some(())
3386 } else {
3387 None
3388 }
3389 }
3390
3391 pub fn then<NextValue, H>(
3393 self,
3394 next: KeyPath<SubValue, NextValue, H>,
3395 ) -> OptionalArcTokioMutexKeyPathChain<Root, MutexValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r InnerValue) -> &'r NextValue + 'static>
3396 where
3397 H: for<'r> Fn(&'r SubValue) -> &'r NextValue,
3398 G: 'static,
3399 H: 'static,
3400 InnerValue: 'static,
3401 SubValue: 'static,
3402 NextValue: 'static,
3403 {
3404 let first = self.inner_keypath;
3405 let second = next;
3406
3407 let composed = KeyPath::new(move |inner: &InnerValue| {
3408 let sub = first.get(inner);
3409 second.get(sub)
3410 });
3411
3412 OptionalArcTokioMutexKeyPathChain {
3413 outer_keypath: self.outer_keypath,
3414 inner_keypath: composed,
3415 }
3416 }
3417
3418 pub fn chain_optional<NextValue, H>(
3420 self,
3421 next: OptionalKeyPath<SubValue, NextValue, H>,
3422 ) -> OptionalArcTokioMutexOptionalKeyPathChain<Root, MutexValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r InnerValue) -> Option<&'r NextValue> + 'static>
3423 where
3424 H: for<'r> Fn(&'r SubValue) -> Option<&'r NextValue>,
3425 G: 'static,
3426 H: 'static,
3427 InnerValue: 'static,
3428 SubValue: 'static,
3429 NextValue: 'static,
3430 {
3431 let first = self.inner_keypath;
3432 let second = next;
3433
3434 let composed = OptionalKeyPath::new(move |inner: &InnerValue| {
3435 let sub = first.get(inner);
3436 second.get(sub)
3437 });
3438
3439 OptionalArcTokioMutexOptionalKeyPathChain {
3440 outer_keypath: self.outer_keypath,
3441 inner_keypath: composed,
3442 }
3443 }
3444}
3445
3446#[cfg(feature = "tokio")]
3448pub struct OptionalArcTokioMutexOptionalKeyPathChain<Root, MutexValue, InnerValue, SubValue, F, G>
3449where
3450 MutexValue: std::borrow::Borrow<Arc<TokioMutex<InnerValue>>>,
3451 F: for<'r> Fn(&'r Root) -> Option<&'r MutexValue>,
3452 G: for<'r> Fn(&'r InnerValue) -> Option<&'r SubValue>,
3453{
3454 outer_keypath: OptionalKeyPath<Root, MutexValue, F>,
3455 inner_keypath: OptionalKeyPath<InnerValue, SubValue, G>,
3456}
3457
3458#[cfg(feature = "tokio")]
3459impl<Root, MutexValue, InnerValue, SubValue, F, G> OptionalArcTokioMutexOptionalKeyPathChain<Root, MutexValue, InnerValue, SubValue, F, G>
3460where
3461 MutexValue: std::borrow::Borrow<Arc<TokioMutex<InnerValue>>>,
3462 F: for<'r> Fn(&'r Root) -> Option<&'r MutexValue>,
3463 G: for<'r> Fn(&'r InnerValue) -> Option<&'r SubValue>,
3464{
3465 pub async fn get<Callback>(self, container: &Root, callback: Callback) -> Option<()>
3467 where
3468 Callback: FnOnce(&SubValue) -> (),
3469 {
3470 if let Some(arc_mutex_ref) = self.outer_keypath.get(container) { let arc_mutex_ref = arc_mutex_ref.borrow();
3471 let guard = arc_mutex_ref.lock().await;
3472 self.inner_keypath.get(&*guard).map(|value| callback(value))
3473 } else {
3474 None
3475 }
3476 }
3477
3478 pub fn then<NextValue, H>(
3480 self,
3481 next: KeyPath<SubValue, NextValue, H>,
3482 ) -> OptionalArcTokioMutexOptionalKeyPathChain<Root, MutexValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r InnerValue) -> Option<&'r NextValue> + 'static>
3483 where
3484 H: for<'r> Fn(&'r SubValue) -> &'r NextValue,
3485 G: 'static,
3486 H: 'static,
3487 InnerValue: 'static,
3488 SubValue: 'static,
3489 NextValue: 'static,
3490 {
3491 let first = self.inner_keypath;
3492 let second = next;
3493
3494 let composed = OptionalKeyPath::new(move |inner: &InnerValue| {
3495 first.get(inner).map(|sub| second.get(sub))
3496 });
3497
3498 OptionalArcTokioMutexOptionalKeyPathChain {
3499 outer_keypath: self.outer_keypath,
3500 inner_keypath: composed,
3501 }
3502 }
3503
3504 pub fn chain_optional<NextValue, H>(
3506 self,
3507 next: OptionalKeyPath<SubValue, NextValue, H>,
3508 ) -> OptionalArcTokioMutexOptionalKeyPathChain<Root, MutexValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r InnerValue) -> Option<&'r NextValue> + 'static>
3509 where
3510 H: for<'r> Fn(&'r SubValue) -> Option<&'r NextValue>,
3511 G: 'static,
3512 H: 'static,
3513 InnerValue: 'static,
3514 SubValue: 'static,
3515 NextValue: 'static,
3516 {
3517 let first = self.inner_keypath;
3518 let second = next;
3519
3520 let composed = OptionalKeyPath::new(move |inner: &InnerValue| {
3521 first.get(inner).and_then(|sub| second.get(sub))
3522 });
3523
3524 OptionalArcTokioMutexOptionalKeyPathChain {
3525 outer_keypath: self.outer_keypath,
3526 inner_keypath: composed,
3527 }
3528 }
3529}
3530
3531#[cfg(feature = "tokio")]
3533pub struct OptionalArcTokioMutexWritableKeyPathChain<Root, MutexValue, InnerValue, SubValue, F, G>
3534where
3535 MutexValue: std::borrow::Borrow<Arc<TokioMutex<InnerValue>>>,
3536 F: for<'r> Fn(&'r Root) -> Option<&'r MutexValue>,
3537 G: for<'r> Fn(&'r mut InnerValue) -> &'r mut SubValue,
3538{
3539 outer_keypath: OptionalKeyPath<Root, MutexValue, F>,
3540 inner_keypath: WritableKeyPath<InnerValue, SubValue, G>,
3541}
3542
3543#[cfg(feature = "tokio")]
3544impl<Root, MutexValue, InnerValue, SubValue, F, G> OptionalArcTokioMutexWritableKeyPathChain<Root, MutexValue, InnerValue, SubValue, F, G>
3545where
3546 MutexValue: std::borrow::Borrow<Arc<TokioMutex<InnerValue>>>,
3547 F: for<'r> Fn(&'r Root) -> Option<&'r MutexValue>,
3548 G: for<'r> Fn(&'r mut InnerValue) -> &'r mut SubValue,
3549{
3550 pub async fn get_mut<Callback, R>(self, container: &Root, callback: Callback) -> Option<R>
3552 where
3553 Callback: FnOnce(&mut SubValue) -> R,
3554 {
3555 if let Some(arc_mutex_ref) = self.outer_keypath.get(container) { let arc_mutex_ref = arc_mutex_ref.borrow();
3556 let mut guard = arc_mutex_ref.lock().await;
3557 let value_ref = self.inner_keypath.get_mut(&mut *guard);
3558 Some(callback(value_ref))
3559 } else {
3560 None
3561 }
3562 }
3563
3564 pub fn then<NextValue, H>(
3566 self,
3567 next: WritableKeyPath<SubValue, NextValue, H>,
3568 ) -> OptionalArcTokioMutexWritableKeyPathChain<Root, MutexValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r mut InnerValue) -> &'r mut NextValue + 'static>
3569 where
3570 H: for<'r> Fn(&'r mut SubValue) -> &'r mut NextValue,
3571 G: 'static,
3572 H: 'static,
3573 InnerValue: 'static,
3574 SubValue: 'static,
3575 NextValue: 'static,
3576 {
3577 let first = self.inner_keypath;
3578 let second = next;
3579
3580 let composed = WritableKeyPath::new(move |inner: &mut InnerValue| {
3581 let sub = first.get_mut(inner);
3582 second.get_mut(sub)
3583 });
3584
3585 OptionalArcTokioMutexWritableKeyPathChain {
3586 outer_keypath: self.outer_keypath,
3587 inner_keypath: composed,
3588 }
3589 }
3590
3591 pub fn chain_optional<NextValue, H>(
3593 self,
3594 next: WritableOptionalKeyPath<SubValue, NextValue, H>,
3595 ) -> OptionalArcTokioMutexWritableOptionalKeyPathChain<Root, MutexValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut NextValue> + 'static>
3596 where
3597 H: for<'r> Fn(&'r mut SubValue) -> Option<&'r mut NextValue>,
3598 G: 'static,
3599 H: 'static,
3600 InnerValue: 'static,
3601 SubValue: 'static,
3602 NextValue: 'static,
3603 {
3604 let first = self.inner_keypath;
3605 let second = next;
3606
3607 let composed = WritableOptionalKeyPath::new(move |inner: &mut InnerValue| {
3608 let sub = first.get_mut(inner);
3609 second.get_mut(sub)
3610 });
3611
3612 OptionalArcTokioMutexWritableOptionalKeyPathChain {
3613 outer_keypath: self.outer_keypath,
3614 inner_keypath: composed,
3615 }
3616 }
3617}
3618
3619#[cfg(feature = "tokio")]
3621pub struct OptionalArcTokioMutexWritableOptionalKeyPathChain<Root, MutexValue, InnerValue, SubValue, F, G>
3622where
3623 MutexValue: std::borrow::Borrow<Arc<TokioMutex<InnerValue>>>,
3624 F: for<'r> Fn(&'r Root) -> Option<&'r MutexValue>,
3625 G: for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut SubValue>,
3626{
3627 outer_keypath: OptionalKeyPath<Root, MutexValue, F>,
3628 inner_keypath: WritableOptionalKeyPath<InnerValue, SubValue, G>,
3629}
3630
3631#[cfg(feature = "tokio")]
3632impl<Root, MutexValue, InnerValue, SubValue, F, G> OptionalArcTokioMutexWritableOptionalKeyPathChain<Root, MutexValue, InnerValue, SubValue, F, G>
3633where
3634 MutexValue: std::borrow::Borrow<Arc<TokioMutex<InnerValue>>>,
3635 F: for<'r> Fn(&'r Root) -> Option<&'r MutexValue>,
3636 G: for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut SubValue>,
3637{
3638 pub async fn get_mut<Callback, R>(self, container: &Root, callback: Callback) -> Option<R>
3640 where
3641 Callback: FnOnce(&mut SubValue) -> R,
3642 {
3643 if let Some(arc_mutex_ref) = self.outer_keypath.get(container) { let arc_mutex_ref = arc_mutex_ref.borrow();
3644 let mut guard = arc_mutex_ref.lock().await;
3645 self.inner_keypath.get_mut(&mut *guard).map(|value_ref| callback(value_ref))
3646 } else {
3647 None
3648 }
3649 }
3650
3651 pub fn then<NextValue, H>(
3653 self,
3654 next: WritableKeyPath<SubValue, NextValue, H>,
3655 ) -> OptionalArcTokioMutexWritableOptionalKeyPathChain<Root, MutexValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut NextValue> + 'static>
3656 where
3657 H: for<'r> Fn(&'r mut SubValue) -> &'r mut NextValue,
3658 G: 'static,
3659 H: 'static,
3660 InnerValue: 'static,
3661 SubValue: 'static,
3662 NextValue: 'static,
3663 {
3664 let first = self.inner_keypath;
3665 let second = next;
3666
3667 let composed = WritableOptionalKeyPath::new(move |inner: &mut InnerValue| {
3668 first.get_mut(inner).map(|sub| second.get_mut(sub))
3669 });
3670
3671 OptionalArcTokioMutexWritableOptionalKeyPathChain {
3672 outer_keypath: self.outer_keypath,
3673 inner_keypath: composed,
3674 }
3675 }
3676
3677 pub fn chain_optional<NextValue, H>(
3679 self,
3680 next: WritableOptionalKeyPath<SubValue, NextValue, H>,
3681 ) -> OptionalArcTokioMutexWritableOptionalKeyPathChain<Root, MutexValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut NextValue> + 'static>
3682 where
3683 H: for<'r> Fn(&'r mut SubValue) -> Option<&'r mut NextValue>,
3684 G: 'static,
3685 H: 'static,
3686 InnerValue: 'static,
3687 SubValue: 'static,
3688 NextValue: 'static,
3689 {
3690 let first = self.inner_keypath;
3691 let second = next;
3692
3693 let composed = WritableOptionalKeyPath::new(move |inner: &mut InnerValue| {
3694 first.get_mut(inner).and_then(|sub| second.get_mut(sub))
3695 });
3696
3697 OptionalArcTokioMutexWritableOptionalKeyPathChain {
3698 outer_keypath: self.outer_keypath,
3699 inner_keypath: composed,
3700 }
3701 }
3702}
3703
3704#[cfg(feature = "tokio")]
3708pub struct OptionalArcTokioRwLockKeyPathChain<Root, RwLockValue, InnerValue, SubValue, F, G>
3709where
3710 RwLockValue: std::borrow::Borrow<Arc<TokioRwLock<InnerValue>>>,
3711 F: for<'r> Fn(&'r Root) -> Option<&'r RwLockValue>,
3712 G: for<'r> Fn(&'r InnerValue) -> &'r SubValue,
3713{
3714 outer_keypath: OptionalKeyPath<Root, RwLockValue, F>,
3715 inner_keypath: KeyPath<InnerValue, SubValue, G>,
3716}
3717
3718#[cfg(feature = "tokio")]
3719impl<Root, RwLockValue, InnerValue, SubValue, F, G> OptionalArcTokioRwLockKeyPathChain<Root, RwLockValue, InnerValue, SubValue, F, G>
3720where
3721 RwLockValue: std::borrow::Borrow<Arc<TokioRwLock<InnerValue>>>,
3722 F: for<'r> Fn(&'r Root) -> Option<&'r RwLockValue>,
3723 G: for<'r> Fn(&'r InnerValue) -> &'r SubValue,
3724{
3725 pub async fn get<Callback>(self, container: &Root, callback: Callback) -> Option<()>
3727 where
3728 Callback: FnOnce(&SubValue) -> (),
3729 {
3730 if let Some(arc_rwlock_ref) = self.outer_keypath.get(container) { let arc_rwlock_ref = arc_rwlock_ref.borrow();
3731 let guard = arc_rwlock_ref.read().await;
3732 let value = self.inner_keypath.get(&*guard);
3733 callback(value);
3734 Some(())
3735 } else {
3736 None
3737 }
3738 }
3739
3740 pub fn then<NextValue, H>(
3742 self,
3743 next: KeyPath<SubValue, NextValue, H>,
3744 ) -> OptionalArcTokioRwLockKeyPathChain<Root, RwLockValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r InnerValue) -> &'r NextValue + 'static>
3745 where
3746 H: for<'r> Fn(&'r SubValue) -> &'r NextValue,
3747 G: 'static,
3748 H: 'static,
3749 InnerValue: 'static,
3750 SubValue: 'static,
3751 NextValue: 'static,
3752 {
3753 let first = self.inner_keypath;
3754 let second = next;
3755
3756 let composed = KeyPath::new(move |inner: &InnerValue| {
3757 let sub = first.get(inner);
3758 second.get(sub)
3759 });
3760
3761 OptionalArcTokioRwLockKeyPathChain {
3762 outer_keypath: self.outer_keypath,
3763 inner_keypath: composed,
3764 }
3765 }
3766
3767 pub fn chain_optional<NextValue, H>(
3769 self,
3770 next: OptionalKeyPath<SubValue, NextValue, H>,
3771 ) -> OptionalArcTokioRwLockOptionalKeyPathChain<Root, RwLockValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r InnerValue) -> Option<&'r NextValue> + 'static>
3772 where
3773 H: for<'r> Fn(&'r SubValue) -> Option<&'r NextValue>,
3774 G: 'static,
3775 H: 'static,
3776 InnerValue: 'static,
3777 SubValue: 'static,
3778 NextValue: 'static,
3779 {
3780 let first = self.inner_keypath;
3781 let second = next;
3782
3783 let composed = OptionalKeyPath::new(move |inner: &InnerValue| {
3784 let sub = first.get(inner);
3785 second.get(sub)
3786 });
3787
3788 OptionalArcTokioRwLockOptionalKeyPathChain {
3789 outer_keypath: self.outer_keypath,
3790 inner_keypath: composed,
3791 }
3792 }
3793}
3794
3795#[cfg(feature = "tokio")]
3797pub struct OptionalArcTokioRwLockOptionalKeyPathChain<Root, RwLockValue, InnerValue, SubValue, F, G>
3798where
3799 RwLockValue: std::borrow::Borrow<Arc<TokioRwLock<InnerValue>>>,
3800 F: for<'r> Fn(&'r Root) -> Option<&'r RwLockValue>,
3801 G: for<'r> Fn(&'r InnerValue) -> Option<&'r SubValue>,
3802{
3803 outer_keypath: OptionalKeyPath<Root, RwLockValue, F>,
3804 inner_keypath: OptionalKeyPath<InnerValue, SubValue, G>,
3805}
3806
3807#[cfg(feature = "tokio")]
3808impl<Root, RwLockValue, InnerValue, SubValue, F, G> OptionalArcTokioRwLockOptionalKeyPathChain<Root, RwLockValue, InnerValue, SubValue, F, G>
3809where
3810 RwLockValue: std::borrow::Borrow<Arc<TokioRwLock<InnerValue>>>,
3811 F: for<'r> Fn(&'r Root) -> Option<&'r RwLockValue>,
3812 G: for<'r> Fn(&'r InnerValue) -> Option<&'r SubValue>,
3813{
3814 pub async fn get<Callback>(self, container: &Root, callback: Callback) -> Option<()>
3816 where
3817 Callback: FnOnce(&SubValue) -> (),
3818 {
3819 if let Some(arc_rwlock_ref) = self.outer_keypath.get(container) { let arc_rwlock_ref = arc_rwlock_ref.borrow();
3820 let guard = arc_rwlock_ref.read().await;
3821 self.inner_keypath.get(&*guard).map(|value| callback(value))
3822 } else {
3823 None
3824 }
3825 }
3826
3827 pub fn then<NextValue, H>(
3829 self,
3830 next: KeyPath<SubValue, NextValue, H>,
3831 ) -> OptionalArcTokioRwLockOptionalKeyPathChain<Root, RwLockValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r InnerValue) -> Option<&'r NextValue> + 'static>
3832 where
3833 H: for<'r> Fn(&'r SubValue) -> &'r NextValue,
3834 G: 'static,
3835 H: 'static,
3836 InnerValue: 'static,
3837 SubValue: 'static,
3838 NextValue: 'static,
3839 {
3840 let first = self.inner_keypath;
3841 let second = next;
3842
3843 let composed = OptionalKeyPath::new(move |inner: &InnerValue| {
3844 first.get(inner).map(|sub| second.get(sub))
3845 });
3846
3847 OptionalArcTokioRwLockOptionalKeyPathChain {
3848 outer_keypath: self.outer_keypath,
3849 inner_keypath: composed,
3850 }
3851 }
3852
3853 pub fn chain_optional<NextValue, H>(
3855 self,
3856 next: OptionalKeyPath<SubValue, NextValue, H>,
3857 ) -> OptionalArcTokioRwLockOptionalKeyPathChain<Root, RwLockValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r InnerValue) -> Option<&'r NextValue> + 'static>
3858 where
3859 H: for<'r> Fn(&'r SubValue) -> Option<&'r NextValue>,
3860 G: 'static,
3861 H: 'static,
3862 InnerValue: 'static,
3863 SubValue: 'static,
3864 NextValue: 'static,
3865 {
3866 let first = self.inner_keypath;
3867 let second = next;
3868
3869 let composed = OptionalKeyPath::new(move |inner: &InnerValue| {
3870 first.get(inner).and_then(|sub| second.get(sub))
3871 });
3872
3873 OptionalArcTokioRwLockOptionalKeyPathChain {
3874 outer_keypath: self.outer_keypath,
3875 inner_keypath: composed,
3876 }
3877 }
3878}
3879
3880#[cfg(feature = "tokio")]
3882pub struct OptionalArcTokioRwLockWritableKeyPathChain<Root, RwLockValue, InnerValue, SubValue, F, G>
3883where
3884 RwLockValue: std::borrow::Borrow<Arc<TokioRwLock<InnerValue>>>,
3885 F: for<'r> Fn(&'r Root) -> Option<&'r RwLockValue>,
3886 G: for<'r> Fn(&'r mut InnerValue) -> &'r mut SubValue,
3887{
3888 outer_keypath: OptionalKeyPath<Root, RwLockValue, F>,
3889 inner_keypath: WritableKeyPath<InnerValue, SubValue, G>,
3890}
3891
3892#[cfg(feature = "tokio")]
3893impl<Root, RwLockValue, InnerValue, SubValue, F, G> OptionalArcTokioRwLockWritableKeyPathChain<Root, RwLockValue, InnerValue, SubValue, F, G>
3894where
3895 RwLockValue: std::borrow::Borrow<Arc<TokioRwLock<InnerValue>>>,
3896 F: for<'r> Fn(&'r Root) -> Option<&'r RwLockValue>,
3897 G: for<'r> Fn(&'r mut InnerValue) -> &'r mut SubValue,
3898{
3899 pub async fn get_mut<Callback, R>(self, container: &Root, callback: Callback) -> Option<R>
3901 where
3902 Callback: FnOnce(&mut SubValue) -> R,
3903 {
3904 if let Some(arc_rwlock_ref) = self.outer_keypath.get(container) { let arc_rwlock_ref = arc_rwlock_ref.borrow();
3905 let mut guard = arc_rwlock_ref.write().await;
3906 let value_ref = self.inner_keypath.get_mut(&mut *guard);
3907 Some(callback(value_ref))
3908 } else {
3909 None
3910 }
3911 }
3912
3913 pub fn then<NextValue, H>(
3915 self,
3916 next: WritableKeyPath<SubValue, NextValue, H>,
3917 ) -> OptionalArcTokioRwLockWritableKeyPathChain<Root, RwLockValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r mut InnerValue) -> &'r mut NextValue + 'static>
3918 where
3919 H: for<'r> Fn(&'r mut SubValue) -> &'r mut NextValue,
3920 G: 'static,
3921 H: 'static,
3922 InnerValue: 'static,
3923 SubValue: 'static,
3924 NextValue: 'static,
3925 {
3926 let first = self.inner_keypath;
3927 let second = next;
3928
3929 let composed = WritableKeyPath::new(move |inner: &mut InnerValue| {
3930 let sub = first.get_mut(inner);
3931 second.get_mut(sub)
3932 });
3933
3934 OptionalArcTokioRwLockWritableKeyPathChain {
3935 outer_keypath: self.outer_keypath,
3936 inner_keypath: composed,
3937 }
3938 }
3939
3940 pub fn chain_optional<NextValue, H>(
3942 self,
3943 next: WritableOptionalKeyPath<SubValue, NextValue, H>,
3944 ) -> OptionalArcTokioRwLockWritableOptionalKeyPathChain<Root, RwLockValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut NextValue> + 'static>
3945 where
3946 H: for<'r> Fn(&'r mut SubValue) -> Option<&'r mut NextValue>,
3947 G: 'static,
3948 H: 'static,
3949 InnerValue: 'static,
3950 SubValue: 'static,
3951 NextValue: 'static,
3952 {
3953 let first = self.inner_keypath;
3954 let second = next;
3955
3956 let composed = WritableOptionalKeyPath::new(move |inner: &mut InnerValue| {
3957 let sub = first.get_mut(inner);
3958 second.get_mut(sub)
3959 });
3960
3961 OptionalArcTokioRwLockWritableOptionalKeyPathChain {
3962 outer_keypath: self.outer_keypath,
3963 inner_keypath: composed,
3964 }
3965 }
3966}
3967
3968#[cfg(feature = "tokio")]
3970pub struct OptionalArcTokioRwLockWritableOptionalKeyPathChain<Root, RwLockValue, InnerValue, SubValue, F, G>
3971where
3972 RwLockValue: std::borrow::Borrow<Arc<TokioRwLock<InnerValue>>>,
3973 F: for<'r> Fn(&'r Root) -> Option<&'r RwLockValue>,
3974 G: for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut SubValue>,
3975{
3976 outer_keypath: OptionalKeyPath<Root, RwLockValue, F>,
3977 inner_keypath: WritableOptionalKeyPath<InnerValue, SubValue, G>,
3978}
3979
3980#[cfg(feature = "tokio")]
3981impl<Root, RwLockValue, InnerValue, SubValue, F, G> OptionalArcTokioRwLockWritableOptionalKeyPathChain<Root, RwLockValue, InnerValue, SubValue, F, G>
3982where
3983 RwLockValue: std::borrow::Borrow<Arc<TokioRwLock<InnerValue>>>,
3984 F: for<'r> Fn(&'r Root) -> Option<&'r RwLockValue>,
3985 G: for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut SubValue>,
3986{
3987 pub async fn get_mut<Callback, R>(self, container: &Root, callback: Callback) -> Option<R>
3989 where
3990 Callback: FnOnce(&mut SubValue) -> R,
3991 {
3992 if let Some(arc_rwlock_ref) = self.outer_keypath.get(container) { let arc_rwlock_ref = arc_rwlock_ref.borrow();
3993 let mut guard = arc_rwlock_ref.write().await;
3994 self.inner_keypath.get_mut(&mut *guard).map(|value_ref| callback(value_ref))
3995 } else {
3996 None
3997 }
3998 }
3999
4000 pub fn then<NextValue, H>(
4002 self,
4003 next: WritableKeyPath<SubValue, NextValue, H>,
4004 ) -> OptionalArcTokioRwLockWritableOptionalKeyPathChain<Root, RwLockValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut NextValue> + 'static>
4005 where
4006 H: for<'r> Fn(&'r mut SubValue) -> &'r mut NextValue,
4007 G: 'static,
4008 H: 'static,
4009 InnerValue: 'static,
4010 SubValue: 'static,
4011 NextValue: 'static,
4012 {
4013 let first = self.inner_keypath;
4014 let second = next;
4015
4016 let composed = WritableOptionalKeyPath::new(move |inner: &mut InnerValue| {
4017 first.get_mut(inner).map(|sub| second.get_mut(sub))
4018 });
4019
4020 OptionalArcTokioRwLockWritableOptionalKeyPathChain {
4021 outer_keypath: self.outer_keypath,
4022 inner_keypath: composed,
4023 }
4024 }
4025
4026 pub fn chain_optional<NextValue, H>(
4028 self,
4029 next: WritableOptionalKeyPath<SubValue, NextValue, H>,
4030 ) -> OptionalArcTokioRwLockWritableOptionalKeyPathChain<Root, RwLockValue, InnerValue, NextValue, F, impl for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut NextValue> + 'static>
4031 where
4032 H: for<'r> Fn(&'r mut SubValue) -> Option<&'r mut NextValue>,
4033 G: 'static,
4034 H: 'static,
4035 InnerValue: 'static,
4036 SubValue: 'static,
4037 NextValue: 'static,
4038 {
4039 let first = self.inner_keypath;
4040 let second = next;
4041
4042 let composed = WritableOptionalKeyPath::new(move |inner: &mut InnerValue| {
4043 first.get_mut(inner).and_then(|sub| second.get_mut(sub))
4044 });
4045
4046 OptionalArcTokioRwLockWritableOptionalKeyPathChain {
4047 outer_keypath: self.outer_keypath,
4048 inner_keypath: composed,
4049 }
4050 }
4051}
4052
4053#[cfg(feature = "tagged")]
4054use tagged_core::Tagged;
4055
4056
4057#[macro_export]
4079macro_rules! keypath {
4080 ($closure:expr) => {
4082 $crate::KeyPath::new($closure)
4083 };
4084}
4085
4086#[macro_export]
4106macro_rules! opt_keypath {
4107 ($closure:expr) => {
4109 $crate::OptionalKeyPath::new($closure)
4110 };
4111}
4112
4113#[macro_export]
4133macro_rules! writable_keypath {
4134 ($closure:expr) => {
4136 $crate::WritableKeyPath::new($closure)
4137 };
4138}
4139
4140#[macro_export]
4160macro_rules! writable_opt_keypath {
4161 ($closure:expr) => {
4163 $crate::WritableOptionalKeyPath::new($closure)
4164 };
4165}
4166
4167#[derive(Clone)]
4171pub struct KeyPath<Root, Value, F>
4172where
4173 F: for<'r> Fn(&'r Root) -> &'r Value,
4174{
4175 getter: F,
4176 _phantom: PhantomData<(Root, Value)>,
4177}
4178
4179impl<Root, Value, F> fmt::Display for KeyPath<Root, Value, F>
4180where
4181 F: for<'r> Fn(&'r Root) -> &'r Value,
4182{
4183 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4184 let root_name = std::any::type_name::<Root>();
4185 let value_name = std::any::type_name::<Value>();
4186 let root_short = root_name.split("::").last().unwrap_or(root_name);
4188 let value_short = value_name.split("::").last().unwrap_or(value_name);
4189 write!(f, "KeyPath<{} -> {}>", root_short, value_short)
4190 }
4191}
4192
4193impl<Root, Value, F> fmt::Debug for KeyPath<Root, Value, F>
4194where
4195 F: for<'r> Fn(&'r Root) -> &'r Value,
4196{
4197 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4198 fmt::Display::fmt(self, f)
4199 }
4200}
4201
4202impl<Root, Value, F> KeyPath<Root, Value, F>
4203where
4204 F: for<'r> Fn(&'r Root) -> &'r Value,
4205{
4206 pub fn new(getter: F) -> Self {
4207 Self {
4208 getter,
4209 _phantom: PhantomData,
4210 }
4211 }
4212
4213 pub fn get<'r>(&self, root: &'r Root) -> &'r Value {
4214 (self.getter)(root)
4215 }
4216
4217 pub fn chain_arc_mutex_at_kp<InnerValue, SubValue, G>(
4246 self,
4247 inner_keypath: KeyPath<InnerValue, SubValue, G>,
4248 ) -> ArcMutexKeyPathChain<Root, Value, InnerValue, SubValue, F, G>
4249 where
4250 G: for<'r> Fn(&'r InnerValue) -> &'r SubValue,
4251 {
4252 ArcMutexKeyPathChain {
4253 outer_keypath: self,
4254 inner_keypath,
4255 }
4256 }
4257
4258 pub fn chain_arc_mutex_optional_at_kp<InnerValue, SubValue, G>(
4271 self,
4272 inner_keypath: OptionalKeyPath<InnerValue, SubValue, G>,
4273 ) -> ArcMutexOptionalKeyPathChain<Root, Value, InnerValue, SubValue, F, G>
4274 where
4275 G: for<'r> Fn(&'r InnerValue) -> Option<&'r SubValue>,
4276 {
4277 ArcMutexOptionalKeyPathChain {
4278 outer_keypath: self,
4279 inner_keypath,
4280 }
4281 }
4282
4283 pub fn chain_arc_rwlock_at_kp<InnerValue, SubValue, G>(
4296 self,
4297 inner_keypath: KeyPath<InnerValue, SubValue, G>,
4298 ) -> ArcRwLockKeyPathChain<Root, Value, InnerValue, SubValue, F, G>
4299 where
4300 G: for<'r> Fn(&'r InnerValue) -> &'r SubValue,
4301 {
4302 ArcRwLockKeyPathChain {
4303 outer_keypath: self,
4304 inner_keypath,
4305 }
4306 }
4307
4308 pub fn chain_arc_rwlock_optional_at_kp<InnerValue, SubValue, G>(
4321 self,
4322 inner_keypath: OptionalKeyPath<InnerValue, SubValue, G>,
4323 ) -> ArcRwLockOptionalKeyPathChain<Root, Value, InnerValue, SubValue, F, G>
4324 where
4325 G: for<'r> Fn(&'r InnerValue) -> Option<&'r SubValue>,
4326 {
4327 ArcRwLockOptionalKeyPathChain {
4328 outer_keypath: self,
4329 inner_keypath,
4330 }
4331 }
4332
4333 pub fn chain_arc_mutex_writable_at_kp<InnerValue, SubValue, G>(
4343 self,
4344 inner_keypath: WritableKeyPath<InnerValue, SubValue, G>,
4345 ) -> ArcMutexWritableKeyPathChain<Root, Value, InnerValue, SubValue, F, G>
4346 where
4347 G: for<'r> Fn(&'r mut InnerValue) -> &'r mut SubValue,
4348 {
4349 ArcMutexWritableKeyPathChain {
4350 outer_keypath: self,
4351 inner_keypath,
4352 }
4353 }
4354
4355 pub fn chain_arc_mutex_writable_optional_at_kp<InnerValue, SubValue, G>(
4365 self,
4366 inner_keypath: WritableOptionalKeyPath<InnerValue, SubValue, G>,
4367 ) -> ArcMutexWritableOptionalKeyPathChain<Root, Value, InnerValue, SubValue, F, G>
4368 where
4369 G: for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut SubValue>,
4370 {
4371 ArcMutexWritableOptionalKeyPathChain {
4372 outer_keypath: self,
4373 inner_keypath,
4374 }
4375 }
4376
4377 pub fn chain_arc_rwlock_writable_at_kp<InnerValue, SubValue, G>(
4387 self,
4388 inner_keypath: WritableKeyPath<InnerValue, SubValue, G>,
4389 ) -> ArcRwLockWritableKeyPathChain<Root, Value, InnerValue, SubValue, F, G>
4390 where
4391 G: for<'r> Fn(&'r mut InnerValue) -> &'r mut SubValue,
4392 {
4393 ArcRwLockWritableKeyPathChain {
4394 outer_keypath: self,
4395 inner_keypath,
4396 }
4397 }
4398
4399 pub fn chain_arc_rwlock_writable_optional_at_kp<InnerValue, SubValue, G>(
4409 self,
4410 inner_keypath: WritableOptionalKeyPath<InnerValue, SubValue, G>,
4411 ) -> ArcRwLockWritableOptionalKeyPathChain<Root, Value, InnerValue, SubValue, F, G>
4412 where
4413 G: for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut SubValue>,
4414 {
4415 ArcRwLockWritableOptionalKeyPathChain {
4416 outer_keypath: self,
4417 inner_keypath,
4418 }
4419 }
4420
4421 #[cfg(feature = "tokio")]
4422 pub fn chain_arc_tokio_mutex_at_kp<InnerValue, SubValue, G>(
4425 self,
4426 inner_keypath: KeyPath<InnerValue, SubValue, G>,
4427 ) -> ArcTokioMutexKeyPathChain<Root, Value, InnerValue, SubValue, F, G>
4428 where
4429 G: for<'r> Fn(&'r InnerValue) -> &'r SubValue,
4430 {
4431 ArcTokioMutexKeyPathChain {
4432 outer_keypath: self,
4433 inner_keypath,
4434 }
4435 }
4436
4437 #[cfg(feature = "tokio")]
4438 pub fn chain_arc_tokio_mutex_optional_at_kp<InnerValue, SubValue, G>(
4440 self,
4441 inner_keypath: OptionalKeyPath<InnerValue, SubValue, G>,
4442 ) -> ArcTokioMutexOptionalKeyPathChain<Root, Value, InnerValue, SubValue, F, G>
4443 where
4444 Value: std::borrow::Borrow<Arc<tokio::sync::Mutex<InnerValue>>>,
4445 G: for<'r> Fn(&'r InnerValue) -> Option<&'r SubValue>,
4446 {
4447 ArcTokioMutexOptionalKeyPathChain {
4448 outer_keypath: self,
4449 inner_keypath,
4450 }
4451 }
4452
4453 #[cfg(feature = "tokio")]
4454 pub fn chain_arc_tokio_mutex_writable_at_kp<InnerValue, SubValue, G>(
4456 self,
4457 inner_keypath: WritableKeyPath<InnerValue, SubValue, G>,
4458 ) -> ArcTokioMutexWritableKeyPathChain<Root, Value, InnerValue, SubValue, F, G>
4459 where
4460 Value: std::borrow::Borrow<Arc<tokio::sync::Mutex<InnerValue>>>,
4461 G: for<'r> Fn(&'r mut InnerValue) -> &'r mut SubValue,
4462 {
4463 ArcTokioMutexWritableKeyPathChain {
4464 outer_keypath: self,
4465 inner_keypath,
4466 }
4467 }
4468
4469 #[cfg(feature = "tokio")]
4470 pub fn chain_arc_tokio_mutex_writable_optional_at_kp<InnerValue, SubValue, G>(
4472 self,
4473 inner_keypath: WritableOptionalKeyPath<InnerValue, SubValue, G>,
4474 ) -> ArcTokioMutexWritableOptionalKeyPathChain<Root, Value, InnerValue, SubValue, F, G>
4475 where
4476 Value: std::borrow::Borrow<Arc<tokio::sync::Mutex<InnerValue>>>,
4477 G: for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut SubValue>,
4478 {
4479 ArcTokioMutexWritableOptionalKeyPathChain {
4480 outer_keypath: self,
4481 inner_keypath,
4482 }
4483 }
4484
4485 #[cfg(feature = "tokio")]
4486 pub fn chain_arc_tokio_rwlock_at_kp<InnerValue, SubValue, G>(
4488 self,
4489 inner_keypath: KeyPath<InnerValue, SubValue, G>,
4490 ) -> ArcTokioRwLockKeyPathChain<Root, Value, InnerValue, SubValue, F, G>
4491 where
4492 Value: std::borrow::Borrow<Arc<tokio::sync::RwLock<InnerValue>>>,
4493 G: for<'r> Fn(&'r InnerValue) -> &'r SubValue,
4494 {
4495 ArcTokioRwLockKeyPathChain {
4496 outer_keypath: self,
4497 inner_keypath,
4498 }
4499 }
4500
4501 #[cfg(feature = "tokio")]
4502 pub fn chain_arc_tokio_rwlock_optional_at_kp<InnerValue, SubValue, G>(
4504 self,
4505 inner_keypath: OptionalKeyPath<InnerValue, SubValue, G>,
4506 ) -> ArcTokioRwLockOptionalKeyPathChain<Root, Value, InnerValue, SubValue, F, G>
4507 where
4508 Value: std::borrow::Borrow<Arc<tokio::sync::RwLock<InnerValue>>>,
4509 G: for<'r> Fn(&'r InnerValue) -> Option<&'r SubValue>,
4510 {
4511 ArcTokioRwLockOptionalKeyPathChain {
4512 outer_keypath: self,
4513 inner_keypath,
4514 }
4515 }
4516
4517 #[cfg(feature = "tokio")]
4518 pub fn chain_arc_tokio_rwlock_writable_at_kp<InnerValue, SubValue, G>(
4520 self,
4521 inner_keypath: WritableKeyPath<InnerValue, SubValue, G>,
4522 ) -> ArcTokioRwLockWritableKeyPathChain<Root, Value, InnerValue, SubValue, F, G>
4523 where
4524 Value: std::borrow::Borrow<Arc<tokio::sync::RwLock<InnerValue>>>,
4525 G: for<'r> Fn(&'r mut InnerValue) -> &'r mut SubValue,
4526 {
4527 ArcTokioRwLockWritableKeyPathChain {
4528 outer_keypath: self,
4529 inner_keypath,
4530 }
4531 }
4532
4533 #[cfg(feature = "tokio")]
4534 pub fn chain_arc_tokio_rwlock_writable_optional_at_kp<InnerValue, SubValue, G>(
4536 self,
4537 inner_keypath: WritableOptionalKeyPath<InnerValue, SubValue, G>,
4538 ) -> ArcTokioRwLockWritableOptionalKeyPathChain<Root, Value, InnerValue, SubValue, F, G>
4539 where
4540 Value: std::borrow::Borrow<Arc<tokio::sync::RwLock<InnerValue>>>,
4541 G: for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut SubValue>,
4542 {
4543 ArcTokioRwLockWritableOptionalKeyPathChain {
4544 outer_keypath: self,
4545 inner_keypath,
4546 }
4547 }
4548
4549 pub fn then_rwlock<InnerValue, SubValue, G>(
4560 self,
4561 inner_keypath: WritableKeyPath<InnerValue, SubValue, G>,
4562 ) -> ArcRwLockWritableKeyPathChain<Root, Value, InnerValue, SubValue, F, G>
4563 where
4564 Value: std::borrow::Borrow<Arc<RwLock<InnerValue>>>,
4565 G: for<'r> Fn(&'r mut InnerValue) -> &'r mut SubValue,
4566 {
4567 self.chain_arc_rwlock_writable_at_kp(inner_keypath)
4568 }
4569
4570 pub fn to_arc_rwlock_kp<InnerValue>(self) -> Self
4583 where
4584 Value: std::borrow::Borrow<Arc<RwLock<InnerValue>>>,
4585 {
4586 self
4587 }
4588
4589 pub fn to_arc_mutex_kp<InnerValue>(self) -> Self
4592 where
4593 Value: std::borrow::Borrow<Arc<Mutex<InnerValue>>>,
4594 {
4595 self
4596 }
4597
4598 #[cfg(feature = "parking_lot")]
4599 pub fn to_arc_parking_rwlock_kp<InnerValue>(self) -> Self
4609 where
4610 Value: std::borrow::Borrow<Arc<parking_lot::RwLock<InnerValue>>>,
4611 {
4612 self
4613 }
4614
4615 #[cfg(feature = "parking_lot")]
4616 pub fn to_arc_parking_mutex_kp<InnerValue>(self) -> Self
4619 where
4620 Value: std::borrow::Borrow<Arc<parking_lot::Mutex<InnerValue>>>,
4621 {
4622 self
4623 }
4624
4625 pub fn to_arc_rwlock_chain<InnerValue>(self) -> ArcRwLockKeyPathChain<Root, Value, InnerValue, InnerValue, F, impl for<'r> Fn(&'r InnerValue) -> &'r InnerValue + 'static>
4645 where
4646 Value: std::borrow::Borrow<Arc<RwLock<InnerValue>>>,
4647 F: 'static,
4648 InnerValue: 'static,
4649 {
4650 let identity = KeyPath::new(|inner: &InnerValue| inner);
4651 ArcRwLockKeyPathChain {
4652 outer_keypath: self,
4653 inner_keypath: identity,
4654 }
4655 }
4656
4657 pub fn to_arc_mutex_chain<InnerValue>(self) -> ArcMutexKeyPathChain<Root, Value, InnerValue, InnerValue, F, impl for<'r> Fn(&'r InnerValue) -> &'r InnerValue + 'static>
4661 where
4662 Value: std::borrow::Borrow<Arc<Mutex<InnerValue>>>,
4663 F: 'static,
4664 InnerValue: 'static,
4665 {
4666 let identity = KeyPath::new(|inner: &InnerValue| inner);
4667 ArcMutexKeyPathChain {
4668 outer_keypath: self,
4669 inner_keypath: identity,
4670 }
4671 }
4672
4673 pub fn for_box<Target>(self) -> KeyPath<Root, Target, impl for<'r> Fn(&'r Root) -> &'r Target + 'static>
4726 where
4727 Value: std::ops::Deref<Target = Target>,
4728 F: 'static,
4729 Value: 'static,
4730 {
4731 let getter = self.getter;
4732
4733 KeyPath {
4734 getter: move |root: &Root| {
4735 getter(root).deref()
4736 },
4737 _phantom: PhantomData,
4738 }
4739 }
4740
4741 pub fn for_arc<Target>(self) -> KeyPath<Root, Target, impl for<'r> Fn(&'r Root) -> &'r Target + 'static>
4743 where
4744 Value: std::ops::Deref<Target = Target>,
4745 F: 'static,
4746 Value: 'static,
4747 {
4748 let getter = self.getter;
4749
4750 KeyPath {
4751 getter: move |root: &Root| {
4752 getter(root).deref()
4753 },
4754 _phantom: PhantomData,
4755 }
4756 }
4757
4758 pub fn for_rc<Target>(self) -> KeyPath<Root, Target, impl for<'r> Fn(&'r Root) -> &'r Target + 'static>
4760 where
4761 Value: std::ops::Deref<Target = Target>,
4762 F: 'static,
4763 Value: 'static,
4764 {
4765 let getter = self.getter;
4766
4767 KeyPath {
4768 getter: move |root: &Root| {
4769 getter(root).deref()
4770 },
4771 _phantom: PhantomData,
4772 }
4773 }
4774
4775 pub fn for_arc_root(self) -> OptionalKeyPath<Arc<Root>, Value, impl for<'r> Fn(&'r Arc<Root>) -> Option<&'r Value> + 'static>
4777 where
4778 Value: Sized,
4779 F: 'static,
4780 Root: 'static,
4781 Value: 'static,
4782 {
4783 let getter = self.getter;
4784
4785 OptionalKeyPath {
4786 getter: move |arc: &Arc<Root>| {
4787 Some(getter(arc.as_ref()))
4788 },
4789 _phantom: PhantomData,
4790 }
4791 }
4792
4793 pub fn for_box_root(self) -> OptionalKeyPath<Box<Root>, Value, impl for<'r> Fn(&'r Box<Root>) -> Option<&'r Value> + 'static>
4795 where
4796 Value: Sized,
4797 F: 'static,
4798 Root: 'static,
4799 Value: 'static,
4800 {
4801 let getter = self.getter;
4802
4803 OptionalKeyPath {
4804 getter: move |boxed: &Box<Root>| {
4805 Some(getter(boxed.as_ref()))
4806 },
4807 _phantom: PhantomData,
4808 }
4809 }
4810
4811 pub fn for_rc_root(self) -> OptionalKeyPath<Rc<Root>, Value, impl for<'r> Fn(&'r Rc<Root>) -> Option<&'r Value> + 'static>
4813 where
4814 Value: Sized,
4815 F: 'static,
4816 Root: 'static,
4817 Value: 'static,
4818 {
4819 let getter = self.getter;
4820
4821 OptionalKeyPath {
4822 getter: move |rc: &Rc<Root>| {
4823 Some(getter(rc.as_ref()))
4824 },
4825 _phantom: PhantomData,
4826 }
4827 }
4828
4829 pub fn for_result<E>(self) -> OptionalKeyPath<Result<Root, E>, Value, impl for<'r> Fn(&'r Result<Root, E>) -> Option<&'r Value> + 'static>
4832 where
4833 F: 'static,
4834 Root: 'static,
4835 Value: 'static,
4836 E: 'static,
4837 {
4838 let getter = self.getter;
4839
4840 OptionalKeyPath {
4841 getter: move |result: &Result<Root, E>| {
4842 result.as_ref().ok().map(|root| getter(root))
4843 },
4844 _phantom: PhantomData,
4845 }
4846 }
4847
4848 pub fn to_optional(self) -> OptionalKeyPath<Root, Value, impl for<'r> Fn(&'r Root) -> Option<&'r Value> + 'static>
4851 where
4852 F: 'static,
4853 {
4854 let getter = self.getter;
4855 OptionalKeyPath::new(move |root: &Root| Some(getter(root)))
4856 }
4857
4858 pub fn with_option<Callback, R>(&self, option: &Option<Root>, f: Callback) -> Option<R>
4860 where
4861 F: Clone,
4862 Callback: FnOnce(&Value) -> R,
4863 {
4864 option.as_ref().map(|root| {
4865 let value = self.get(root);
4866 f(value)
4867 })
4868 }
4869
4870 pub fn with_result<Callback, R, E>(&self, result: &Result<Root, E>, f: Callback) -> Option<R>
4872 where
4873 F: Clone,
4874 Callback: FnOnce(&Value) -> R,
4875 {
4876 result.as_ref().ok().map(|root| {
4877 let value = self.get(root);
4878 f(value)
4879 })
4880 }
4881
4882 pub fn with_box<Callback, R>(&self, boxed: &Box<Root>, f: Callback) -> R
4884 where
4885 F: Clone,
4886 Callback: FnOnce(&Value) -> R,
4887 {
4888 let value = self.get(boxed);
4889 f(value)
4890 }
4891
4892 pub fn with_arc<Callback, R>(&self, arc: &Arc<Root>, f: Callback) -> R
4894 where
4895 F: Clone,
4896 Callback: FnOnce(&Value) -> R,
4897 {
4898 let value = self.get(arc);
4899 f(value)
4900 }
4901
4902 pub fn with_rc<Callback, R>(&self, rc: &Rc<Root>, f: Callback) -> R
4904 where
4905 F: Clone,
4906 Callback: FnOnce(&Value) -> R,
4907 {
4908 let value = self.get(rc);
4909 f(value)
4910 }
4911
4912 pub fn with_refcell<Callback, R>(&self, refcell: &RefCell<Root>, f: Callback) -> Option<R>
4914 where
4915 F: Clone,
4916 Callback: FnOnce(&Value) -> R,
4917 {
4918 refcell.try_borrow().ok().map(|borrow| {
4919 let value = self.get(&*borrow);
4920 f(value)
4921 })
4922 }
4923
4924 pub fn with_mutex<Callback, R>(&self, mutex: &Mutex<Root>, f: Callback) -> Option<R>
4926 where
4927 F: Clone,
4928 Callback: FnOnce(&Value) -> R,
4929 {
4930 mutex.lock().ok().map(|guard| {
4931 let value = self.get(&*guard);
4932 f(value)
4933 })
4934 }
4935
4936 pub fn with_rwlock<Callback, R>(&self, rwlock: &RwLock<Root>, f: Callback) -> Option<R>
4938 where
4939 F: Clone,
4940 Callback: FnOnce(&Value) -> R,
4941 {
4942 rwlock.read().ok().map(|guard| {
4943 let value = self.get(&*guard);
4944 f(value)
4945 })
4946 }
4947
4948 pub fn with_arc_rwlock<Callback, R>(&self, arc_rwlock: &Arc<RwLock<Root>>, f: Callback) -> Option<R>
4950 where
4951 F: Clone,
4952 Callback: FnOnce(&Value) -> R,
4953 {
4954 arc_rwlock.read().ok().map(|guard| {
4955 let value = self.get(&*guard);
4956 f(value)
4957 })
4958 }
4959
4960 pub fn with_arc_mutex<Callback, R>(&self, arc_mutex: &Arc<Mutex<Root>>, f: Callback) -> Option<R>
4962 where
4963 F: Clone,
4964 Callback: FnOnce(&Value) -> R,
4965 {
4966 arc_mutex.lock().ok().map(|guard| {
4967 let value = self.get(&*guard);
4968 f(value)
4969 })
4970 }
4971
4972 #[cfg(feature = "tagged")]
4973 pub fn for_tagged<Tag>(self) -> KeyPath<Tagged<Root, Tag>, Value, impl for<'r> Fn(&'r Tagged<Root, Tag>) -> &'r Value + 'static>
4976 where
4977 Tagged<Root, Tag>: std::ops::Deref<Target = Root>,
4978 F: 'static,
4979 Root: 'static,
4980 Value: 'static,
4981 Tag: 'static,
4982 {
4983 use std::ops::Deref;
4984 let getter = self.getter;
4985
4986 KeyPath {
4987 getter: move |tagged: &Tagged<Root, Tag>| {
4988 getter(tagged.deref())
4989 },
4990 _phantom: PhantomData,
4991 }
4992 }
4993
4994 #[cfg(feature = "tagged")]
4995 pub fn with_tagged<Tag, Callback, R>(&self, tagged: &Tagged<Root, Tag>, f: Callback) -> R
4998 where
4999 Tagged<Root, Tag>: std::ops::Deref<Target = Root>,
5000 Callback: FnOnce(&Value) -> R,
5001 {
5002 use std::ops::Deref;
5003 let value = self.get(tagged.deref());
5004 f(value)
5005 }
5006
5007 pub fn for_option(self) -> OptionalKeyPath<Option<Root>, Value, impl for<'r> Fn(&'r Option<Root>) -> Option<&'r Value> + 'static>
5010 where
5011 F: 'static,
5012 Root: 'static,
5013 Value: 'static,
5014 {
5015 let getter = self.getter;
5016
5017 OptionalKeyPath {
5018 getter: move |opt: &Option<Root>| {
5019 opt.as_ref().map(|root| getter(root))
5020 },
5021 _phantom: PhantomData,
5022 }
5023 }
5024
5025 pub fn iter<'r, T>(&self, root: &'r Root) -> Option<std::slice::Iter<'r, T>>
5028 where
5029 Value: AsRef<[T]> + 'r,
5030 {
5031 let value_ref: &'r Value = self.get(root);
5032 Some(value_ref.as_ref().iter())
5033 }
5034
5035 pub fn extract_from_slice<'r>(&self, slice: &'r [Root]) -> Vec<&'r Value> {
5038 slice.iter().map(|item| self.get(item)).collect()
5039 }
5040
5041 pub fn extract_from_ref_slice<'r>(&self, slice: &'r [&Root]) -> Vec<&'r Value> {
5044 slice.iter().map(|item| self.get(item)).collect()
5045 }
5046
5047 pub fn then<SubValue, G>(
5050 self,
5051 next: KeyPath<Value, SubValue, G>,
5052 ) -> KeyPath<Root, SubValue, impl for<'r> Fn(&'r Root) -> &'r SubValue>
5053 where
5054 G: for<'r> Fn(&'r Value) -> &'r SubValue,
5055 F: 'static,
5056 G: 'static,
5057 Value: 'static,
5058 {
5059 let first = self.getter;
5060 let second = next.getter;
5061
5062 KeyPath::new(move |root: &Root| {
5063 let value = first(root);
5064 second(value)
5065 })
5066 }
5067
5068 pub fn chain_optional<SubValue, G>(
5071 self,
5072 next: OptionalKeyPath<Value, SubValue, G>,
5073 ) -> OptionalKeyPath<Root, SubValue, impl for<'r> Fn(&'r Root) -> Option<&'r SubValue>>
5074 where
5075 G: for<'r> Fn(&'r Value) -> Option<&'r SubValue>,
5076 F: 'static,
5077 G: 'static,
5078 Value: 'static,
5079 {
5080 let first = self.getter;
5081 let second = next.getter;
5082
5083 OptionalKeyPath::new(move |root: &Root| {
5084 let value = first(root);
5085 second(value)
5086 })
5087 }
5088
5089}
5090
5091impl<Root, Value, F> KeyPath<Root, Value, F>
5093where
5094 F: for<'r> Fn(&'r Root) -> &'r Value,
5095{
5096 pub fn with_arc_rwlock_direct<Callback, R>(&self, arc_rwlock: &Arc<RwLock<Root>>, f: Callback) -> Option<R>
5099 where
5100 Callback: FnOnce(&Value) -> R,
5101 {
5102 arc_rwlock.read().ok().map(|guard| {
5103 let value = self.get(&*guard);
5104 f(value)
5105 })
5106 }
5107
5108 pub fn with_arc_mutex_direct<Callback, R>(&self, arc_mutex: &Arc<Mutex<Root>>, f: Callback) -> Option<R>
5111 where
5112 Callback: FnOnce(&Value) -> R,
5113 {
5114 arc_mutex.lock().ok().map(|guard| {
5115 let value = self.get(&*guard);
5116 f(value)
5117 })
5118 }
5119}
5120
5121pub fn for_slice<T>() -> impl for<'r> Fn(&'r [T], usize) -> Option<&'r T> {
5123 |slice: &[T], index: usize| slice.get(index)
5124}
5125
5126pub mod containers {
5128 use super::{OptionalKeyPath, WritableOptionalKeyPath, KeyPath, WritableKeyPath};
5129 use std::collections::{HashMap, BTreeMap, HashSet, BTreeSet, VecDeque, LinkedList, BinaryHeap};
5130 use std::sync::{Mutex, RwLock, Weak as StdWeak, Arc};
5131 use std::rc::{Weak as RcWeak, Rc};
5132 use std::ops::{Deref, DerefMut};
5133
5134 #[cfg(feature = "parking_lot")]
5135 use parking_lot::{Mutex as ParkingMutex, RwLock as ParkingRwLock};
5136
5137 #[cfg(feature = "tagged")]
5138 use tagged_core::Tagged;
5139
5140 pub fn for_vec_index<T>(index: usize) -> OptionalKeyPath<Vec<T>, T, impl for<'r> Fn(&'r Vec<T>) -> Option<&'r T>> {
5142 OptionalKeyPath::new(move |vec: &Vec<T>| vec.get(index))
5143 }
5144
5145 pub fn for_vecdeque_index<T>(index: usize) -> OptionalKeyPath<VecDeque<T>, T, impl for<'r> Fn(&'r VecDeque<T>) -> Option<&'r T>> {
5147 OptionalKeyPath::new(move |deque: &VecDeque<T>| deque.get(index))
5148 }
5149
5150 pub fn for_linkedlist_index<T>(index: usize) -> OptionalKeyPath<LinkedList<T>, T, impl for<'r> Fn(&'r LinkedList<T>) -> Option<&'r T>> {
5152 OptionalKeyPath::new(move |list: &LinkedList<T>| {
5153 list.iter().nth(index)
5154 })
5155 }
5156
5157 pub fn for_hashmap_key<K, V>(key: K) -> OptionalKeyPath<HashMap<K, V>, V, impl for<'r> Fn(&'r HashMap<K, V>) -> Option<&'r V>>
5159 where
5160 K: std::hash::Hash + Eq + Clone + 'static,
5161 V: 'static,
5162 {
5163 OptionalKeyPath::new(move |map: &HashMap<K, V>| map.get(&key))
5164 }
5165
5166 pub fn for_btreemap_key<K, V>(key: K) -> OptionalKeyPath<BTreeMap<K, V>, V, impl for<'r> Fn(&'r BTreeMap<K, V>) -> Option<&'r V>>
5168 where
5169 K: Ord + Clone + 'static,
5170 V: 'static,
5171 {
5172 OptionalKeyPath::new(move |map: &BTreeMap<K, V>| map.get(&key))
5173 }
5174
5175 pub fn for_hashset_get<T>(value: T) -> OptionalKeyPath<HashSet<T>, T, impl for<'r> Fn(&'r HashSet<T>) -> Option<&'r T>>
5177 where
5178 T: std::hash::Hash + Eq + Clone + 'static,
5179 {
5180 OptionalKeyPath::new(move |set: &HashSet<T>| set.get(&value))
5181 }
5182
5183 pub fn for_btreeset_get<T>(value: T) -> OptionalKeyPath<BTreeSet<T>, T, impl for<'r> Fn(&'r BTreeSet<T>) -> Option<&'r T>>
5185 where
5186 T: Ord + Clone + 'static,
5187 {
5188 OptionalKeyPath::new(move |set: &BTreeSet<T>| set.get(&value))
5189 }
5190
5191 pub fn for_binaryheap_peek<T>() -> OptionalKeyPath<BinaryHeap<T>, T, impl for<'r> Fn(&'r BinaryHeap<T>) -> Option<&'r T>>
5193 where
5194 T: Ord + 'static,
5195 {
5196 OptionalKeyPath::new(|heap: &BinaryHeap<T>| heap.peek())
5197 }
5198
5199 pub fn for_vec_index_mut<T>(index: usize) -> WritableOptionalKeyPath<Vec<T>, T, impl for<'r> Fn(&'r mut Vec<T>) -> Option<&'r mut T>> {
5203 WritableOptionalKeyPath::new(move |vec: &mut Vec<T>| vec.get_mut(index))
5204 }
5205
5206 pub fn for_vecdeque_index_mut<T>(index: usize) -> WritableOptionalKeyPath<VecDeque<T>, T, impl for<'r> Fn(&'r mut VecDeque<T>) -> Option<&'r mut T>> {
5208 WritableOptionalKeyPath::new(move |deque: &mut VecDeque<T>| deque.get_mut(index))
5209 }
5210
5211 pub fn for_linkedlist_index_mut<T>(index: usize) -> WritableOptionalKeyPath<LinkedList<T>, T, impl for<'r> Fn(&'r mut LinkedList<T>) -> Option<&'r mut T>> {
5213 WritableOptionalKeyPath::new(move |list: &mut LinkedList<T>| {
5214 let mut iter = list.iter_mut();
5216 iter.nth(index)
5217 })
5218 }
5219
5220 pub fn for_hashmap_key_mut<K, V>(key: K) -> WritableOptionalKeyPath<HashMap<K, V>, V, impl for<'r> Fn(&'r mut HashMap<K, V>) -> Option<&'r mut V>>
5222 where
5223 K: std::hash::Hash + Eq + Clone + 'static,
5224 V: 'static,
5225 {
5226 WritableOptionalKeyPath::new(move |map: &mut HashMap<K, V>| map.get_mut(&key))
5227 }
5228
5229 pub fn for_btreemap_key_mut<K, V>(key: K) -> WritableOptionalKeyPath<BTreeMap<K, V>, V, impl for<'r> Fn(&'r mut BTreeMap<K, V>) -> Option<&'r mut V>>
5231 where
5232 K: Ord + Clone + 'static,
5233 V: 'static,
5234 {
5235 WritableOptionalKeyPath::new(move |map: &mut BTreeMap<K, V>| map.get_mut(&key))
5236 }
5237
5238 pub fn for_hashset_get_mut<T>(value: T) -> WritableOptionalKeyPath<HashSet<T>, T, impl for<'r> Fn(&'r mut HashSet<T>) -> Option<&'r mut T>>
5241 where
5242 T: std::hash::Hash + Eq + Clone + 'static,
5243 {
5244 WritableOptionalKeyPath::new(move |set: &mut HashSet<T>| {
5245 if set.contains(&value) {
5248 None
5251 } else {
5252 None
5253 }
5254 })
5255 }
5256
5257 pub fn for_btreeset_get_mut<T>(value: T) -> WritableOptionalKeyPath<BTreeSet<T>, T, impl for<'r> Fn(&'r mut BTreeSet<T>) -> Option<&'r mut T>>
5260 where
5261 T: Ord + Clone + 'static,
5262 {
5263 WritableOptionalKeyPath::new(move |set: &mut BTreeSet<T>| {
5264 if set.contains(&value) {
5267 None
5270 } else {
5271 None
5272 }
5273 })
5274 }
5275
5276 pub fn for_binaryheap_peek_mut<T>() -> WritableOptionalKeyPath<BinaryHeap<T>, T, impl for<'r> Fn(&'r mut BinaryHeap<T>) -> Option<&'r mut T>>
5282 where
5283 T: Ord + 'static,
5284 {
5285 WritableOptionalKeyPath::new(|_heap: &mut BinaryHeap<T>| {
5289 None
5290 })
5291 }
5292
5293 pub fn lock_mutex<T>(mutex: &Mutex<T>) -> Option<std::sync::MutexGuard<'_, T>> {
5302 mutex.lock().ok()
5303 }
5304
5305 pub fn read_rwlock<T>(rwlock: &RwLock<T>) -> Option<std::sync::RwLockReadGuard<'_, T>> {
5309 rwlock.read().ok()
5310 }
5311
5312 pub fn write_rwlock<T>(rwlock: &RwLock<T>) -> Option<std::sync::RwLockWriteGuard<'_, T>> {
5316 rwlock.write().ok()
5317 }
5318
5319 pub fn lock_arc_mutex<T>(arc_mutex: &Arc<Mutex<T>>) -> Option<std::sync::MutexGuard<'_, T>> {
5323 arc_mutex.lock().ok()
5324 }
5325
5326 pub fn read_arc_rwlock<T>(arc_rwlock: &Arc<RwLock<T>>) -> Option<std::sync::RwLockReadGuard<'_, T>> {
5330 arc_rwlock.read().ok()
5331 }
5332
5333 pub fn write_arc_rwlock<T>(arc_rwlock: &Arc<RwLock<T>>) -> Option<std::sync::RwLockWriteGuard<'_, T>> {
5337 arc_rwlock.write().ok()
5338 }
5339
5340 pub fn upgrade_weak<T>(weak: &StdWeak<T>) -> Option<Arc<T>> {
5344 weak.upgrade()
5345 }
5346
5347 pub fn upgrade_rc_weak<T>(weak: &RcWeak<T>) -> Option<Rc<T>> {
5351 weak.upgrade()
5352 }
5353
5354 #[cfg(feature = "parking_lot")]
5355 pub fn lock_parking_mutex<T>(mutex: &ParkingMutex<T>) -> parking_lot::MutexGuard<'_, T> {
5358 mutex.lock()
5359 }
5360
5361 #[cfg(feature = "parking_lot")]
5362 pub fn read_parking_rwlock<T>(rwlock: &ParkingRwLock<T>) -> parking_lot::RwLockReadGuard<'_, T> {
5365 rwlock.read()
5366 }
5367
5368 #[cfg(feature = "parking_lot")]
5369 pub fn write_parking_rwlock<T>(rwlock: &ParkingRwLock<T>) -> parking_lot::RwLockWriteGuard<'_, T> {
5372 rwlock.write()
5373 }
5374
5375 #[cfg(feature = "tagged")]
5376 pub fn for_tagged<Tag, T>() -> KeyPath<Tagged<Tag, T>, T, impl for<'r> Fn(&'r Tagged<Tag, T>) -> &'r T>
5379 where
5380 Tagged<Tag, T>: std::ops::Deref<Target = T>,
5381 Tag: 'static,
5382 T: 'static,
5383 {
5384 KeyPath::new(|tagged: &Tagged<Tag, T>| tagged.deref())
5385 }
5386
5387 #[cfg(feature = "tagged")]
5388 pub fn for_tagged_mut<Tag, T>() -> WritableKeyPath<Tagged<Tag, T>, T, impl for<'r> Fn(&'r mut Tagged<Tag, T>) -> &'r mut T>
5391 where
5392 Tagged<Tag, T>: std::ops::DerefMut<Target = T>,
5393 Tag: 'static,
5394 T: 'static,
5395 {
5396 WritableKeyPath::new(|tagged: &mut Tagged<Tag, T>| tagged.deref_mut())
5397 }
5398}
5399
5400#[cfg(feature = "parking_lot")]
5403impl<Root, InnerValue, F> KeyPath<Root, Arc<parking_lot::Mutex<InnerValue>>, F>
5404where
5405 F: for<'r> Fn(&'r Root) -> &'r Arc<parking_lot::Mutex<InnerValue>>,
5406{
5407 pub fn chain_arc_parking_mutex_at_kp<SubValue, G>(
5409 self,
5410 inner_keypath: KeyPath<InnerValue, SubValue, G>,
5411 ) -> ArcParkingMutexKeyPathChain<Root, InnerValue, SubValue, F, G>
5412 where
5413 G: for<'r> Fn(&'r InnerValue) -> &'r SubValue,
5414 {
5415 ArcParkingMutexKeyPathChain {
5416 outer_keypath: self,
5417 inner_keypath,
5418 }
5419 }
5420
5421 pub fn then_arc_parking_mutex_optional_at_kp<SubValue, G>(
5423 self,
5424 inner_keypath: OptionalKeyPath<InnerValue, SubValue, G>,
5425 ) -> ArcParkingMutexOptionalKeyPathChain<Root, InnerValue, SubValue, F, G>
5426 where
5427 G: for<'r> Fn(&'r InnerValue) -> Option<&'r SubValue>,
5428 {
5429 ArcParkingMutexOptionalKeyPathChain {
5430 outer_keypath: self,
5431 inner_keypath,
5432 }
5433 }
5434
5435 pub fn chain_arc_parking_mutex_writable_at_kp<SubValue, G>(
5437 self,
5438 inner_keypath: WritableKeyPath<InnerValue, SubValue, G>,
5439 ) -> ArcParkingMutexWritableKeyPathChain<Root, InnerValue, SubValue, F, G>
5440 where
5441 G: for<'r> Fn(&'r mut InnerValue) -> &'r mut SubValue,
5442 {
5443 ArcParkingMutexWritableKeyPathChain {
5444 outer_keypath: self,
5445 inner_keypath,
5446 }
5447 }
5448
5449 pub fn then_arc_parking_mutex_writable_optional_at_kp<SubValue, G>(
5451 self,
5452 inner_keypath: WritableOptionalKeyPath<InnerValue, SubValue, G>,
5453 ) -> ArcParkingMutexWritableOptionalKeyPathChain<Root, InnerValue, SubValue, F, G>
5454 where
5455 G: for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut SubValue>,
5456 {
5457 ArcParkingMutexWritableOptionalKeyPathChain {
5458 outer_keypath: self,
5459 inner_keypath,
5460 }
5461 }
5462}
5463
5464#[cfg(feature = "parking_lot")]
5465impl<Root, InnerValue, F> KeyPath<Root, Arc<parking_lot::RwLock<InnerValue>>, F>
5466where
5467 F: for<'r> Fn(&'r Root) -> &'r Arc<parking_lot::RwLock<InnerValue>>,
5468{
5469 pub fn chain_arc_parking_rwlock_at_kp<SubValue, G>(
5471 self,
5472 inner_keypath: KeyPath<InnerValue, SubValue, G>,
5473 ) -> ArcParkingRwLockKeyPathChain<Root, InnerValue, SubValue, F, G>
5474 where
5475 G: for<'r> Fn(&'r InnerValue) -> &'r SubValue,
5476 {
5477 ArcParkingRwLockKeyPathChain {
5478 outer_keypath: self,
5479 inner_keypath,
5480 }
5481 }
5482
5483 pub fn then_arc_parking_rwlock_optional_at_kp<SubValue, G>(
5485 self,
5486 inner_keypath: OptionalKeyPath<InnerValue, SubValue, G>,
5487 ) -> ArcParkingRwLockOptionalKeyPathChain<Root, InnerValue, SubValue, F, G>
5488 where
5489 G: for<'r> Fn(&'r InnerValue) -> Option<&'r SubValue>,
5490 {
5491 ArcParkingRwLockOptionalKeyPathChain {
5492 outer_keypath: self,
5493 inner_keypath,
5494 }
5495 }
5496
5497 pub fn chain_arc_parking_rwlock_writable_at_kp<SubValue, G>(
5499 self,
5500 inner_keypath: WritableKeyPath<InnerValue, SubValue, G>,
5501 ) -> ArcParkingRwLockWritableKeyPathChain<Root, InnerValue, SubValue, F, G>
5502 where
5503 G: for<'r> Fn(&'r mut InnerValue) -> &'r mut SubValue,
5504 {
5505 ArcParkingRwLockWritableKeyPathChain {
5506 outer_keypath: self,
5507 inner_keypath,
5508 }
5509 }
5510
5511 pub fn then_arc_parking_rwlock_writable_optional_at_kp<SubValue, G>(
5513 self,
5514 inner_keypath: WritableOptionalKeyPath<InnerValue, SubValue, G>,
5515 ) -> ArcParkingRwLockWritableOptionalKeyPathChain<Root, InnerValue, SubValue, F, G>
5516 where
5517 G: for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut SubValue>,
5518 {
5519 ArcParkingRwLockWritableOptionalKeyPathChain {
5520 outer_keypath: self,
5521 inner_keypath,
5522 }
5523 }
5524}
5525
5526#[derive(Clone)]
5528pub struct OptionalKeyPath<Root, Value, F>
5529where
5530 F: for<'r> Fn(&'r Root) -> Option<&'r Value>,
5531{
5532 getter: F,
5533 _phantom: PhantomData<(Root, Value)>,
5534}
5535
5536impl<Root, Value, F> fmt::Display for OptionalKeyPath<Root, Value, F>
5537where
5538 F: for<'r> Fn(&'r Root) -> Option<&'r Value>,
5539{
5540 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
5541 let root_name = std::any::type_name::<Root>();
5542 let value_name = std::any::type_name::<Value>();
5543 let root_short = root_name.split("::").last().unwrap_or(root_name);
5545 let value_short = value_name.split("::").last().unwrap_or(value_name);
5546 write!(f, "OptionalKeyPath<{} -> Option<{}>>", root_short, value_short)
5547 }
5548}
5549
5550impl<Root, Value, F> fmt::Debug for OptionalKeyPath<Root, Value, F>
5551where
5552 F: for<'r> Fn(&'r Root) -> Option<&'r Value>,
5553{
5554 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
5555 fmt::Display::fmt(self, f)
5556 }
5557}
5558
5559impl<Root, Value, F> OptionalKeyPath<Root, Value, F>
5560where
5561 F: for<'r> Fn(&'r Root) -> Option<&'r Value>,
5562{
5563 pub fn new(getter: F) -> Self {
5564 Self {
5565 getter,
5566 _phantom: PhantomData,
5567 }
5568 }
5569
5570 pub fn get<'r>(&self, root: &'r Root) -> Option<&'r Value> {
5571 (self.getter)(root)
5572 }
5573
5574 pub fn chain_arc_mutex_at_kp<InnerValue, SubValue, G>(
5587 self,
5588 inner_keypath: KeyPath<InnerValue, SubValue, G>,
5589 ) -> OptionalArcMutexKeyPathChain<Root, Value, InnerValue, SubValue, F, G>
5590 where
5591 G: for<'r> Fn(&'r InnerValue) -> &'r SubValue,
5592 {
5593 OptionalArcMutexKeyPathChain {
5594 outer_keypath: self,
5595 inner_keypath,
5596 }
5597 }
5598
5599 pub fn chain_arc_mutex_optional_at_kp<InnerValue, SubValue, G>(
5612 self,
5613 inner_keypath: OptionalKeyPath<InnerValue, SubValue, G>,
5614 ) -> OptionalArcMutexOptionalKeyPathChain<Root, Value, InnerValue, SubValue, F, G>
5615 where
5616 G: for<'r> Fn(&'r InnerValue) -> Option<&'r SubValue>,
5617 {
5618 OptionalArcMutexOptionalKeyPathChain {
5619 outer_keypath: self,
5620 inner_keypath,
5621 }
5622 }
5623
5624 pub fn chain_arc_rwlock_at_kp<InnerValue, SubValue, G>(
5637 self,
5638 inner_keypath: KeyPath<InnerValue, SubValue, G>,
5639 ) -> OptionalArcRwLockKeyPathChain<Root, Value, InnerValue, SubValue, F, G>
5640 where
5641 G: for<'r> Fn(&'r InnerValue) -> &'r SubValue,
5642 {
5643 OptionalArcRwLockKeyPathChain {
5644 outer_keypath: self,
5645 inner_keypath,
5646 }
5647 }
5648
5649 pub fn chain_arc_rwlock_optional_at_kp<InnerValue, SubValue, G>(
5662 self,
5663 inner_keypath: OptionalKeyPath<InnerValue, SubValue, G>,
5664 ) -> OptionalArcRwLockOptionalKeyPathChain<Root, Value, InnerValue, SubValue, F, G>
5665 where
5666 G: for<'r> Fn(&'r InnerValue) -> Option<&'r SubValue>,
5667 {
5668 OptionalArcRwLockOptionalKeyPathChain {
5669 outer_keypath: self,
5670 inner_keypath,
5671 }
5672 }
5673
5674 pub fn chain_arc_mutex_writable_at_kp<InnerValue, SubValue, G>(
5687 self,
5688 inner_keypath: WritableKeyPath<InnerValue, SubValue, G>,
5689 ) -> OptionalArcMutexWritableKeyPathChain<Root, Value, InnerValue, SubValue, F, G>
5690 where
5691 G: for<'r> Fn(&'r mut InnerValue) -> &'r mut SubValue,
5692 {
5693 OptionalArcMutexWritableKeyPathChain {
5694 outer_keypath: self,
5695 inner_keypath,
5696 }
5697 }
5698
5699 pub fn chain_arc_mutex_writable_optional_at_kp<InnerValue, SubValue, G>(
5712 self,
5713 inner_keypath: WritableOptionalKeyPath<InnerValue, SubValue, G>,
5714 ) -> OptionalArcMutexWritableOptionalKeyPathChain<Root, Value, InnerValue, SubValue, F, G>
5715 where
5716 G: for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut SubValue>,
5717 {
5718 OptionalArcMutexWritableOptionalKeyPathChain {
5719 outer_keypath: self,
5720 inner_keypath,
5721 }
5722 }
5723
5724 pub fn chain_arc_rwlock_writable_at_kp<InnerValue, SubValue, G>(
5737 self,
5738 inner_keypath: WritableKeyPath<InnerValue, SubValue, G>,
5739 ) -> OptionalArcRwLockWritableKeyPathChain<Root, Value, InnerValue, SubValue, F, G>
5740 where
5741 G: for<'r> Fn(&'r mut InnerValue) -> &'r mut SubValue,
5742 {
5743 OptionalArcRwLockWritableKeyPathChain {
5744 outer_keypath: self,
5745 inner_keypath,
5746 }
5747 }
5748
5749 pub fn chain_arc_rwlock_writable_optional_at_kp<InnerValue, SubValue, G>(
5762 self,
5763 inner_keypath: WritableOptionalKeyPath<InnerValue, SubValue, G>,
5764 ) -> OptionalArcRwLockWritableOptionalKeyPathChain<Root, Value, InnerValue, SubValue, F, G>
5765 where
5766 G: for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut SubValue>,
5767 {
5768 OptionalArcRwLockWritableOptionalKeyPathChain {
5769 outer_keypath: self,
5770 inner_keypath,
5771 }
5772 }
5773
5774 #[cfg(feature = "tokio")]
5775 pub fn chain_arc_tokio_mutex_at_kp<InnerValue, SubValue, G>(
5777 self,
5778 inner_keypath: KeyPath<InnerValue, SubValue, G>,
5779 ) -> OptionalArcTokioMutexKeyPathChain<Root, Value, InnerValue, SubValue, F, G>
5780 where
5781 Value: std::borrow::Borrow<Arc<tokio::sync::Mutex<InnerValue>>>,
5782 G: for<'r> Fn(&'r InnerValue) -> &'r SubValue,
5783 {
5784 OptionalArcTokioMutexKeyPathChain {
5785 outer_keypath: self,
5786 inner_keypath,
5787 }
5788 }
5789
5790 #[cfg(feature = "tokio")]
5791 pub fn chain_arc_tokio_mutex_optional_at_kp<InnerValue, SubValue, G>(
5793 self,
5794 inner_keypath: OptionalKeyPath<InnerValue, SubValue, G>,
5795 ) -> OptionalArcTokioMutexOptionalKeyPathChain<Root, Value, InnerValue, SubValue, F, G>
5796 where
5797 Value: std::borrow::Borrow<Arc<tokio::sync::Mutex<InnerValue>>>,
5798 G: for<'r> Fn(&'r InnerValue) -> Option<&'r SubValue>,
5799 {
5800 OptionalArcTokioMutexOptionalKeyPathChain {
5801 outer_keypath: self,
5802 inner_keypath,
5803 }
5804 }
5805
5806 #[cfg(feature = "tokio")]
5807 pub fn chain_arc_tokio_mutex_writable_at_kp<InnerValue, SubValue, G>(
5809 self,
5810 inner_keypath: WritableKeyPath<InnerValue, SubValue, G>,
5811 ) -> OptionalArcTokioMutexWritableKeyPathChain<Root, Value, InnerValue, SubValue, F, G>
5812 where
5813 Value: std::borrow::Borrow<Arc<tokio::sync::Mutex<InnerValue>>>,
5814 G: for<'r> Fn(&'r mut InnerValue) -> &'r mut SubValue,
5815 {
5816 OptionalArcTokioMutexWritableKeyPathChain {
5817 outer_keypath: self,
5818 inner_keypath,
5819 }
5820 }
5821
5822 #[cfg(feature = "tokio")]
5823 pub fn chain_arc_tokio_mutex_writable_optional_at_kp<InnerValue, SubValue, G>(
5825 self,
5826 inner_keypath: WritableOptionalKeyPath<InnerValue, SubValue, G>,
5827 ) -> OptionalArcTokioMutexWritableOptionalKeyPathChain<Root, Value, InnerValue, SubValue, F, G>
5828 where
5829 Value: std::borrow::Borrow<Arc<tokio::sync::Mutex<InnerValue>>>,
5830 G: for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut SubValue>,
5831 {
5832 OptionalArcTokioMutexWritableOptionalKeyPathChain {
5833 outer_keypath: self,
5834 inner_keypath,
5835 }
5836 }
5837
5838 #[cfg(feature = "tokio")]
5839 pub fn chain_arc_tokio_rwlock_at_kp<InnerValue, SubValue, G>(
5841 self,
5842 inner_keypath: KeyPath<InnerValue, SubValue, G>,
5843 ) -> OptionalArcTokioRwLockKeyPathChain<Root, Value, InnerValue, SubValue, F, G>
5844 where
5845 Value: std::borrow::Borrow<Arc<tokio::sync::RwLock<InnerValue>>>,
5846 G: for<'r> Fn(&'r InnerValue) -> &'r SubValue,
5847 {
5848 OptionalArcTokioRwLockKeyPathChain {
5849 outer_keypath: self,
5850 inner_keypath,
5851 }
5852 }
5853
5854 #[cfg(feature = "tokio")]
5855 pub fn chain_arc_tokio_rwlock_optional_at_kp<InnerValue, SubValue, G>(
5857 self,
5858 inner_keypath: OptionalKeyPath<InnerValue, SubValue, G>,
5859 ) -> OptionalArcTokioRwLockOptionalKeyPathChain<Root, Value, InnerValue, SubValue, F, G>
5860 where
5861 Value: std::borrow::Borrow<Arc<tokio::sync::RwLock<InnerValue>>>,
5862 G: for<'r> Fn(&'r InnerValue) -> Option<&'r SubValue>,
5863 {
5864 OptionalArcTokioRwLockOptionalKeyPathChain {
5865 outer_keypath: self,
5866 inner_keypath,
5867 }
5868 }
5869
5870 #[cfg(feature = "tokio")]
5871 pub fn chain_arc_tokio_rwlock_writable_at_kp<InnerValue, SubValue, G>(
5873 self,
5874 inner_keypath: WritableKeyPath<InnerValue, SubValue, G>,
5875 ) -> OptionalArcTokioRwLockWritableKeyPathChain<Root, Value, InnerValue, SubValue, F, G>
5876 where
5877 Value: std::borrow::Borrow<Arc<tokio::sync::RwLock<InnerValue>>>,
5878 G: for<'r> Fn(&'r mut InnerValue) -> &'r mut SubValue,
5879 {
5880 OptionalArcTokioRwLockWritableKeyPathChain {
5881 outer_keypath: self,
5882 inner_keypath,
5883 }
5884 }
5885
5886 #[cfg(feature = "tokio")]
5887 pub fn chain_arc_tokio_rwlock_writable_optional_at_kp<InnerValue, SubValue, G>(
5889 self,
5890 inner_keypath: WritableOptionalKeyPath<InnerValue, SubValue, G>,
5891 ) -> OptionalArcTokioRwLockWritableOptionalKeyPathChain<Root, Value, InnerValue, SubValue, F, G>
5892 where
5893 Value: std::borrow::Borrow<Arc<tokio::sync::RwLock<InnerValue>>>,
5894 G: for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut SubValue>,
5895 {
5896 OptionalArcTokioRwLockWritableOptionalKeyPathChain {
5897 outer_keypath: self,
5898 inner_keypath,
5899 }
5900 }
5901
5902 pub fn then<SubValue, G>(
5904 self,
5905 next: OptionalKeyPath<Value, SubValue, G>,
5906 ) -> OptionalKeyPath<Root, SubValue, impl for<'r> Fn(&'r Root) -> Option<&'r SubValue>>
5907 where
5908 G: for<'r> Fn(&'r Value) -> Option<&'r SubValue>,
5909 F: 'static,
5910 G: 'static,
5911 Value: 'static,
5912 {
5913 let first = self.getter;
5914 let second = next.getter;
5915
5916 OptionalKeyPath::new(move |root: &Root| {
5917 first(root).and_then(|value| second(value))
5918 })
5919 }
5920
5921 pub fn for_box<Target>(self) -> OptionalKeyPath<Root, Target, impl for<'r> Fn(&'r Root) -> Option<&'r Target> + 'static>
5924 where
5925 Value: std::ops::Deref<Target = Target>,
5926 F: 'static,
5927 Value: 'static,
5928 {
5929 let getter = self.getter;
5930
5931 OptionalKeyPath {
5932 getter: move |root: &Root| {
5933 getter(root).map(|boxed| boxed.deref())
5934 },
5935 _phantom: PhantomData,
5936 }
5937 }
5938
5939 pub fn for_arc<Target>(self) -> OptionalKeyPath<Root, Target, impl for<'r> Fn(&'r Root) -> Option<&'r Target> + 'static>
5941 where
5942 Value: std::ops::Deref<Target = Target>,
5943 F: 'static,
5944 Value: 'static,
5945 {
5946 let getter = self.getter;
5947
5948 OptionalKeyPath {
5949 getter: move |root: &Root| {
5950 getter(root).map(|arc| arc.deref())
5951 },
5952 _phantom: PhantomData,
5953 }
5954 }
5955
5956 pub fn for_rc<Target>(self) -> OptionalKeyPath<Root, Target, impl for<'r> Fn(&'r Root) -> Option<&'r Target> + 'static>
5958 where
5959 Value: std::ops::Deref<Target = Target>,
5960 F: 'static,
5961 Value: 'static,
5962 {
5963 let getter = self.getter;
5964
5965 OptionalKeyPath {
5966 getter: move |root: &Root| {
5967 getter(root).map(|rc| rc.deref())
5968 },
5969 _phantom: PhantomData,
5970 }
5971 }
5972
5973 #[cfg(feature = "tagged")]
5974 pub fn for_tagged<Tag>(self) -> OptionalKeyPath<Tagged<Root, Tag>, Value, impl for<'r> Fn(&'r Tagged<Root, Tag>) -> Option<&'r Value> + 'static>
5977 where
5978 Tagged<Root, Tag>: std::ops::Deref<Target = Root>,
5979 F: 'static,
5980 Root: 'static,
5981 Value: 'static,
5982 Tag: 'static,
5983 {
5984 use std::ops::Deref;
5985 let getter = self.getter;
5986
5987 OptionalKeyPath {
5988 getter: move |tagged: &Tagged<Root, Tag>| {
5989 getter(tagged.deref())
5990 },
5991 _phantom: PhantomData,
5992 }
5993 }
5994
5995 #[cfg(feature = "tagged")]
5996 pub fn with_tagged<Tag, Callback, R>(&self, tagged: &Tagged<Root, Tag>, f: Callback) -> Option<R>
5999 where
6000 Tagged<Root, Tag>: std::ops::Deref<Target = Root>,
6001 F: Clone,
6002 Callback: FnOnce(&Value) -> R,
6003 {
6004 use std::ops::Deref;
6005 self.get(tagged.deref()).map(|value| f(value))
6006 }
6007
6008 pub fn for_option(self) -> OptionalKeyPath<Option<Root>, Value, impl for<'r> Fn(&'r Option<Root>) -> Option<&'r Value> + 'static>
6011 where
6012 F: 'static,
6013 Root: 'static,
6014 Value: 'static,
6015 {
6016 let getter = self.getter;
6017
6018 OptionalKeyPath {
6019 getter: move |opt: &Option<Root>| {
6020 opt.as_ref().and_then(|root| getter(root))
6021 },
6022 _phantom: PhantomData,
6023 }
6024 }
6025
6026 pub fn for_result<E>(self) -> OptionalKeyPath<Result<Root, E>, Value, impl for<'r> Fn(&'r Result<Root, E>) -> Option<&'r Value> + 'static>
6029 where
6030 F: 'static,
6031 Root: 'static,
6032 Value: 'static,
6033 E: 'static,
6034 {
6035 let getter = self.getter;
6036
6037 OptionalKeyPath {
6038 getter: move |result: &Result<Root, E>| {
6039 result.as_ref().ok().and_then(|root| getter(root))
6040 },
6041 _phantom: PhantomData,
6042 }
6043 }
6044
6045 pub fn to_arc_rwlock_kp<InnerValue>(self) -> Self
6051 where
6052 Value: std::borrow::Borrow<Arc<RwLock<InnerValue>>>,
6053 {
6054 self
6055 }
6056
6057 pub fn to_arc_mutex_kp<InnerValue>(self) -> Self
6060 where
6061 Value: std::borrow::Borrow<Arc<Mutex<InnerValue>>>,
6062 {
6063 self
6064 }
6065
6066 #[cfg(feature = "parking_lot")]
6067 pub fn to_arc_parking_rwlock_kp<InnerValue>(self) -> Self
6070 where
6071 Value: std::borrow::Borrow<Arc<parking_lot::RwLock<InnerValue>>>,
6072 {
6073 self
6074 }
6075
6076 #[cfg(feature = "parking_lot")]
6077 pub fn to_arc_parking_mutex_kp<InnerValue>(self) -> Self
6080 where
6081 Value: std::borrow::Borrow<Arc<parking_lot::Mutex<InnerValue>>>,
6082 {
6083 self
6084 }
6085
6086 pub fn to_arc_rwlock_chain<InnerValue>(self) -> OptionalArcRwLockKeyPathChain<Root, Value, InnerValue, InnerValue, F, impl for<'r> Fn(&'r InnerValue) -> &'r InnerValue + 'static>
6090 where
6091 Value: std::borrow::Borrow<Arc<RwLock<InnerValue>>>,
6092 F: 'static,
6093 InnerValue: 'static,
6094 {
6095 let identity = KeyPath::new(|inner: &InnerValue| inner);
6096 OptionalArcRwLockKeyPathChain {
6097 outer_keypath: self,
6098 inner_keypath: identity,
6099 }
6100 }
6101
6102 pub fn to_arc_mutex_chain<InnerValue>(self) -> OptionalArcMutexKeyPathChain<Root, Value, InnerValue, InnerValue, F, impl for<'r> Fn(&'r InnerValue) -> &'r InnerValue + 'static>
6106 where
6107 Value: std::borrow::Borrow<Arc<Mutex<InnerValue>>>,
6108 F: 'static,
6109 InnerValue: 'static,
6110 {
6111 let identity = KeyPath::new(|inner: &InnerValue| inner);
6112 OptionalArcMutexKeyPathChain {
6113 outer_keypath: self,
6114 inner_keypath: identity,
6115 }
6116 }
6117
6118 pub fn for_arc_root(self) -> OptionalKeyPath<Arc<Root>, Value, impl for<'r> Fn(&'r Arc<Root>) -> Option<&'r Value> + 'static>
6164 where
6165 Value: Sized,
6166 F: 'static,
6167 Root: 'static,
6168 Value: 'static,
6169 {
6170 let getter = self.getter;
6171
6172 OptionalKeyPath {
6173 getter: move |arc: &Arc<Root>| {
6174 getter(arc.as_ref())
6175 },
6176 _phantom: PhantomData,
6177 }
6178 }
6179
6180 pub fn for_rc_root(self) -> OptionalKeyPath<Rc<Root>, Value, impl for<'r> Fn(&'r Rc<Root>) -> Option<&'r Value> + 'static>
6182 where
6183 Value: Sized,
6184 F: 'static,
6185 Root: 'static,
6186 Value: 'static,
6187 {
6188 let getter = self.getter;
6189
6190 OptionalKeyPath {
6191 getter: move |rc: &Rc<Root>| {
6192 getter(rc.as_ref())
6193 },
6194 _phantom: PhantomData,
6195 }
6196 }
6197
6198 pub fn with_option<Callback, R>(&self, option: &Option<Root>, f: Callback) -> Option<R>
6200 where
6201 F: Clone,
6202 Callback: FnOnce(&Value) -> R,
6203 {
6204 option.as_ref().and_then(|root| {
6205 self.get(root).map(|value| f(value))
6206 })
6207 }
6208
6209 pub fn with_mutex<Callback, R>(&self, mutex: &Mutex<Root>, f: Callback) -> Option<R>
6211 where
6212 F: Clone,
6213 Callback: FnOnce(&Value) -> R,
6214 {
6215 mutex.lock().ok().and_then(|guard| {
6216 self.get(&*guard).map(|value| f(value))
6217 })
6218 }
6219
6220 pub fn with_rwlock<Callback, R>(&self, rwlock: &RwLock<Root>, f: Callback) -> Option<R>
6222 where
6223 F: Clone,
6224 Callback: FnOnce(&Value) -> R,
6225 {
6226 rwlock.read().ok().and_then(|guard| {
6227 self.get(&*guard).map(|value| f(value))
6228 })
6229 }
6230
6231 pub fn with_arc_rwlock<Callback, R>(&self, arc_rwlock: &Arc<RwLock<Root>>, f: Callback) -> Option<R>
6233 where
6234 F: Clone,
6235 Callback: FnOnce(&Value) -> R,
6236 {
6237 arc_rwlock.read().ok().and_then(|guard| {
6238 self.get(&*guard).map(|value| f(value))
6239 })
6240 }
6241
6242 pub fn with_arc_rwlock_direct<Callback, R>(&self, arc_rwlock: &Arc<RwLock<Root>>, f: Callback) -> Option<R>
6246 where
6247 Callback: FnOnce(&Value) -> R,
6248 {
6249 arc_rwlock.read().ok().and_then(|guard| {
6250 self.get(&*guard).map(|value| f(value))
6251 })
6252 }
6253
6254 pub fn with_arc_mutex<Callback, R>(&self, arc_mutex: &Arc<Mutex<Root>>, f: Callback) -> Option<R>
6256 where
6257 F: Clone,
6258 Callback: FnOnce(&Value) -> R,
6259 {
6260 arc_mutex.lock().ok().and_then(|guard| {
6261 self.get(&*guard).map(|value| f(value))
6262 })
6263 }
6264}
6265
6266#[cfg(feature = "parking_lot")]
6269impl<Root, InnerValue, F> OptionalKeyPath<Root, Arc<parking_lot::Mutex<InnerValue>>, F>
6270where
6271 F: for<'r> Fn(&'r Root) -> Option<&'r Arc<parking_lot::Mutex<InnerValue>>>,
6272{
6273 pub fn chain_arc_parking_mutex_at_kp<SubValue, G>(
6275 self,
6276 inner_keypath: KeyPath<InnerValue, SubValue, G>,
6277 ) -> OptionalArcParkingMutexKeyPathChain<Root, InnerValue, SubValue, F, G>
6278 where
6279 G: for<'r> Fn(&'r InnerValue) -> &'r SubValue,
6280 {
6281 OptionalArcParkingMutexKeyPathChain {
6282 outer_keypath: self,
6283 inner_keypath,
6284 }
6285 }
6286
6287 pub fn then_arc_parking_mutex_optional_at_kp<SubValue, G>(
6289 self,
6290 inner_keypath: OptionalKeyPath<InnerValue, SubValue, G>,
6291 ) -> OptionalArcParkingMutexOptionalKeyPathChain<Root, InnerValue, SubValue, F, G>
6292 where
6293 G: for<'r> Fn(&'r InnerValue) -> Option<&'r SubValue>,
6294 {
6295 OptionalArcParkingMutexOptionalKeyPathChain {
6296 outer_keypath: self,
6297 inner_keypath,
6298 }
6299 }
6300
6301 pub fn chain_arc_parking_mutex_writable_at_kp<SubValue, G>(
6303 self,
6304 inner_keypath: WritableKeyPath<InnerValue, SubValue, G>,
6305 ) -> OptionalArcParkingMutexWritableKeyPathChain<Root, InnerValue, SubValue, F, G>
6306 where
6307 G: for<'r> Fn(&'r mut InnerValue) -> &'r mut SubValue,
6308 {
6309 OptionalArcParkingMutexWritableKeyPathChain {
6310 outer_keypath: self,
6311 inner_keypath,
6312 }
6313 }
6314
6315 pub fn then_arc_parking_mutex_writable_optional_at_kp<SubValue, G>(
6317 self,
6318 inner_keypath: WritableOptionalKeyPath<InnerValue, SubValue, G>,
6319 ) -> OptionalArcParkingMutexWritableOptionalKeyPathChain<Root, InnerValue, SubValue, F, G>
6320 where
6321 G: for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut SubValue>,
6322 {
6323 OptionalArcParkingMutexWritableOptionalKeyPathChain {
6324 outer_keypath: self,
6325 inner_keypath,
6326 }
6327 }
6328}
6329
6330#[cfg(feature = "parking_lot")]
6331impl<Root, InnerValue, F> OptionalKeyPath<Root, Arc<parking_lot::RwLock<InnerValue>>, F>
6332where
6333 F: for<'r> Fn(&'r Root) -> Option<&'r Arc<parking_lot::RwLock<InnerValue>>>,
6334{
6335 pub fn chain_arc_parking_rwlock_at_kp<SubValue, G>(
6337 self,
6338 inner_keypath: KeyPath<InnerValue, SubValue, G>,
6339 ) -> OptionalArcParkingRwLockKeyPathChain<Root, InnerValue, SubValue, F, G>
6340 where
6341 G: for<'r> Fn(&'r InnerValue) -> &'r SubValue,
6342 {
6343 OptionalArcParkingRwLockKeyPathChain {
6344 outer_keypath: self,
6345 inner_keypath,
6346 }
6347 }
6348
6349 pub fn then_arc_parking_rwlock_optional_at_kp<SubValue, G>(
6351 self,
6352 inner_keypath: OptionalKeyPath<InnerValue, SubValue, G>,
6353 ) -> OptionalArcParkingRwLockOptionalKeyPathChain<Root, InnerValue, SubValue, F, G>
6354 where
6355 G: for<'r> Fn(&'r InnerValue) -> Option<&'r SubValue>,
6356 {
6357 OptionalArcParkingRwLockOptionalKeyPathChain {
6358 outer_keypath: self,
6359 inner_keypath,
6360 }
6361 }
6362
6363 pub fn chain_arc_parking_rwlock_writable_at_kp<SubValue, G>(
6365 self,
6366 inner_keypath: WritableKeyPath<InnerValue, SubValue, G>,
6367 ) -> OptionalArcParkingRwLockWritableKeyPathChain<Root, InnerValue, SubValue, F, G>
6368 where
6369 G: for<'r> Fn(&'r mut InnerValue) -> &'r mut SubValue,
6370 {
6371 OptionalArcParkingRwLockWritableKeyPathChain {
6372 outer_keypath: self,
6373 inner_keypath,
6374 }
6375 }
6376
6377 pub fn then_arc_parking_rwlock_writable_optional_at_kp<SubValue, G>(
6379 self,
6380 inner_keypath: WritableOptionalKeyPath<InnerValue, SubValue, G>,
6381 ) -> OptionalArcParkingRwLockWritableOptionalKeyPathChain<Root, InnerValue, SubValue, F, G>
6382 where
6383 G: for<'r> Fn(&'r mut InnerValue) -> Option<&'r mut SubValue>,
6384 {
6385 OptionalArcParkingRwLockWritableOptionalKeyPathChain {
6386 outer_keypath: self,
6387 inner_keypath,
6388 }
6389 }
6390}
6391
6392
6393#[derive(Clone)]
6395pub struct WritableKeyPath<Root, Value, F>
6396where
6397 F: for<'r> Fn(&'r mut Root) -> &'r mut Value,
6398{
6399 getter: F,
6400 _phantom: PhantomData<(Root, Value)>,
6401}
6402
6403impl<Root, Value, F> fmt::Display for WritableKeyPath<Root, Value, F>
6404where
6405 F: for<'r> Fn(&'r mut Root) -> &'r mut Value,
6406{
6407 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
6408 let root_name = std::any::type_name::<Root>();
6409 let value_name = std::any::type_name::<Value>();
6410 let root_short = root_name.split("::").last().unwrap_or(root_name);
6412 let value_short = value_name.split("::").last().unwrap_or(value_name);
6413 write!(f, "WritableKeyPath<{} -> {}>", root_short, value_short)
6414 }
6415}
6416
6417impl<Root, Value, F> fmt::Debug for WritableKeyPath<Root, Value, F>
6418where
6419 F: for<'r> Fn(&'r mut Root) -> &'r mut Value,
6420{
6421 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
6422 fmt::Display::fmt(self, f)
6423 }
6424}
6425
6426impl<Root, Value, F> WritableKeyPath<Root, Value, F>
6427where
6428 F: for<'r> Fn(&'r mut Root) -> &'r mut Value,
6429{
6430 pub fn new(getter: F) -> Self {
6431 Self {
6432 getter,
6433 _phantom: PhantomData,
6434 }
6435 }
6436
6437 pub fn get_mut<'r>(&self, root: &'r mut Root) -> &'r mut Value {
6438 (self.getter)(root)
6439 }
6440
6441 pub fn for_result<E>(self) -> WritableOptionalKeyPath<Result<Root, E>, Value, impl for<'r> Fn(&'r mut Result<Root, E>) -> Option<&'r mut Value> + 'static>
6444 where
6445 F: 'static,
6446 Root: 'static,
6447 Value: 'static,
6448 E: 'static,
6449 {
6450 let getter = self.getter;
6451
6452 WritableOptionalKeyPath {
6453 getter: move |result: &mut Result<Root, E>| {
6454 result.as_mut().ok().map(|root| getter(root))
6455 },
6456 _phantom: PhantomData,
6457 }
6458 }
6459
6460 pub fn for_box_root(self) -> WritableKeyPath<Box<Root>, Value, impl for<'r> Fn(&'r mut Box<Root>) -> &'r mut Value + 'static>
6462 where
6463 Value: Sized,
6464 F: 'static,
6465 Root: 'static,
6466 Value: 'static,
6467 {
6468 let getter = self.getter;
6469
6470 WritableKeyPath {
6471 getter: move |boxed: &mut Box<Root>| {
6472 getter(boxed.as_mut())
6473 },
6474 _phantom: PhantomData,
6475 }
6476 }
6477
6478 pub fn for_option(self) -> WritableOptionalKeyPath<Option<Root>, Value, impl for<'r> Fn(&'r mut Option<Root>) -> Option<&'r mut Value> + 'static>
6481 where
6482 F: 'static,
6483 Root: 'static,
6484 Value: 'static,
6485 {
6486 let getter = self.getter;
6487
6488 WritableOptionalKeyPath {
6489 getter: move |option: &mut Option<Root>| {
6490 option.as_mut().map(|root| getter(root))
6491 },
6492 _phantom: PhantomData,
6493 }
6494 }
6495
6496 pub fn to_optional(self) -> WritableOptionalKeyPath<Root, Value, impl for<'r> Fn(&'r mut Root) -> Option<&'r mut Value> + 'static>
6499 where
6500 F: 'static,
6501 {
6502 let getter = self.getter;
6503 WritableOptionalKeyPath::new(move |root: &mut Root| Some(getter(root)))
6504 }
6505
6506 pub fn for_box<Target>(self) -> WritableKeyPath<Root, Target, impl for<'r> Fn(&'r mut Root) -> &'r mut Target + 'static>
6509 where
6510 Value: std::ops::DerefMut<Target = Target>,
6511 F: 'static,
6512 Value: 'static,
6513 {
6514 let getter = self.getter;
6515
6516 WritableKeyPath {
6517 getter: move |root: &mut Root| {
6518 getter(root).deref_mut()
6519 },
6520 _phantom: PhantomData,
6521 }
6522 }
6523
6524 pub fn for_arc<Target>(self) -> WritableKeyPath<Root, Target, impl for<'r> Fn(&'r mut Root) -> &'r mut Target + 'static>
6527 where
6528 Value: std::ops::DerefMut<Target = Target>,
6529 F: 'static,
6530 Value: 'static,
6531 {
6532 let getter = self.getter;
6533
6534 WritableKeyPath {
6535 getter: move |root: &mut Root| {
6536 getter(root).deref_mut()
6537 },
6538 _phantom: PhantomData,
6539 }
6540 }
6541
6542 pub fn for_rc<Target>(self) -> WritableKeyPath<Root, Target, impl for<'r> Fn(&'r mut Root) -> &'r mut Target + 'static>
6545 where
6546 Value: std::ops::DerefMut<Target = Target>,
6547 F: 'static,
6548 Value: 'static,
6549 {
6550 let getter = self.getter;
6551
6552 WritableKeyPath {
6553 getter: move |root: &mut Root| {
6554 getter(root).deref_mut()
6555 },
6556 _phantom: PhantomData,
6557 }
6558 }
6559
6560 pub fn with_box_mut<Callback, R>(&self, boxed: &mut Box<Root>, f: Callback) -> R
6562 where
6563 F: Clone,
6564 Callback: FnOnce(&mut Value) -> R,
6565 {
6566 let value = self.get_mut(boxed);
6567 f(value)
6568 }
6569
6570 pub fn with_result_mut<Callback, R, E>(&self, result: &mut Result<Root, E>, f: Callback) -> Option<R>
6572 where
6573 F: Clone,
6574 Callback: FnOnce(&mut Value) -> R,
6575 {
6576 result.as_mut().ok().map(|root| {
6577 let value = self.get_mut(root);
6578 f(value)
6579 })
6580 }
6581
6582 pub fn with_option_mut<Callback, R>(&self, option: &mut Option<Root>, f: Callback) -> Option<R>
6584 where
6585 F: Clone,
6586 Callback: FnOnce(&mut Value) -> R,
6587 {
6588 option.as_mut().map(|root| {
6589 let value = self.get_mut(root);
6590 f(value)
6591 })
6592 }
6593
6594 pub fn with_refcell_mut<Callback, R>(&self, refcell: &RefCell<Root>, f: Callback) -> Option<R>
6596 where
6597 F: Clone,
6598 Callback: FnOnce(&mut Value) -> R,
6599 {
6600 refcell.try_borrow_mut().ok().map(|mut borrow| {
6601 let value = self.get_mut(&mut *borrow);
6602 f(value)
6603 })
6604 }
6605
6606 pub fn with_mutex_mut<Callback, R>(&self, mutex: &mut Mutex<Root>, f: Callback) -> Option<R>
6608 where
6609 F: Clone,
6610 Callback: FnOnce(&mut Value) -> R,
6611 {
6612 mutex.get_mut().ok().map(|root| {
6613 let value = self.get_mut(root);
6614 f(value)
6615 })
6616 }
6617
6618 pub fn with_rwlock_mut<Callback, R>(&self, rwlock: &mut RwLock<Root>, f: Callback) -> Option<R>
6620 where
6621 F: Clone,
6622 Callback: FnOnce(&mut Value) -> R,
6623 {
6624 rwlock.write().ok().map(|mut guard| {
6625 let value = self.get_mut(&mut *guard);
6626 f(value)
6627 })
6628 }
6629
6630 pub fn iter_mut<'r, T>(&self, root: &'r mut Root) -> Option<std::slice::IterMut<'r, T>>
6633 where
6634 Value: AsMut<[T]> + 'r,
6635 {
6636 let value_ref: &'r mut Value = self.get_mut(root);
6637 Some(value_ref.as_mut().iter_mut())
6638 }
6639
6640 pub fn extract_mut_from_slice<'r>(&self, slice: &'r mut [Root]) -> Vec<&'r mut Value> {
6643 slice.iter_mut().map(|item| self.get_mut(item)).collect()
6644 }
6645
6646 pub fn extract_mut_from_ref_slice<'r>(&self, slice: &'r mut [&'r mut Root]) -> Vec<&'r mut Value> {
6649 slice.iter_mut().map(|item| self.get_mut(*item)).collect()
6650 }
6651
6652 pub fn then<SubValue, G>(
6655 self,
6656 next: WritableKeyPath<Value, SubValue, G>,
6657 ) -> WritableKeyPath<Root, SubValue, impl for<'r> Fn(&'r mut Root) -> &'r mut SubValue>
6658 where
6659 G: for<'r> Fn(&'r mut Value) -> &'r mut SubValue,
6660 F: 'static,
6661 G: 'static,
6662 Value: 'static,
6663 {
6664 let first = self.getter;
6665 let second = next.getter;
6666
6667 WritableKeyPath::new(move |root: &mut Root| {
6668 let value = first(root);
6669 second(value)
6670 })
6671 }
6672
6673 pub fn chain_optional<SubValue, G>(
6676 self,
6677 next: WritableOptionalKeyPath<Value, SubValue, G>,
6678 ) -> WritableOptionalKeyPath<Root, SubValue, impl for<'r> Fn(&'r mut Root) -> Option<&'r mut SubValue>>
6679 where
6680 G: for<'r> Fn(&'r mut Value) -> Option<&'r mut SubValue>,
6681 F: 'static,
6682 G: 'static,
6683 Value: 'static,
6684 {
6685 let first = self.getter;
6686 let second = next.getter;
6687
6688 WritableOptionalKeyPath::new(move |root: &mut Root| {
6689 let value = first(root);
6690 second(value)
6691 })
6692 }
6693
6694 pub fn to_arc_rwlock_kp<InnerValue>(self) -> Self
6700 where
6701 Value: std::borrow::Borrow<Arc<RwLock<InnerValue>>>,
6702 {
6703 self
6704 }
6705
6706 pub fn to_arc_mutex_kp<InnerValue>(self) -> Self
6709 where
6710 Value: std::borrow::Borrow<Arc<Mutex<InnerValue>>>,
6711 {
6712 self
6713 }
6714
6715 #[cfg(feature = "parking_lot")]
6716 pub fn to_arc_parking_rwlock_kp<InnerValue>(self) -> Self
6719 where
6720 Value: std::borrow::Borrow<Arc<parking_lot::RwLock<InnerValue>>>,
6721 {
6722 self
6723 }
6724
6725 #[cfg(feature = "parking_lot")]
6726 pub fn to_arc_parking_mutex_kp<InnerValue>(self) -> Self
6729 where
6730 Value: std::borrow::Borrow<Arc<parking_lot::Mutex<InnerValue>>>,
6731 {
6732 self
6733 }
6734
6735}
6736
6737#[derive(Clone)]
6739pub struct WritableOptionalKeyPath<Root, Value, F>
6740where
6741 F: for<'r> Fn(&'r mut Root) -> Option<&'r mut Value>,
6742{
6743 getter: F,
6744 _phantom: PhantomData<(Root, Value)>,
6745}
6746
6747impl<Root, Value, F> fmt::Display for WritableOptionalKeyPath<Root, Value, F>
6748where
6749 F: for<'r> Fn(&'r mut Root) -> Option<&'r mut Value>,
6750{
6751 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
6752 let root_name = std::any::type_name::<Root>();
6753 let value_name = std::any::type_name::<Value>();
6754 let root_short = root_name.split("::").last().unwrap_or(root_name);
6756 let value_short = value_name.split("::").last().unwrap_or(value_name);
6757 write!(f, "WritableOptionalKeyPath<{} -> Option<{}>>", root_short, value_short)
6758 }
6759}
6760
6761impl<Root, Value, F> fmt::Debug for WritableOptionalKeyPath<Root, Value, F>
6762where
6763 F: for<'r> Fn(&'r mut Root) -> Option<&'r mut Value>,
6764{
6765 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
6766 let root_name = std::any::type_name::<Root>();
6768 let value_name = std::any::type_name::<Value>();
6769 let root_short = root_name.split("::").last().unwrap_or(root_name);
6770 let value_short = value_name.split("::").last().unwrap_or(value_name);
6771
6772 if f.alternate() {
6774 writeln!(f, "WritableOptionalKeyPath<{} -> Option<{}>>", root_short, value_short)?;
6775 writeln!(f, " âš Chain may break if any intermediate step returns None")?;
6776 writeln!(f, " 💡 Use trace_chain() to find where the chain breaks")
6777 } else {
6778 write!(f, "WritableOptionalKeyPath<{} -> Option<{}>>", root_short, value_short)
6779 }
6780 }
6781}
6782
6783impl<Root, Value, F> WritableOptionalKeyPath<Root, Value, F>
6784where
6785 F: for<'r> Fn(&'r mut Root) -> Option<&'r mut Value>,
6786{
6787 pub fn new(getter: F) -> Self {
6788 Self {
6789 getter,
6790 _phantom: PhantomData,
6791 }
6792 }
6793
6794 pub fn get_mut<'r>(&self, root: &'r mut Root) -> Option<&'r mut Value> {
6795 (self.getter)(root)
6796 }
6797
6798 pub fn trace_chain(&self, root: &mut Root) -> Result<(), String> {
6813 match self.get_mut(root) {
6814 Some(_) => Ok(()),
6815 None => {
6816 let root_name = std::any::type_name::<Root>();
6817 let value_name = std::any::type_name::<Value>();
6818 let root_short = root_name.split("::").last().unwrap_or(root_name);
6819 let value_short = value_name.split("::").last().unwrap_or(value_name);
6820 Err(format!("{} -> Option<{}> returned None (chain broken at this step)", root_short, value_short))
6821 }
6822 }
6823 }
6824
6825 pub fn for_option(self) -> WritableOptionalKeyPath<Option<Root>, Value, impl for<'r> Fn(&'r mut Option<Root>) -> Option<&'r mut Value> + 'static>
6828 where
6829 F: 'static,
6830 Root: 'static,
6831 Value: 'static,
6832 {
6833 let getter = self.getter;
6834
6835 WritableOptionalKeyPath {
6836 getter: move |option: &mut Option<Root>| {
6837 option.as_mut().and_then(|root| getter(root))
6838 },
6839 _phantom: PhantomData,
6840 }
6841 }
6842
6843 pub fn then<SubValue, G>(
6845 self,
6846 next: WritableOptionalKeyPath<Value, SubValue, G>,
6847 ) -> WritableOptionalKeyPath<Root, SubValue, impl for<'r> Fn(&'r mut Root) -> Option<&'r mut SubValue>>
6848 where
6849 G: for<'r> Fn(&'r mut Value) -> Option<&'r mut SubValue>,
6850 F: 'static,
6851 G: 'static,
6852 Value: 'static,
6853 {
6854 let first = self.getter;
6855 let second = next.getter;
6856
6857 WritableOptionalKeyPath::new(move |root: &mut Root| {
6858 first(root).and_then(|value| second(value))
6859 })
6860 }
6861
6862 pub fn for_box<Target>(self) -> WritableOptionalKeyPath<Root, Target, impl for<'r> Fn(&'r mut Root) -> Option<&'r mut Target> + 'static>
6865 where
6866 Value: std::ops::DerefMut<Target = Target>,
6867 F: 'static,
6868 Value: 'static,
6869 {
6870 let getter = self.getter;
6871
6872 WritableOptionalKeyPath {
6873 getter: move |root: &mut Root| {
6874 getter(root).map(|boxed| boxed.deref_mut())
6875 },
6876 _phantom: PhantomData,
6877 }
6878 }
6879
6880 pub fn for_arc<Target>(self) -> WritableOptionalKeyPath<Root, Target, impl for<'r> Fn(&'r mut Root) -> Option<&'r mut Target> + 'static>
6882 where
6883 Value: std::ops::DerefMut<Target = Target>,
6884 F: 'static,
6885 Value: 'static,
6886 {
6887 let getter = self.getter;
6888
6889 WritableOptionalKeyPath {
6890 getter: move |root: &mut Root| {
6891 getter(root).map(|arc| arc.deref_mut())
6892 },
6893 _phantom: PhantomData,
6894 }
6895 }
6896
6897 pub fn for_rc<Target>(self) -> WritableOptionalKeyPath<Root, Target, impl for<'r> Fn(&'r mut Root) -> Option<&'r mut Target> + 'static>
6899 where
6900 Value: std::ops::DerefMut<Target = Target>,
6901 F: 'static,
6902 Value: 'static,
6903 {
6904 let getter = self.getter;
6905
6906 WritableOptionalKeyPath {
6907 getter: move |root: &mut Root| {
6908 getter(root).map(|rc| rc.deref_mut())
6909 },
6910 _phantom: PhantomData,
6911 }
6912 }
6913
6914 pub fn for_result<E>(self) -> WritableOptionalKeyPath<Result<Root, E>, Value, impl for<'r> Fn(&'r mut Result<Root, E>) -> Option<&'r mut Value> + 'static>
6917 where
6918 F: 'static,
6919 Root: 'static,
6920 Value: 'static,
6921 E: 'static,
6922 {
6923 let getter = self.getter;
6924
6925 WritableOptionalKeyPath {
6926 getter: move |result: &mut Result<Root, E>| {
6927 result.as_mut().ok().and_then(|root| getter(root))
6928 },
6929 _phantom: PhantomData,
6930 }
6931 }
6932
6933 pub fn for_box_root(self) -> WritableOptionalKeyPath<Box<Root>, Value, impl for<'r> Fn(&'r mut Box<Root>) -> Option<&'r mut Value> + 'static>
6935 where
6936 Value: Sized,
6937 F: 'static,
6938 Root: 'static,
6939 Value: 'static,
6940 {
6941 let getter = self.getter;
6942
6943 WritableOptionalKeyPath {
6944 getter: move |boxed: &mut Box<Root>| {
6945 getter(boxed.as_mut())
6946 },
6947 _phantom: PhantomData,
6948 }
6949 }
6950
6951 pub fn for_arc_root(self) -> WritableOptionalKeyPath<Arc<Root>, Value, impl for<'r> Fn(&'r mut Arc<Root>) -> Option<&'r mut Value> + 'static>
6953 where
6954 Value: Sized,
6955 F: 'static,
6956 Root: 'static,
6957 Value: 'static,
6958 {
6959 let getter = self.getter;
6960
6961 WritableOptionalKeyPath {
6962 getter: move |arc: &mut Arc<Root>| {
6963 None
6966 },
6967 _phantom: PhantomData,
6968 }
6969 }
6970
6971 pub fn for_rc_root(self) -> WritableOptionalKeyPath<Rc<Root>, Value, impl for<'r> Fn(&'r mut Rc<Root>) -> Option<&'r mut Value> + 'static>
6973 where
6974 Value: Sized,
6975 F: 'static,
6976 Root: 'static,
6977 Value: 'static,
6978 {
6979 let getter = self.getter;
6980
6981 WritableOptionalKeyPath {
6982 getter: move |rc: &mut Rc<Root>| {
6983 None
6986 },
6987 _phantom: PhantomData,
6988 }
6989 }
6990
6991 pub fn to_arc_rwlock_kp<InnerValue>(self) -> Self
6997 where
6998 Value: std::borrow::Borrow<Arc<RwLock<InnerValue>>>,
6999 {
7000 self
7001 }
7002
7003 pub fn to_arc_mutex_kp<InnerValue>(self) -> Self
7006 where
7007 Value: std::borrow::Borrow<Arc<Mutex<InnerValue>>>,
7008 {
7009 self
7010 }
7011
7012 #[cfg(feature = "parking_lot")]
7013 pub fn to_arc_parking_rwlock_kp<InnerValue>(self) -> Self
7016 where
7017 Value: std::borrow::Borrow<Arc<parking_lot::RwLock<InnerValue>>>,
7018 {
7019 self
7020 }
7021
7022 #[cfg(feature = "parking_lot")]
7023 pub fn to_arc_parking_mutex_kp<InnerValue>(self) -> Self
7026 where
7027 Value: std::borrow::Borrow<Arc<parking_lot::Mutex<InnerValue>>>,
7028 {
7029 self
7030 }
7031}
7032
7033impl WritableOptionalKeyPath<(), (), fn(&mut ()) -> Option<&mut ()>> {
7035 pub fn for_option_static<T>() -> WritableOptionalKeyPath<Option<T>, T, impl for<'r> Fn(&'r mut Option<T>) -> Option<&'r mut T>> {
7038 WritableOptionalKeyPath::new(|opt: &mut Option<T>| opt.as_mut())
7039 }
7040
7041 pub fn writable_enum<Enum, Variant, EmbedFn, ReadExtractFn, WriteExtractFn>(
7062 _embedder: EmbedFn,
7063 _read_extractor: ReadExtractFn,
7064 write_extractor: WriteExtractFn,
7065 ) -> WritableOptionalKeyPath<Enum, Variant, impl for<'r> Fn(&'r mut Enum) -> Option<&'r mut Variant> + 'static>
7066 where
7067 EmbedFn: Fn(Variant) -> Enum + 'static,
7068 ReadExtractFn: for<'r> Fn(&'r Enum) -> Option<&'r Variant> + 'static,
7069 WriteExtractFn: for<'r> Fn(&'r mut Enum) -> Option<&'r mut Variant> + 'static,
7070 {
7071 WritableOptionalKeyPath::new(write_extractor)
7072 }
7073}
7074
7075pub struct EnumKeyPath<Enum = (), Variant = (), ExtractFn = fn(&()) -> Option<&()>, EmbedFn = fn(()) -> ()>
7083where
7084 ExtractFn: for<'r> Fn(&'r Enum) -> Option<&'r Variant> + 'static,
7085 EmbedFn: Fn(Variant) -> Enum + 'static,
7086{
7087 extractor: OptionalKeyPath<Enum, Variant, ExtractFn>,
7088 embedder: EmbedFn,
7089}
7090
7091impl<Enum, Variant, ExtractFn, EmbedFn> EnumKeyPath<Enum, Variant, ExtractFn, EmbedFn>
7092where
7093 ExtractFn: for<'r> Fn(&'r Enum) -> Option<&'r Variant> + 'static,
7094 EmbedFn: Fn(Variant) -> Enum + 'static,
7095{
7096 pub fn new(
7098 extractor: ExtractFn,
7099 embedder: EmbedFn,
7100 ) -> Self {
7101 Self {
7102 extractor: OptionalKeyPath::new(extractor),
7103 embedder,
7104 }
7105 }
7106
7107 pub fn get<'r>(&self, enum_value: &'r Enum) -> Option<&'r Variant> {
7109 self.extractor.get(enum_value)
7110 }
7111
7112 pub fn embed(&self, value: Variant) -> Enum {
7114 (self.embedder)(value)
7115 }
7116
7117 pub fn as_optional(&self) -> &OptionalKeyPath<Enum, Variant, ExtractFn> {
7119 &self.extractor
7120 }
7121
7122 pub fn to_optional(self) -> OptionalKeyPath<Enum, Variant, ExtractFn> {
7124 self.extractor
7125 }
7126}
7127
7128impl EnumKeyPath {
7130 pub fn readable_enum<Enum, Variant, ExtractFn, EmbedFn>(
7133 embedder: EmbedFn,
7134 extractor: ExtractFn,
7135 ) -> EnumKeyPath<Enum, Variant, ExtractFn, EmbedFn>
7136 where
7137 ExtractFn: for<'r> Fn(&'r Enum) -> Option<&'r Variant> + 'static,
7138 EmbedFn: Fn(Variant) -> Enum + 'static,
7139 {
7140 EnumKeyPath::new(extractor, embedder)
7141 }
7142
7143 pub fn for_variant<Enum, Variant, ExtractFn>(
7145 extractor: ExtractFn
7146 ) -> OptionalKeyPath<Enum, Variant, impl for<'r> Fn(&'r Enum) -> Option<&'r Variant>>
7147 where
7148 ExtractFn: Fn(&Enum) -> Option<&Variant>,
7149 {
7150 OptionalKeyPath::new(extractor)
7151 }
7152
7153 pub fn for_match<Enum, Output, MatchFn>(
7155 matcher: MatchFn
7156 ) -> KeyPath<Enum, Output, impl for<'r> Fn(&'r Enum) -> &'r Output>
7157 where
7158 MatchFn: Fn(&Enum) -> &Output,
7159 {
7160 KeyPath::new(matcher)
7161 }
7162
7163 pub fn for_ok<T, E>() -> OptionalKeyPath<Result<T, E>, T, impl for<'r> Fn(&'r Result<T, E>) -> Option<&'r T>> {
7165 OptionalKeyPath::new(|result: &Result<T, E>| result.as_ref().ok())
7166 }
7167
7168 pub fn for_err<T, E>() -> OptionalKeyPath<Result<T, E>, E, impl for<'r> Fn(&'r Result<T, E>) -> Option<&'r E>> {
7170 OptionalKeyPath::new(|result: &Result<T, E>| result.as_ref().err())
7171 }
7172
7173 pub fn for_some<T>() -> OptionalKeyPath<Option<T>, T, impl for<'r> Fn(&'r Option<T>) -> Option<&'r T>> {
7175 OptionalKeyPath::new(|opt: &Option<T>| opt.as_ref())
7176 }
7177
7178 pub fn for_option<T>() -> OptionalKeyPath<Option<T>, T, impl for<'r> Fn(&'r Option<T>) -> Option<&'r T>> {
7180 OptionalKeyPath::new(|opt: &Option<T>| opt.as_ref())
7181 }
7182
7183 pub fn for_box<T>() -> KeyPath<Box<T>, T, impl for<'r> Fn(&'r Box<T>) -> &'r T> {
7185 KeyPath::new(|b: &Box<T>| b.as_ref())
7186 }
7187
7188 pub fn for_arc<T>() -> KeyPath<Arc<T>, T, impl for<'r> Fn(&'r Arc<T>) -> &'r T> {
7190 KeyPath::new(|arc: &Arc<T>| arc.as_ref())
7191 }
7192
7193 pub fn for_rc<T>() -> KeyPath<std::rc::Rc<T>, T, impl for<'r> Fn(&'r std::rc::Rc<T>) -> &'r T> {
7195 KeyPath::new(|rc: &std::rc::Rc<T>| rc.as_ref())
7196 }
7197
7198 pub fn for_box_mut<T>() -> WritableKeyPath<Box<T>, T, impl for<'r> Fn(&'r mut Box<T>) -> &'r mut T> {
7200 WritableKeyPath::new(|b: &mut Box<T>| b.as_mut())
7201 }
7202
7203 }
7207
7208pub fn variant_of<Enum, Variant, F>(extractor: F) -> OptionalKeyPath<Enum, Variant, F>
7210where
7211 F: for<'r> Fn(&'r Enum) -> Option<&'r Variant>,
7212{
7213 OptionalKeyPath::new(extractor)
7214}
7215
7216#[derive(Clone)]
7232pub struct PartialKeyPath<Root> {
7233 getter: Rc<dyn for<'r> Fn(&'r Root) -> &'r dyn Any>,
7234 value_type_id: TypeId,
7235 _phantom: PhantomData<Root>,
7236}
7237
7238impl<Root> PartialKeyPath<Root> {
7239 pub fn new<Value>(keypath: KeyPath<Root, Value, impl for<'r> Fn(&'r Root) -> &'r Value + 'static>) -> Self
7240 where
7241 Value: Any + 'static,
7242 Root: 'static,
7243 {
7244 let value_type_id = TypeId::of::<Value>();
7245 let getter = Rc::new(keypath.getter);
7246
7247 Self {
7248 getter: Rc::new(move |root: &Root| {
7249 let value: &Value = getter(root);
7250 value as &dyn Any
7251 }),
7252 value_type_id,
7253 _phantom: PhantomData,
7254 }
7255 }
7256
7257 pub fn from<Value>(keypath: KeyPath<Root, Value, impl for<'r> Fn(&'r Root) -> &'r Value + 'static>) -> Self
7260 where
7261 Value: Any + 'static,
7262 Root: 'static,
7263 {
7264 Self::new(keypath)
7265 }
7266
7267 pub fn get<'r>(&self, root: &'r Root) -> &'r dyn Any {
7268 (self.getter)(root)
7269 }
7270
7271 pub fn value_type_id(&self) -> TypeId {
7273 self.value_type_id
7274 }
7275
7276 pub fn get_as<'a, Value: Any>(&self, root: &'a Root) -> Option<&'a Value> {
7278 if self.value_type_id == TypeId::of::<Value>() {
7279 self.get(root).downcast_ref::<Value>()
7280 } else {
7281 None
7282 }
7283 }
7284
7285 pub fn kind_name(&self) -> String {
7288 format!("{:?}", self.value_type_id)
7289 }
7290
7291 pub fn for_arc(&self) -> PartialOptionalKeyPath<Arc<Root>>
7293 where
7294 Root: 'static,
7295 {
7296 let getter = self.getter.clone();
7297 let value_type_id = self.value_type_id;
7298
7299 PartialOptionalKeyPath {
7300 getter: Rc::new(move |arc: &Arc<Root>| {
7301 Some(getter(arc.as_ref()))
7302 }),
7303 value_type_id,
7304 _phantom: PhantomData,
7305 }
7306 }
7307
7308 pub fn for_box(&self) -> PartialOptionalKeyPath<Box<Root>>
7310 where
7311 Root: 'static,
7312 {
7313 let getter = self.getter.clone();
7314 let value_type_id = self.value_type_id;
7315
7316 PartialOptionalKeyPath {
7317 getter: Rc::new(move |boxed: &Box<Root>| {
7318 Some(getter(boxed.as_ref()))
7319 }),
7320 value_type_id,
7321 _phantom: PhantomData,
7322 }
7323 }
7324
7325 pub fn for_rc(&self) -> PartialOptionalKeyPath<Rc<Root>>
7327 where
7328 Root: 'static,
7329 {
7330 let getter = self.getter.clone();
7331 let value_type_id = self.value_type_id;
7332
7333 PartialOptionalKeyPath {
7334 getter: Rc::new(move |rc: &Rc<Root>| {
7335 Some(getter(rc.as_ref()))
7336 }),
7337 value_type_id,
7338 _phantom: PhantomData,
7339 }
7340 }
7341
7342 pub fn for_option(&self) -> PartialOptionalKeyPath<Option<Root>>
7344 where
7345 Root: 'static,
7346 {
7347 let getter = self.getter.clone();
7348 let value_type_id = self.value_type_id;
7349
7350 PartialOptionalKeyPath {
7351 getter: Rc::new(move |opt: &Option<Root>| {
7352 opt.as_ref().map(|root| getter(root))
7353 }),
7354 value_type_id,
7355 _phantom: PhantomData,
7356 }
7357 }
7358
7359 pub fn for_result<E>(&self) -> PartialOptionalKeyPath<Result<Root, E>>
7361 where
7362 Root: 'static,
7363 E: 'static,
7364 {
7365 let getter = self.getter.clone();
7366 let value_type_id = self.value_type_id;
7367
7368 PartialOptionalKeyPath {
7369 getter: Rc::new(move |result: &Result<Root, E>| {
7370 result.as_ref().ok().map(|root| getter(root))
7371 }),
7372 value_type_id,
7373 _phantom: PhantomData,
7374 }
7375 }
7376
7377 pub fn for_arc_rwlock(&self) -> PartialOptionalKeyPath<Arc<RwLock<Root>>>
7381 where
7382 Root: Clone + 'static,
7383 {
7384 PartialOptionalKeyPath {
7387 getter: Rc::new(move |_arc_rwlock: &Arc<RwLock<Root>>| {
7388 None
7391 }),
7392 value_type_id: self.value_type_id,
7393 _phantom: PhantomData,
7394 }
7395 }
7396
7397 pub fn for_arc_mutex(&self) -> PartialOptionalKeyPath<Arc<Mutex<Root>>>
7401 where
7402 Root: Clone + 'static,
7403 {
7404 PartialOptionalKeyPath {
7407 getter: Rc::new(move |_arc_mutex: &Arc<Mutex<Root>>| {
7408 None
7411 }),
7412 value_type_id: self.value_type_id,
7413 _phantom: PhantomData,
7414 }
7415 }
7416}
7417
7418#[derive(Clone)]
7425pub struct PartialOptionalKeyPath<Root> {
7426 getter: Rc<dyn for<'r> Fn(&'r Root) -> Option<&'r dyn Any>>,
7427 value_type_id: TypeId,
7428 _phantom: PhantomData<Root>,
7429}
7430
7431impl<Root> PartialOptionalKeyPath<Root> {
7432 pub fn new<Value>(keypath: OptionalKeyPath<Root, Value, impl for<'r> Fn(&'r Root) -> Option<&'r Value> + 'static>) -> Self
7433 where
7434 Value: Any + 'static,
7435 Root: 'static,
7436 {
7437 let value_type_id = TypeId::of::<Value>();
7438 let getter = Rc::new(keypath.getter);
7439
7440 Self {
7441 getter: Rc::new(move |root: &Root| {
7442 getter(root).map(|value: &Value| value as &dyn Any)
7443 }),
7444 value_type_id,
7445 _phantom: PhantomData,
7446 }
7447 }
7448
7449 pub fn get<'r>(&self, root: &'r Root) -> Option<&'r dyn Any> {
7450 (self.getter)(root)
7451 }
7452
7453 pub fn value_type_id(&self) -> TypeId {
7455 self.value_type_id
7456 }
7457
7458 pub fn get_as<'a, Value: Any>(&self, root: &'a Root) -> Option<Option<&'a Value>> {
7460 if self.value_type_id == TypeId::of::<Value>() {
7461 self.get(root).map(|any| any.downcast_ref::<Value>())
7462 } else {
7463 None
7464 }
7465 }
7466
7467 pub fn then<MidValue>(
7471 self,
7472 next: PartialOptionalKeyPath<MidValue>,
7473 ) -> PartialOptionalKeyPath<Root>
7474 where
7475 MidValue: Any + 'static,
7476 Root: 'static,
7477 {
7478 let first = self.getter;
7479 let second = next.getter;
7480 let value_type_id = next.value_type_id;
7481
7482 PartialOptionalKeyPath {
7483 getter: Rc::new(move |root: &Root| {
7484 first(root).and_then(|any| {
7485 if let Some(mid_value) = any.downcast_ref::<MidValue>() {
7486 second(mid_value)
7487 } else {
7488 None
7489 }
7490 })
7491 }),
7492 value_type_id,
7493 _phantom: PhantomData,
7494 }
7495 }
7496}
7497
7498#[derive(Clone)]
7504pub struct PartialWritableKeyPath<Root> {
7505 getter: Rc<dyn for<'r> Fn(&'r mut Root) -> &'r mut dyn Any>,
7506 value_type_id: TypeId,
7507 _phantom: PhantomData<Root>,
7508}
7509
7510impl<Root> PartialWritableKeyPath<Root> {
7511 pub fn new<Value>(keypath: WritableKeyPath<Root, Value, impl for<'r> Fn(&'r mut Root) -> &'r mut Value + 'static>) -> Self
7512 where
7513 Value: Any + 'static,
7514 Root: 'static,
7515 {
7516 let value_type_id = TypeId::of::<Value>();
7517 let getter = Rc::new(keypath.getter);
7518
7519 Self {
7520 getter: Rc::new(move |root: &mut Root| {
7521 let value: &mut Value = getter(root);
7522 value as &mut dyn Any
7523 }),
7524 value_type_id,
7525 _phantom: PhantomData,
7526 }
7527 }
7528
7529 pub fn from<Value>(keypath: WritableKeyPath<Root, Value, impl for<'r> Fn(&'r mut Root) -> &'r mut Value + 'static>) -> Self
7532 where
7533 Value: Any + 'static,
7534 Root: 'static,
7535 {
7536 Self::new(keypath)
7537 }
7538
7539 pub fn get_mut<'r>(&self, root: &'r mut Root) -> &'r mut dyn Any {
7540 (self.getter)(root)
7541 }
7542
7543 pub fn value_type_id(&self) -> TypeId {
7545 self.value_type_id
7546 }
7547
7548 pub fn get_mut_as<'a, Value: Any>(&self, root: &'a mut Root) -> Option<&'a mut Value> {
7550 if self.value_type_id == TypeId::of::<Value>() {
7551 self.get_mut(root).downcast_mut::<Value>()
7552 } else {
7553 None
7554 }
7555 }
7556}
7557
7558#[derive(Clone)]
7564pub struct PartialWritableOptionalKeyPath<Root> {
7565 getter: Rc<dyn for<'r> Fn(&'r mut Root) -> Option<&'r mut dyn Any>>,
7566 value_type_id: TypeId,
7567 _phantom: PhantomData<Root>,
7568}
7569
7570impl<Root> PartialWritableOptionalKeyPath<Root> {
7571 pub fn new<Value>(keypath: WritableOptionalKeyPath<Root, Value, impl for<'r> Fn(&'r mut Root) -> Option<&'r mut Value> + 'static>) -> Self
7572 where
7573 Value: Any + 'static,
7574 Root: 'static,
7575 {
7576 let value_type_id = TypeId::of::<Value>();
7577 let getter = Rc::new(keypath.getter);
7578
7579 Self {
7580 getter: Rc::new(move |root: &mut Root| {
7581 getter(root).map(|value: &mut Value| value as &mut dyn Any)
7582 }),
7583 value_type_id,
7584 _phantom: PhantomData,
7585 }
7586 }
7587
7588 pub fn get_mut<'r>(&self, root: &'r mut Root) -> Option<&'r mut dyn Any> {
7589 (self.getter)(root)
7590 }
7591
7592 pub fn value_type_id(&self) -> TypeId {
7594 self.value_type_id
7595 }
7596
7597 pub fn get_mut_as<'a, Value: Any>(&self, root: &'a mut Root) -> Option<Option<&'a mut Value>> {
7599 if self.value_type_id == TypeId::of::<Value>() {
7600 self.get_mut(root).map(|any| any.downcast_mut::<Value>())
7601 } else {
7602 None
7603 }
7604 }
7605}
7606
7607#[derive(Clone)]
7621pub struct AnyKeyPath {
7622 getter: Rc<dyn for<'r> Fn(&'r dyn Any) -> Option<&'r dyn Any>>,
7623 root_type_id: TypeId,
7624 value_type_id: TypeId,
7625}
7626
7627impl AnyKeyPath {
7628 pub fn new<Root, Value>(keypath: OptionalKeyPath<Root, Value, impl for<'r> Fn(&'r Root) -> Option<&'r Value> + 'static>) -> Self
7629 where
7630 Root: Any + 'static,
7631 Value: Any + 'static,
7632 {
7633 let root_type_id = TypeId::of::<Root>();
7634 let value_type_id = TypeId::of::<Value>();
7635 let getter = keypath.getter;
7636
7637 Self {
7638 getter: Rc::new(move |any: &dyn Any| {
7639 if let Some(root) = any.downcast_ref::<Root>() {
7640 getter(root).map(|value: &Value| value as &dyn Any)
7641 } else {
7642 None
7643 }
7644 }),
7645 root_type_id,
7646 value_type_id,
7647 }
7648 }
7649
7650 pub fn from<Root, Value>(keypath: OptionalKeyPath<Root, Value, impl for<'r> Fn(&'r Root) -> Option<&'r Value> + 'static>) -> Self
7653 where
7654 Root: Any + 'static,
7655 Value: Any + 'static,
7656 {
7657 Self::new(keypath)
7658 }
7659
7660 pub fn get<'r>(&self, root: &'r dyn Any) -> Option<&'r dyn Any> {
7661 (self.getter)(root)
7662 }
7663
7664 pub fn root_type_id(&self) -> TypeId {
7666 self.root_type_id
7667 }
7668
7669 pub fn value_type_id(&self) -> TypeId {
7671 self.value_type_id
7672 }
7673
7674 pub fn get_as<'a, Root: Any, Value: Any>(&self, root: &'a Root) -> Option<Option<&'a Value>> {
7676 if self.root_type_id == TypeId::of::<Root>() && self.value_type_id == TypeId::of::<Value>() {
7677 self.get(root as &dyn Any).map(|any| any.downcast_ref::<Value>())
7678 } else {
7679 None
7680 }
7681 }
7682
7683 pub fn kind_name(&self) -> String {
7686 format!("{:?}", self.value_type_id)
7687 }
7688
7689 pub fn for_arc<Root>(&self) -> AnyKeyPath
7691 where
7692 Root: Any + 'static,
7693 {
7694 let root_type_id = self.root_type_id;
7695 let value_type_id = self.value_type_id;
7696 let getter = self.getter.clone();
7697
7698 AnyKeyPath {
7699 getter: Rc::new(move |any: &dyn Any| {
7700 if let Some(arc) = any.downcast_ref::<Arc<Root>>() {
7701 getter(arc.as_ref() as &dyn Any)
7702 } else {
7703 None
7704 }
7705 }),
7706 root_type_id: TypeId::of::<Arc<Root>>(),
7707 value_type_id,
7708 }
7709 }
7710
7711 pub fn for_box<Root>(&self) -> AnyKeyPath
7713 where
7714 Root: Any + 'static,
7715 {
7716 let root_type_id = self.root_type_id;
7717 let value_type_id = self.value_type_id;
7718 let getter = self.getter.clone();
7719
7720 AnyKeyPath {
7721 getter: Rc::new(move |any: &dyn Any| {
7722 if let Some(boxed) = any.downcast_ref::<Box<Root>>() {
7723 getter(boxed.as_ref() as &dyn Any)
7724 } else {
7725 None
7726 }
7727 }),
7728 root_type_id: TypeId::of::<Box<Root>>(),
7729 value_type_id,
7730 }
7731 }
7732
7733 pub fn for_rc<Root>(&self) -> AnyKeyPath
7735 where
7736 Root: Any + 'static,
7737 {
7738 let root_type_id = self.root_type_id;
7739 let value_type_id = self.value_type_id;
7740 let getter = self.getter.clone();
7741
7742 AnyKeyPath {
7743 getter: Rc::new(move |any: &dyn Any| {
7744 if let Some(rc) = any.downcast_ref::<Rc<Root>>() {
7745 getter(rc.as_ref() as &dyn Any)
7746 } else {
7747 None
7748 }
7749 }),
7750 root_type_id: TypeId::of::<Rc<Root>>(),
7751 value_type_id,
7752 }
7753 }
7754
7755 pub fn for_option<Root>(&self) -> AnyKeyPath
7757 where
7758 Root: Any + 'static,
7759 {
7760 let root_type_id = self.root_type_id;
7761 let value_type_id = self.value_type_id;
7762 let getter = self.getter.clone();
7763
7764 AnyKeyPath {
7765 getter: Rc::new(move |any: &dyn Any| {
7766 if let Some(opt) = any.downcast_ref::<Option<Root>>() {
7767 opt.as_ref().and_then(|root| getter(root as &dyn Any))
7768 } else {
7769 None
7770 }
7771 }),
7772 root_type_id: TypeId::of::<Option<Root>>(),
7773 value_type_id,
7774 }
7775 }
7776
7777 pub fn for_result<Root, E>(&self) -> AnyKeyPath
7779 where
7780 Root: Any + 'static,
7781 E: Any + 'static,
7782 {
7783 let root_type_id = self.root_type_id;
7784 let value_type_id = self.value_type_id;
7785 let getter = self.getter.clone();
7786
7787 AnyKeyPath {
7788 getter: Rc::new(move |any: &dyn Any| {
7789 if let Some(result) = any.downcast_ref::<Result<Root, E>>() {
7790 result.as_ref().ok().and_then(|root| getter(root as &dyn Any))
7791 } else {
7792 None
7793 }
7794 }),
7795 root_type_id: TypeId::of::<Result<Root, E>>(),
7796 value_type_id,
7797 }
7798 }
7799
7800 pub fn for_arc_rwlock<Root>(&self) -> AnyKeyPath
7803 where
7804 Root: Any + Clone + 'static,
7805 {
7806 AnyKeyPath {
7809 getter: Rc::new(move |_any: &dyn Any| {
7810 None
7813 }),
7814 root_type_id: TypeId::of::<Arc<RwLock<Root>>>(),
7815 value_type_id: self.value_type_id,
7816 }
7817 }
7818
7819 pub fn for_arc_mutex<Root>(&self) -> AnyKeyPath
7822 where
7823 Root: Any + Clone + 'static,
7824 {
7825 AnyKeyPath {
7828 getter: Rc::new(move |_any: &dyn Any| {
7829 None
7832 }),
7833 root_type_id: TypeId::of::<Arc<Mutex<Root>>>(),
7834 value_type_id: self.value_type_id,
7835 }
7836 }
7837}
7838
7839#[derive(Clone)]
7841pub struct AnyWritableKeyPath {
7842 getter: Rc<dyn for<'r> Fn(&'r mut dyn Any) -> Option<&'r mut dyn Any>>,
7843 root_type_id: TypeId,
7844 value_type_id: TypeId,
7845}
7846
7847#[derive(Clone)]
7852pub struct FailableCombinedKeyPath<Root, Value, ReadFn, WriteFn, OwnedFn>
7853where
7854 ReadFn: for<'r> Fn(&'r Root) -> Option<&'r Value> + 'static,
7855 WriteFn: for<'r> Fn(&'r mut Root) -> Option<&'r mut Value> + 'static,
7856 OwnedFn: Fn(Root) -> Option<Value> + 'static,
7857{
7858 readable: ReadFn,
7859 writable: WriteFn,
7860 owned: OwnedFn,
7861 _phantom: PhantomData<(Root, Value)>,
7862}
7863
7864impl<Root, Value, ReadFn, WriteFn, OwnedFn> FailableCombinedKeyPath<Root, Value, ReadFn, WriteFn, OwnedFn>
7865where
7866 ReadFn: for<'r> Fn(&'r Root) -> Option<&'r Value> + 'static,
7867 WriteFn: for<'r> Fn(&'r mut Root) -> Option<&'r mut Value> + 'static,
7868 OwnedFn: Fn(Root) -> Option<Value> + 'static,
7869{
7870 pub fn new(readable: ReadFn, writable: WriteFn, owned: OwnedFn) -> Self {
7872 Self {
7873 readable,
7874 writable,
7875 owned,
7876 _phantom: PhantomData,
7877 }
7878 }
7879
7880 pub fn get<'r>(&self, root: &'r Root) -> Option<&'r Value> {
7882 (self.readable)(root)
7883 }
7884
7885 pub fn get_mut<'r>(&self, root: &'r mut Root) -> Option<&'r mut Value> {
7887 (self.writable)(root)
7888 }
7889
7890 pub fn get_failable_owned(&self, root: Root) -> Option<Value> {
7892 (self.owned)(root)
7893 }
7894
7895 pub fn to_optional(self) -> OptionalKeyPath<Root, Value, ReadFn> {
7897 OptionalKeyPath::new(self.readable)
7898 }
7899
7900 pub fn to_writable_optional(self) -> WritableOptionalKeyPath<Root, Value, WriteFn> {
7902 WritableOptionalKeyPath::new(self.writable)
7903 }
7904
7905 pub fn then<SubValue, SubReadFn, SubWriteFn, SubOwnedFn>(
7908 self,
7909 next: FailableCombinedKeyPath<Value, SubValue, SubReadFn, SubWriteFn, SubOwnedFn>,
7910 ) -> FailableCombinedKeyPath<Root, SubValue, impl for<'r> Fn(&'r Root) -> Option<&'r SubValue> + 'static, impl for<'r> Fn(&'r mut Root) -> Option<&'r mut SubValue> + 'static, impl Fn(Root) -> Option<SubValue> + 'static>
7911 where
7912 SubReadFn: for<'r> Fn(&'r Value) -> Option<&'r SubValue> + 'static,
7913 SubWriteFn: for<'r> Fn(&'r mut Value) -> Option<&'r mut SubValue> + 'static,
7914 SubOwnedFn: Fn(Value) -> Option<SubValue> + 'static,
7915 ReadFn: 'static,
7916 WriteFn: 'static,
7917 OwnedFn: 'static,
7918 Value: 'static,
7919 Root: 'static,
7920 SubValue: 'static,
7921 {
7922 let first_read = self.readable;
7923 let first_write = self.writable;
7924 let first_owned = self.owned;
7925 let second_read = next.readable;
7926 let second_write = next.writable;
7927 let second_owned = next.owned;
7928
7929 FailableCombinedKeyPath::new(
7930 move |root: &Root| {
7931 first_read(root).and_then(|value| second_read(value))
7932 },
7933 move |root: &mut Root| {
7934 first_write(root).and_then(|value| second_write(value))
7935 },
7936 move |root: Root| {
7937 first_owned(root).and_then(|value| second_owned(value))
7938 },
7939 )
7940 }
7941
7942 pub fn chain_optional<SubValue, SubReadFn>(
7946 self,
7947 next: OptionalKeyPath<Value, SubValue, SubReadFn>,
7948 ) -> FailableCombinedKeyPath<Root, SubValue, impl for<'r> Fn(&'r Root) -> Option<&'r SubValue> + 'static, impl for<'r> Fn(&'r mut Root) -> Option<&'r mut SubValue> + 'static, impl Fn(Root) -> Option<SubValue> + 'static>
7949 where
7950 SubReadFn: for<'r> Fn(&'r Value) -> Option<&'r SubValue> + 'static,
7951 ReadFn: 'static,
7952 WriteFn: 'static,
7953 OwnedFn: 'static,
7954 Value: 'static,
7955 Root: 'static,
7956 SubValue: 'static,
7957 {
7958 let first_read = self.readable;
7959 let first_write = self.writable;
7960 let first_owned = self.owned;
7961 let second_read = next.getter;
7962
7963 FailableCombinedKeyPath::new(
7964 move |root: &Root| {
7965 first_read(root).and_then(|value| second_read(value))
7966 },
7967 move |_root: &mut Root| {
7968 None },
7970 move |root: Root| {
7971 first_owned(root).and_then(|value| {
7972 None
7974 })
7975 },
7976 )
7977 }
7978}
7979
7980impl FailableCombinedKeyPath<(), (), fn(&()) -> Option<&()>, fn(&mut ()) -> Option<&mut ()>, fn(()) -> Option<()>> {
7982 pub fn failable_combined<Root, Value, ReadFn, WriteFn, OwnedFn>(
7984 readable: ReadFn,
7985 writable: WriteFn,
7986 owned: OwnedFn,
7987 ) -> FailableCombinedKeyPath<Root, Value, ReadFn, WriteFn, OwnedFn>
7988 where
7989 ReadFn: for<'r> Fn(&'r Root) -> Option<&'r Value> + 'static,
7990 WriteFn: for<'r> Fn(&'r mut Root) -> Option<&'r mut Value> + 'static,
7991 OwnedFn: Fn(Root) -> Option<Value> + 'static,
7992 {
7993 FailableCombinedKeyPath::new(readable, writable, owned)
7994 }
7995}
7996
7997impl AnyWritableKeyPath {
7998 pub fn new<Root, Value>(keypath: WritableOptionalKeyPath<Root, Value, impl for<'r> Fn(&'r mut Root) -> Option<&'r mut Value> + 'static>) -> Self
7999 where
8000 Root: Any + 'static,
8001 Value: Any + 'static,
8002 {
8003 let root_type_id = TypeId::of::<Root>();
8004 let value_type_id = TypeId::of::<Value>();
8005 let getter = keypath.getter;
8006
8007 Self {
8008 getter: Rc::new(move |any: &mut dyn Any| {
8009 if let Some(root) = any.downcast_mut::<Root>() {
8010 getter(root).map(|value: &mut Value| value as &mut dyn Any)
8011 } else {
8012 None
8013 }
8014 }),
8015 root_type_id,
8016 value_type_id,
8017 }
8018 }
8019
8020 pub fn get_mut<'r>(&self, root: &'r mut dyn Any) -> Option<&'r mut dyn Any> {
8021 (self.getter)(root)
8022 }
8023
8024 pub fn root_type_id(&self) -> TypeId {
8026 self.root_type_id
8027 }
8028
8029 pub fn value_type_id(&self) -> TypeId {
8031 self.value_type_id
8032 }
8033
8034 pub fn get_mut_as<'a, Root: Any, Value: Any>(&self, root: &'a mut Root) -> Option<Option<&'a mut Value>> {
8036 if self.root_type_id == TypeId::of::<Root>() && self.value_type_id == TypeId::of::<Value>() {
8037 self.get_mut(root as &mut dyn Any).map(|any| any.downcast_mut::<Value>())
8038 } else {
8039 None
8040 }
8041 }
8042}
8043
8044impl<Root, Value, F> KeyPath<Root, Value, F>
8046where
8047 F: for<'r> Fn(&'r Root) -> &'r Value + 'static,
8048 Root: 'static,
8049 Value: Any + 'static,
8050{
8051 pub fn to_partial(self) -> PartialKeyPath<Root> {
8053 PartialKeyPath::new(self)
8054 }
8055
8056 pub fn to(self) -> PartialKeyPath<Root> {
8058 self.to_partial()
8059 }
8060}
8061
8062impl<Root, Value, F> OptionalKeyPath<Root, Value, F>
8063where
8064 F: for<'r> Fn(&'r Root) -> Option<&'r Value> + 'static,
8065 Root: Any + 'static,
8066 Value: Any + 'static,
8067{
8068 pub fn to_partial(self) -> PartialOptionalKeyPath<Root> {
8070 PartialOptionalKeyPath::new(self)
8071 }
8072
8073 pub fn to_any(self) -> AnyKeyPath {
8075 AnyKeyPath::new(self)
8076 }
8077
8078 pub fn to(self) -> PartialOptionalKeyPath<Root> {
8080 self.to_partial()
8081 }
8082}
8083
8084impl<Root, Value, F> WritableKeyPath<Root, Value, F>
8085where
8086 F: for<'r> Fn(&'r mut Root) -> &'r mut Value + 'static,
8087 Root: 'static,
8088 Value: Any + 'static,
8089{
8090 pub fn to_partial(self) -> PartialWritableKeyPath<Root> {
8092 PartialWritableKeyPath::new(self)
8093 }
8094
8095 pub fn to(self) -> PartialWritableKeyPath<Root> {
8097 self.to_partial()
8098 }
8099}
8100
8101impl<Root, Value, F> WritableOptionalKeyPath<Root, Value, F>
8102where
8103 F: for<'r> Fn(&'r mut Root) -> Option<&'r mut Value> + 'static,
8104 Root: Any + 'static,
8105 Value: Any + 'static,
8106{
8107 pub fn to_partial(self) -> PartialWritableOptionalKeyPath<Root> {
8109 PartialWritableOptionalKeyPath::new(self)
8110 }
8111
8112 pub fn to_any(self) -> AnyWritableKeyPath {
8114 AnyWritableKeyPath::new(self)
8115 }
8116
8117 pub fn to(self) -> PartialWritableOptionalKeyPath<Root> {
8119 self.to_partial()
8120 }
8121}
8122
8123#[cfg(test)]
8241mod tests {
8242 use super::*;
8243 use std::sync::atomic::{AtomicUsize, Ordering};
8244 use std::rc::Rc;
8245
8246 static ALLOC_COUNT: AtomicUsize = AtomicUsize::new(0);
8248 static DEALLOC_COUNT: AtomicUsize = AtomicUsize::new(0);
8249
8250 #[derive(Debug)]
8252 struct NoCloneType {
8253 id: usize,
8254 data: String,
8255 }
8256
8257 impl NoCloneType {
8258 fn new(data: String) -> Self {
8259 ALLOC_COUNT.fetch_add(1, Ordering::SeqCst);
8260 Self {
8261 id: ALLOC_COUNT.load(Ordering::SeqCst),
8262 data,
8263 }
8264 }
8265 }
8266
8267 impl Clone for NoCloneType {
8268 fn clone(&self) -> Self {
8269 eprintln!("[DEBUG] NoCloneType should not be cloned! ID: {}", self.id);
8270 unreachable!("NoCloneType should not be cloned! ID: {}", self.id);
8271 }
8272 }
8273
8274 impl Drop for NoCloneType {
8275 fn drop(&mut self) {
8276 DEALLOC_COUNT.fetch_add(1, Ordering::SeqCst);
8277 }
8278 }
8279
8280 fn reset_memory_counters() {
8282 ALLOC_COUNT.store(0, Ordering::SeqCst);
8283 DEALLOC_COUNT.store(0, Ordering::SeqCst);
8284 }
8285
8286 fn get_alloc_count() -> usize {
8287 ALLOC_COUNT.load(Ordering::SeqCst)
8288 }
8289
8290 fn get_dealloc_count() -> usize {
8291 DEALLOC_COUNT.load(Ordering::SeqCst)
8292 }
8293
8294#[derive(Debug)]
8296struct User {
8297 name: String,
8298 metadata: Option<Box<UserMetadata>>,
8299 friends: Vec<Arc<User>>,
8300}
8301
8302#[derive(Debug)]
8303struct UserMetadata {
8304 created_at: String,
8305}
8306
8307fn some_fn() {
8308 let akash = User {
8309 name: "Alice".to_string(),
8310 metadata: Some(Box::new(UserMetadata {
8311 created_at: "2024-01-01".to_string(),
8312 })),
8313 friends: vec![
8314 Arc::new(User {
8315 name: "Bob".to_string(),
8316 metadata: None,
8317 friends: vec![],
8318 }),
8319 ],
8320 };
8321
8322 let name_kp = KeyPath::new(|u: &User| &u.name);
8324 let metadata_kp = OptionalKeyPath::new(|u: &User| u.metadata.as_ref());
8325 let friends_kp = KeyPath::new(|u: &User| &u.friends);
8326
8327 println!("Name: {}", name_kp.get(&akash));
8329
8330 if let Some(metadata) = metadata_kp.get(&akash) {
8331 println!("Has metadata: {:?}", metadata);
8332 }
8333
8334 if let Some(first_friend) = akash.friends.get(0) {
8336 println!("First friend: {}", name_kp.get(first_friend));
8337 }
8338
8339 let created_at_kp = KeyPath::new(|m: &UserMetadata| &m.created_at);
8341
8342 if let Some(metadata) = akash.metadata.as_ref() {
8343 let boxed_metadata: &Box<UserMetadata> = metadata;
8345 let unwrapped = boxed_metadata.as_ref();
8346 println!("Created at: {:?}", created_at_kp.get(unwrapped));
8347 }
8348 }
8349
8350 #[test]
8351 fn test_name() {
8352 some_fn();
8353 }
8354
8355 #[test]
8356 fn test_no_cloning_on_keypath_operations() {
8357 reset_memory_counters();
8358
8359 let value = NoCloneType::new("test".to_string());
8361 let boxed = Box::new(value);
8362
8363 let kp = KeyPath::new(|b: &Box<NoCloneType>| b.as_ref());
8365
8366 let _ref = kp.get(&boxed);
8368
8369 let _kp_clone = kp.clone();
8371
8372 let _ref2 = _kp_clone.get(&boxed);
8374
8375 assert_eq!(get_alloc_count(), 1);
8377 }
8378
8379 #[test]
8380 fn test_no_cloning_on_optional_keypath_operations() {
8381 reset_memory_counters();
8382
8383 let value = NoCloneType::new("test".to_string());
8384 let opt = Some(Box::new(value));
8385
8386 let okp = OptionalKeyPath::new(|o: &Option<Box<NoCloneType>>| o.as_ref());
8388
8389 let _ref = okp.get(&opt);
8391
8392 let _okp_clone = okp.clone();
8394
8395 let chained = okp.then(OptionalKeyPath::new(|b: &Box<NoCloneType>| Some(b.as_ref())));
8397 let _ref2 = chained.get(&opt);
8398
8399 assert_eq!(get_alloc_count(), 1);
8400 }
8401
8402 #[test]
8403 fn test_memory_release() {
8404 reset_memory_counters();
8405
8406 {
8407 let value = NoCloneType::new("test".to_string());
8408 let boxed = Box::new(value);
8409 let kp = KeyPath::new(|b: &Box<NoCloneType>| b.as_ref());
8410
8411 let _ref = kp.get(&boxed);
8413
8414 }
8416
8417 assert_eq!(get_alloc_count(), 1);
8420 }
8423
8424 #[test]
8425 fn test_keypath_clone_does_not_clone_underlying_data() {
8426 reset_memory_counters();
8427
8428 let value = NoCloneType::new("data".to_string());
8429 let rc_value = Rc::new(value);
8430
8431 let kp = KeyPath::new(|r: &Rc<NoCloneType>| r.as_ref());
8433
8434 let kp1 = kp.clone();
8436 let kp2 = kp.clone();
8437 let kp3 = kp1.clone();
8438
8439 let _ref1 = kp.get(&rc_value);
8441 let _ref2 = kp1.get(&rc_value);
8442 let _ref3 = kp2.get(&rc_value);
8443 let _ref4 = kp3.get(&rc_value);
8444
8445 assert_eq!(get_alloc_count(), 1);
8447 }
8448
8449 #[test]
8450 fn test_optional_keypath_chaining_no_clone() {
8451 reset_memory_counters();
8452
8453 let value = NoCloneType::new("value1".to_string());
8454
8455 struct Container {
8456 inner: Option<Box<NoCloneType>>,
8457 }
8458
8459 let container = Container {
8460 inner: Some(Box::new(value)),
8461 };
8462
8463 let kp1 = OptionalKeyPath::new(|c: &Container| c.inner.as_ref());
8465 let kp2 = OptionalKeyPath::new(|b: &Box<NoCloneType>| Some(b.as_ref()));
8466
8467 let chained = kp1.then(kp2);
8469
8470 let _result = chained.get(&container);
8472
8473 assert_eq!(get_alloc_count(), 1);
8475 }
8476
8477 #[test]
8478 fn test_for_box_no_clone() {
8479 reset_memory_counters();
8480
8481 let value = NoCloneType::new("test".to_string());
8482 let boxed = Box::new(value);
8483 let opt_boxed = Some(boxed);
8484
8485 let kp = OptionalKeyPath::new(|o: &Option<Box<NoCloneType>>| o.as_ref());
8487 let unwrapped = kp.for_box();
8488
8489 let _ref = unwrapped.get(&opt_boxed);
8491
8492 assert_eq!(get_alloc_count(), 1);
8493 }
8494
8495 #[derive(Debug, PartialEq)]
8498 struct TestUser {
8499 name: String,
8500 age: u32,
8501 metadata: Option<String>,
8502 address: Option<TestAddress>,
8503 }
8504
8505 #[derive(Debug, PartialEq)]
8506 struct TestAddress {
8507 street: String,
8508 city: String,
8509 country: Option<TestCountry>,
8510 }
8511
8512 #[derive(Debug, PartialEq)]
8513 struct TestCountry {
8514 name: String,
8515 }
8516
8517 #[test]
8518 fn test_keypath_macro() {
8519 let user = TestUser {
8520 name: "Alice".to_string(),
8521 age: 30,
8522 metadata: None,
8523 address: None,
8524 };
8525
8526 let name_kp = keypath!(|u: &TestUser| &u.name);
8528 assert_eq!(name_kp.get(&user), "Alice");
8529
8530 let user_with_address = TestUser {
8532 name: "Bob".to_string(),
8533 age: 25,
8534 metadata: None,
8535 address: Some(TestAddress {
8536 street: "123 Main St".to_string(),
8537 city: "New York".to_string(),
8538 country: None,
8539 }),
8540 };
8541
8542 let street_kp = keypath!(|u: &TestUser| &u.address.as_ref().unwrap().street);
8543 assert_eq!(street_kp.get(&user_with_address), "123 Main St");
8544
8545 let user_with_country = TestUser {
8547 name: "Charlie".to_string(),
8548 age: 35,
8549 metadata: None,
8550 address: Some(TestAddress {
8551 street: "456 Oak Ave".to_string(),
8552 city: "London".to_string(),
8553 country: Some(TestCountry {
8554 name: "UK".to_string(),
8555 }),
8556 }),
8557 };
8558
8559 let country_name_kp = keypath!(|u: &TestUser| &u.address.as_ref().unwrap().country.as_ref().unwrap().name);
8560 assert_eq!(country_name_kp.get(&user_with_country), "UK");
8561
8562 let age_kp = keypath!(|u: &TestUser| &u.age);
8564 assert_eq!(age_kp.get(&user), &30);
8565 }
8566
8567 #[test]
8568 fn test_opt_keypath_macro() {
8569 let user = TestUser {
8570 name: "Alice".to_string(),
8571 age: 30,
8572 metadata: Some("admin".to_string()),
8573 address: None,
8574 };
8575
8576 let metadata_kp = opt_keypath!(|u: &TestUser| u.metadata.as_ref());
8578 assert_eq!(metadata_kp.get(&user), Some(&"admin".to_string()));
8579
8580 let user_no_metadata = TestUser {
8582 name: "Bob".to_string(),
8583 age: 25,
8584 metadata: None,
8585 address: None,
8586 };
8587 assert_eq!(metadata_kp.get(&user_no_metadata), None);
8588
8589 let user_with_address = TestUser {
8591 name: "Charlie".to_string(),
8592 age: 35,
8593 metadata: None,
8594 address: Some(TestAddress {
8595 street: "789 Pine Rd".to_string(),
8596 city: "Paris".to_string(),
8597 country: None,
8598 }),
8599 };
8600
8601 let street_kp = opt_keypath!(|u: &TestUser| u.address.as_ref().map(|a| &a.street));
8602 assert_eq!(street_kp.get(&user_with_address), Some(&"789 Pine Rd".to_string()));
8603
8604 let user_with_country = TestUser {
8606 name: "David".to_string(),
8607 age: 40,
8608 metadata: None,
8609 address: Some(TestAddress {
8610 street: "321 Elm St".to_string(),
8611 city: "Tokyo".to_string(),
8612 country: Some(TestCountry {
8613 name: "Japan".to_string(),
8614 }),
8615 }),
8616 };
8617
8618 let country_name_kp = opt_keypath!(|u: &TestUser| u.address.as_ref().and_then(|a| a.country.as_ref().map(|c| &c.name)));
8619 assert_eq!(country_name_kp.get(&user_with_country), Some(&"Japan".to_string()));
8620
8621 let metadata_kp2 = opt_keypath!(|u: &TestUser| u.metadata.as_ref());
8623 assert_eq!(metadata_kp2.get(&user), Some(&"admin".to_string()));
8624 }
8625
8626 #[test]
8627 fn test_writable_keypath_macro() {
8628 let mut user = TestUser {
8629 name: "Alice".to_string(),
8630 age: 30,
8631 metadata: None,
8632 address: None,
8633 };
8634
8635 let name_kp = writable_keypath!(|u: &mut TestUser| &mut u.name);
8637 *name_kp.get_mut(&mut user) = "Bob".to_string();
8638 assert_eq!(user.name, "Bob");
8639
8640 let mut user_with_address = TestUser {
8642 name: "Charlie".to_string(),
8643 age: 25,
8644 metadata: None,
8645 address: Some(TestAddress {
8646 street: "123 Main St".to_string(),
8647 city: "New York".to_string(),
8648 country: None,
8649 }),
8650 };
8651
8652 let street_kp = writable_keypath!(|u: &mut TestUser| &mut u.address.as_mut().unwrap().street);
8653 *street_kp.get_mut(&mut user_with_address) = "456 Oak Ave".to_string();
8654 assert_eq!(user_with_address.address.as_ref().unwrap().street, "456 Oak Ave");
8655
8656 let mut user_with_country = TestUser {
8658 name: "David".to_string(),
8659 age: 35,
8660 metadata: None,
8661 address: Some(TestAddress {
8662 street: "789 Pine Rd".to_string(),
8663 city: "London".to_string(),
8664 country: Some(TestCountry {
8665 name: "UK".to_string(),
8666 }),
8667 }),
8668 };
8669
8670 let country_name_kp = writable_keypath!(|u: &mut TestUser| &mut u.address.as_mut().unwrap().country.as_mut().unwrap().name);
8671 *country_name_kp.get_mut(&mut user_with_country) = "United Kingdom".to_string();
8672 assert_eq!(user_with_country.address.as_ref().unwrap().country.as_ref().unwrap().name, "United Kingdom");
8673
8674 let age_kp = writable_keypath!(|u: &mut TestUser| &mut u.age);
8676 *age_kp.get_mut(&mut user) = 31;
8677 assert_eq!(user.age, 31);
8678 }
8679
8680 #[test]
8681 fn test_writable_opt_keypath_macro() {
8682 let mut user = TestUser {
8683 name: "Alice".to_string(),
8684 age: 30,
8685 metadata: Some("user".to_string()),
8686 address: None,
8687 };
8688
8689 let metadata_kp = writable_opt_keypath!(|u: &mut TestUser| u.metadata.as_mut());
8691 if let Some(metadata) = metadata_kp.get_mut(&mut user) {
8692 *metadata = "admin".to_string();
8693 }
8694 assert_eq!(user.metadata, Some("admin".to_string()));
8695
8696 let mut user_no_metadata = TestUser {
8698 name: "Bob".to_string(),
8699 age: 25,
8700 metadata: None,
8701 address: None,
8702 };
8703 assert_eq!(metadata_kp.get_mut(&mut user_no_metadata), None);
8704
8705 let mut user_with_address = TestUser {
8707 name: "Charlie".to_string(),
8708 age: 35,
8709 metadata: None,
8710 address: Some(TestAddress {
8711 street: "123 Main St".to_string(),
8712 city: "New York".to_string(),
8713 country: None,
8714 }),
8715 };
8716
8717 let street_kp = writable_opt_keypath!(|u: &mut TestUser| u.address.as_mut().map(|a| &mut a.street));
8718 if let Some(street) = street_kp.get_mut(&mut user_with_address) {
8719 *street = "456 Oak Ave".to_string();
8720 }
8721 assert_eq!(user_with_address.address.as_ref().unwrap().street, "456 Oak Ave");
8722
8723 let mut user_with_country = TestUser {
8725 name: "David".to_string(),
8726 age: 40,
8727 metadata: None,
8728 address: Some(TestAddress {
8729 street: "789 Pine Rd".to_string(),
8730 city: "Tokyo".to_string(),
8731 country: Some(TestCountry {
8732 name: "Japan".to_string(),
8733 }),
8734 }),
8735 };
8736
8737 let country_name_kp = writable_opt_keypath!(|u: &mut TestUser| u.address.as_mut().and_then(|a| a.country.as_mut().map(|c| &mut c.name)));
8738 if let Some(country_name) = country_name_kp.get_mut(&mut user_with_country) {
8739 *country_name = "Nippon".to_string();
8740 }
8741 assert_eq!(user_with_country.address.as_ref().unwrap().country.as_ref().unwrap().name, "Nippon");
8742
8743 let metadata_kp2 = writable_opt_keypath!(|u: &mut TestUser| u.metadata.as_mut());
8745 if let Some(metadata) = metadata_kp2.get_mut(&mut user) {
8746 *metadata = "super_admin".to_string();
8747 }
8748 assert_eq!(user.metadata, Some("super_admin".to_string()));
8749 }
8750}
8751
8752pub trait WithContainer<Root, Value> {
8758 fn with_arc<F, R>(&self, arc: &Arc<Root>, f: F) -> R
8760 where
8761 F: FnOnce(&Value) -> R;
8762
8763 fn with_box<F, R>(&self, boxed: &Box<Root>, f: F) -> R
8765 where
8766 F: FnOnce(&Value) -> R;
8767
8768 fn with_box_mut<F, R>(&self, boxed: &mut Box<Root>, f: F) -> R
8770 where
8771 F: FnOnce(&mut Value) -> R;
8772
8773 fn with_rc<F, R>(&self, rc: &Rc<Root>, f: F) -> R
8775 where
8776 F: FnOnce(&Value) -> R;
8777
8778 fn with_result<F, R, E>(&self, result: &Result<Root, E>, f: F) -> Option<R>
8780 where
8781 F: FnOnce(&Value) -> R;
8782
8783 fn with_result_mut<F, R, E>(&self, result: &mut Result<Root, E>, f: F) -> Option<R>
8785 where
8786 F: FnOnce(&mut Value) -> R;
8787
8788 fn with_option<F, R>(&self, option: &Option<Root>, f: F) -> Option<R>
8790 where
8791 F: FnOnce(&Value) -> R;
8792
8793 fn with_option_mut<F, R>(&self, option: &mut Option<Root>, f: F) -> Option<R>
8795 where
8796 F: FnOnce(&mut Value) -> R;
8797
8798 fn with_refcell<F, R>(&self, refcell: &RefCell<Root>, f: F) -> Option<R>
8800 where
8801 F: FnOnce(&Value) -> R;
8802
8803 fn with_refcell_mut<F, R>(&self, refcell: &RefCell<Root>, f: F) -> Option<R>
8805 where
8806 F: FnOnce(&mut Value) -> R;
8807
8808 #[cfg(feature = "tagged")]
8809 fn with_tagged<F, R, Tag>(&self, tagged: &Tagged<Root, Tag>, f: F) -> R
8811 where
8812 Tagged<Root, Tag>: std::ops::Deref<Target = Root>,
8813 F: FnOnce(&Value) -> R;
8814
8815 fn with_mutex<F, R>(&self, mutex: &Mutex<Root>, f: F) -> Option<R>
8817 where
8818 F: FnOnce(&Value) -> R;
8819
8820 fn with_mutex_mut<F, R>(&self, mutex: &mut Mutex<Root>, f: F) -> Option<R>
8822 where
8823 F: FnOnce(&mut Value) -> R;
8824
8825 fn with_rwlock<F, R>(&self, rwlock: &RwLock<Root>, f: F) -> Option<R>
8827 where
8828 F: FnOnce(&Value) -> R;
8829
8830 fn with_rwlock_mut<F, R>(&self, rwlock: &mut RwLock<Root>, f: F) -> Option<R>
8832 where
8833 F: FnOnce(&mut Value) -> R;
8834
8835 fn with_arc_rwlock<F, R>(&self, arc_rwlock: &Arc<RwLock<Root>>, f: F) -> Option<R>
8837 where
8838 F: FnOnce(&Value) -> R;
8839
8840 fn with_arc_rwlock_mut<F, R>(&self, arc_rwlock: &Arc<RwLock<Root>>, f: F) -> Option<R>
8842 where
8843 F: FnOnce(&mut Value) -> R;
8844}
8845
8846impl<Root, Value, F> WithContainer<Root, Value> for KeyPath<Root, Value, F>
8848where
8849 F: for<'r> Fn(&'r Root) -> &'r Value + Clone,
8850{
8851 fn with_arc<Callback, R>(&self, arc: &Arc<Root>, f: Callback) -> R
8852 where
8853 Callback: FnOnce(&Value) -> R,
8854 {
8855 self.with_arc(arc, f)
8856 }
8857
8858 fn with_box<Callback, R>(&self, boxed: &Box<Root>, f: Callback) -> R
8859 where
8860 Callback: FnOnce(&Value) -> R,
8861 {
8862 self.with_box(boxed, f)
8863 }
8864
8865 fn with_box_mut<Callback, R>(&self, _boxed: &mut Box<Root>, _f: Callback) -> R
8866 where
8867 Callback: FnOnce(&mut Value) -> R,
8868 {
8869 eprintln!("[DEBUG] KeyPath does not support mutable access - use WritableKeyPath instead");
8870 unreachable!("KeyPath does not support mutable access - use WritableKeyPath instead")
8871 }
8872
8873 fn with_rc<Callback, R>(&self, rc: &Rc<Root>, f: Callback) -> R
8874 where
8875 Callback: FnOnce(&Value) -> R,
8876 {
8877 self.with_rc(rc, f)
8878 }
8879
8880 fn with_result<Callback, R, E>(&self, result: &Result<Root, E>, f: Callback) -> Option<R>
8881 where
8882 Callback: FnOnce(&Value) -> R,
8883 {
8884 self.with_result(result, f)
8885 }
8886
8887 fn with_result_mut<Callback, R, E>(&self, _result: &mut Result<Root, E>, _f: Callback) -> Option<R>
8888 where
8889 Callback: FnOnce(&mut Value) -> R,
8890 {
8891 None
8892 }
8893
8894 fn with_option<Callback, R>(&self, option: &Option<Root>, f: Callback) -> Option<R>
8895 where
8896 Callback: FnOnce(&Value) -> R,
8897 {
8898 self.with_option(option, f)
8899 }
8900
8901 fn with_option_mut<Callback, R>(&self, _option: &mut Option<Root>, _f: Callback) -> Option<R>
8902 where
8903 Callback: FnOnce(&mut Value) -> R,
8904 {
8905 None
8906 }
8907
8908 fn with_refcell<Callback, R>(&self, refcell: &RefCell<Root>, f: Callback) -> Option<R>
8909 where
8910 Callback: FnOnce(&Value) -> R,
8911 {
8912 self.with_refcell(refcell, f)
8913 }
8914
8915 fn with_refcell_mut<Callback, R>(&self, _refcell: &RefCell<Root>, _f: Callback) -> Option<R>
8916 where
8917 Callback: FnOnce(&mut Value) -> R,
8918 {
8919 None
8920 }
8921
8922 #[cfg(feature = "tagged")]
8923 fn with_tagged<Callback, R, Tag>(&self, tagged: &Tagged<Root, Tag>, f: Callback) -> R
8924 where
8925 Tagged<Root, Tag>: std::ops::Deref<Target = Root>,
8926 Callback: FnOnce(&Value) -> R,
8927 {
8928 self.with_tagged(tagged, f)
8929 }
8930
8931 fn with_mutex<Callback, R>(&self, mutex: &Mutex<Root>, f: Callback) -> Option<R>
8932 where
8933 Callback: FnOnce(&Value) -> R,
8934 {
8935 self.with_mutex(mutex, f)
8936 }
8937
8938 fn with_mutex_mut<Callback, R>(&self, _mutex: &mut Mutex<Root>, _f: Callback) -> Option<R>
8939 where
8940 Callback: FnOnce(&mut Value) -> R,
8941 {
8942 None
8943 }
8944
8945 fn with_rwlock<Callback, R>(&self, rwlock: &RwLock<Root>, f: Callback) -> Option<R>
8946 where
8947 Callback: FnOnce(&Value) -> R,
8948 {
8949 self.with_rwlock(rwlock, f)
8950 }
8951
8952 fn with_rwlock_mut<Callback, R>(&self, _rwlock: &mut RwLock<Root>, _f: Callback) -> Option<R>
8953 where
8954 Callback: FnOnce(&mut Value) -> R,
8955 {
8956 None
8957 }
8958
8959 fn with_arc_rwlock<Callback, R>(&self, arc_rwlock: &Arc<RwLock<Root>>, f: Callback) -> Option<R>
8960 where
8961 Callback: FnOnce(&Value) -> R,
8962 {
8963 self.with_arc_rwlock(arc_rwlock, f)
8964 }
8965
8966 fn with_arc_rwlock_mut<Callback, R>(&self, _arc_rwlock: &Arc<RwLock<Root>>, _f: Callback) -> Option<R>
8967 where
8968 Callback: FnOnce(&mut Value) -> R,
8969 {
8970 None
8971 }
8972}
8973
8974impl<Root, Value, F> WithContainer<Root, Value> for OptionalKeyPath<Root, Value, F>
8976where
8977 F: for<'r> Fn(&'r Root) -> Option<&'r Value> + Clone,
8978{
8979 fn with_arc<Callback, R>(&self, arc: &Arc<Root>, f: Callback) -> R
8980 where
8981 Callback: FnOnce(&Value) -> R,
8982 {
8983 self.with_arc(arc, f)
8984 }
8985
8986 fn with_box<Callback, R>(&self, boxed: &Box<Root>, f: Callback) -> R
8987 where
8988 Callback: FnOnce(&Value) -> R,
8989 {
8990 self.with_box(boxed, f)
8991 }
8992
8993 fn with_box_mut<Callback, R>(&self, _boxed: &mut Box<Root>, _f: Callback) -> R
8994 where
8995 Callback: FnOnce(&mut Value) -> R,
8996 {
8997 eprintln!("[DEBUG] OptionalKeyPath does not support mutable access - use WritableOptionalKeyPath instead");
8998 unreachable!("OptionalKeyPath does not support mutable access - use WritableOptionalKeyPath instead")
8999 }
9000
9001 fn with_rc<Callback, R>(&self, rc: &Rc<Root>, f: Callback) -> R
9002 where
9003 Callback: FnOnce(&Value) -> R,
9004 {
9005 self.with_rc(rc, f)
9006 }
9007
9008 fn with_result<Callback, R, E>(&self, result: &Result<Root, E>, f: Callback) -> Option<R>
9009 where
9010 Callback: FnOnce(&Value) -> R,
9011 {
9012 self.with_result(result, f)
9013 }
9014
9015 fn with_result_mut<Callback, R, E>(&self, _result: &mut Result<Root, E>, _f: Callback) -> Option<R>
9016 where
9017 Callback: FnOnce(&mut Value) -> R,
9018 {
9019 None }
9021
9022 fn with_option<Callback, R>(&self, option: &Option<Root>, f: Callback) -> Option<R>
9023 where
9024 Callback: FnOnce(&Value) -> R,
9025 {
9026 self.with_option(option, f)
9027 }
9028
9029 fn with_option_mut<Callback, R>(&self, _option: &mut Option<Root>, _f: Callback) -> Option<R>
9030 where
9031 Callback: FnOnce(&mut Value) -> R,
9032 {
9033 None }
9035
9036 fn with_refcell<Callback, R>(&self, refcell: &RefCell<Root>, f: Callback) -> Option<R>
9037 where
9038 Callback: FnOnce(&Value) -> R,
9039 {
9040 self.with_refcell(refcell, f)
9041 }
9042
9043 fn with_refcell_mut<Callback, R>(&self, _refcell: &RefCell<Root>, _f: Callback) -> Option<R>
9044 where
9045 Callback: FnOnce(&mut Value) -> R,
9046 {
9047 None }
9049
9050 #[cfg(feature = "tagged")]
9051 fn with_tagged<Callback, R, Tag>(&self, tagged: &Tagged<Root, Tag>, f: Callback) -> R
9052 where
9053 Tagged<Root, Tag>: std::ops::Deref<Target = Root>,
9054 Callback: FnOnce(&Value) -> R,
9055 {
9056 use std::ops::Deref;
9057 self.get(tagged.deref())
9058 .map(|value| f(value))
9059 .expect("OptionalKeyPath::with_tagged: Tagged should always contain a value that matches the keypath")
9060 }
9061
9062 fn with_mutex<Callback, R>(&self, mutex: &Mutex<Root>, f: Callback) -> Option<R>
9063 where
9064 Callback: FnOnce(&Value) -> R,
9065 {
9066 self.with_mutex(mutex, f)
9067 }
9068
9069 fn with_mutex_mut<Callback, R>(&self, _mutex: &mut Mutex<Root>, _f: Callback) -> Option<R>
9070 where
9071 Callback: FnOnce(&mut Value) -> R,
9072 {
9073 None }
9075
9076 fn with_rwlock<Callback, R>(&self, rwlock: &RwLock<Root>, f: Callback) -> Option<R>
9077 where
9078 Callback: FnOnce(&Value) -> R,
9079 {
9080 self.with_rwlock(rwlock, f)
9081 }
9082
9083 fn with_rwlock_mut<Callback, R>(&self, _rwlock: &mut RwLock<Root>, _f: Callback) -> Option<R>
9084 where
9085 Callback: FnOnce(&mut Value) -> R,
9086 {
9087 None }
9089
9090 fn with_arc_rwlock<Callback, R>(&self, arc_rwlock: &Arc<RwLock<Root>>, f: Callback) -> Option<R>
9091 where
9092 Callback: FnOnce(&Value) -> R,
9093 {
9094 self.with_arc_rwlock(arc_rwlock, f)
9095 }
9096
9097 fn with_arc_rwlock_mut<Callback, R>(&self, _arc_rwlock: &Arc<RwLock<Root>>, _f: Callback) -> Option<R>
9098 where
9099 Callback: FnOnce(&mut Value) -> R,
9100 {
9101 None }
9103}
9104
9105impl<Root, Value, F> WithContainer<Root, Value> for WritableKeyPath<Root, Value, F>
9107where
9108 F: for<'r> Fn(&'r mut Root) -> &'r mut Value,
9109{
9110 fn with_arc<Callback, R>(&self, _arc: &Arc<Root>, _f: Callback) -> R
9111 where
9112 Callback: FnOnce(&Value) -> R,
9113 {
9114 eprintln!("[DEBUG] WritableKeyPath::with_arc requires &mut Arc<Root> or interior mutability");
9117 unreachable!("WritableKeyPath::with_arc requires &mut Arc<Root> or interior mutability")
9118 }
9119
9120 fn with_box<Callback, R>(&self, boxed: &Box<Root>, f: Callback) -> R
9121 where
9122 Callback: FnOnce(&Value) -> R,
9123 {
9124 eprintln!("[DEBUG] WritableKeyPath::with_box requires &mut Box<Root> - use with_box_mut instead");
9127 unreachable!("WritableKeyPath::with_box requires &mut Box<Root> - use with_box_mut instead")
9128 }
9129
9130 fn with_box_mut<Callback, R>(&self, boxed: &mut Box<Root>, f: Callback) -> R
9131 where
9132 Callback: FnOnce(&mut Value) -> R,
9133 {
9134 let value = self.get_mut(boxed.as_mut());
9135 f(value)
9136 }
9137
9138 fn with_rc<Callback, R>(&self, _rc: &Rc<Root>, _f: Callback) -> R
9139 where
9140 Callback: FnOnce(&Value) -> R,
9141 {
9142 eprintln!("[DEBUG] WritableKeyPath::with_rc requires &mut Rc<Root> or interior mutability");
9145 unreachable!("WritableKeyPath::with_rc requires &mut Rc<Root> or interior mutability")
9146 }
9147
9148 fn with_result<Callback, R, E>(&self, _result: &Result<Root, E>, _f: Callback) -> Option<R>
9149 where
9150 Callback: FnOnce(&Value) -> R,
9151 {
9152 None
9155 }
9156
9157 fn with_result_mut<Callback, R, E>(&self, result: &mut Result<Root, E>, f: Callback) -> Option<R>
9158 where
9159 Callback: FnOnce(&mut Value) -> R,
9160 {
9161 result.as_mut().ok().map(|root| {
9162 let value = self.get_mut(root);
9163 f(value)
9164 })
9165 }
9166
9167 fn with_option<Callback, R>(&self, _option: &Option<Root>, _f: Callback) -> Option<R>
9168 where
9169 Callback: FnOnce(&Value) -> R,
9170 {
9171 None
9174 }
9175
9176 fn with_option_mut<Callback, R>(&self, option: &mut Option<Root>, f: Callback) -> Option<R>
9177 where
9178 Callback: FnOnce(&mut Value) -> R,
9179 {
9180 option.as_mut().map(|root| {
9181 let value = self.get_mut(root);
9182 f(value)
9183 })
9184 }
9185
9186 fn with_refcell<Callback, R>(&self, refcell: &RefCell<Root>, f: Callback) -> Option<R>
9187 where
9188 Callback: FnOnce(&Value) -> R,
9189 {
9190 None
9193 }
9194
9195 fn with_refcell_mut<Callback, R>(&self, refcell: &RefCell<Root>, f: Callback) -> Option<R>
9196 where
9197 Callback: FnOnce(&mut Value) -> R,
9198 {
9199 refcell.try_borrow_mut().ok().map(|mut borrow| {
9200 let value = self.get_mut(&mut *borrow);
9201 f(value)
9202 })
9203 }
9204
9205 #[cfg(feature = "tagged")]
9206 fn with_tagged<Callback, R, Tag>(&self, _tagged: &Tagged<Root, Tag>, _f: Callback) -> R
9207 where
9208 Tagged<Root, Tag>: std::ops::Deref<Target = Root>,
9209 Callback: FnOnce(&Value) -> R,
9210 {
9211 eprintln!("[DEBUG] WritableKeyPath::with_tagged requires &mut Tagged<Root, Tag> or interior mutability");
9214 unreachable!("WritableKeyPath::with_tagged requires &mut Tagged<Root, Tag> or interior mutability")
9215 }
9216
9217 fn with_mutex<Callback, R>(&self, mutex: &Mutex<Root>, f: Callback) -> Option<R>
9218 where
9219 Callback: FnOnce(&Value) -> R,
9220 {
9221 mutex.lock().ok().map(|mut guard| {
9222 let value = self.get_mut(&mut *guard);
9223 f(value)
9224 })
9225 }
9226
9227 fn with_mutex_mut<Callback, R>(&self, mutex: &mut Mutex<Root>, f: Callback) -> Option<R>
9228 where
9229 Callback: FnOnce(&mut Value) -> R,
9230 {
9231 mutex.get_mut().ok().map(|root| {
9233 let value = self.get_mut(root);
9234 f(value)
9235 })
9236 }
9237
9238 fn with_rwlock<Callback, R>(&self, rwlock: &RwLock<Root>, f: Callback) -> Option<R>
9239 where
9240 Callback: FnOnce(&Value) -> R,
9241 {
9242 None
9245 }
9246
9247 fn with_rwlock_mut<Callback, R>(&self, rwlock: &mut RwLock<Root>, f: Callback) -> Option<R>
9248 where
9249 Callback: FnOnce(&mut Value) -> R,
9250 {
9251 rwlock.get_mut().ok().map(|root| {
9253 let value = self.get_mut(root);
9254 f(value)
9255 })
9256 }
9257
9258 fn with_arc_rwlock<Callback, R>(&self, arc_rwlock: &Arc<RwLock<Root>>, f: Callback) -> Option<R>
9259 where
9260 Callback: FnOnce(&Value) -> R,
9261 {
9262 None
9265 }
9266
9267 fn with_arc_rwlock_mut<Callback, R>(&self, arc_rwlock: &Arc<RwLock<Root>>, f: Callback) -> Option<R>
9268 where
9269 Callback: FnOnce(&mut Value) -> R,
9270 {
9271 arc_rwlock.write().ok().map(|mut guard| {
9272 let value = self.get_mut(&mut *guard);
9273 f(value)
9274 })
9275 }
9276}
9277
9278impl<Root, Value, F> WithContainer<Root, Value> for WritableOptionalKeyPath<Root, Value, F>
9280where
9281 F: for<'r> Fn(&'r mut Root) -> Option<&'r mut Value>,
9282{
9283 fn with_arc<Callback, R>(&self, _arc: &Arc<Root>, _f: Callback) -> R
9284 where
9285 Callback: FnOnce(&Value) -> R,
9286 {
9287 eprintln!("[DEBUG] WritableOptionalKeyPath::with_arc requires &mut Arc<Root> or interior mutability");
9290 unreachable!("WritableOptionalKeyPath::with_arc requires &mut Arc<Root> or interior mutability")
9291 }
9292
9293 fn with_box<Callback, R>(&self, _boxed: &Box<Root>, _f: Callback) -> R
9294 where
9295 Callback: FnOnce(&Value) -> R,
9296 {
9297 eprintln!("[DEBUG] WritableOptionalKeyPath::with_box requires &mut Box<Root> - use with_box_mut instead");
9300 unreachable!("WritableOptionalKeyPath::with_box requires &mut Box<Root> - use with_box_mut instead")
9301 }
9302
9303 fn with_box_mut<Callback, R>(&self, boxed: &mut Box<Root>, f: Callback) -> R
9304 where
9305 Callback: FnOnce(&mut Value) -> R,
9306 {
9307 if let Some(value) = self.get_mut(boxed.as_mut()) {
9308 f(value)
9309 } else {
9310 eprintln!("[DEBUG] WritableOptionalKeyPath failed to get value from Box");
9311 unreachable!("WritableOptionalKeyPath failed to get value from Box")
9312 }
9313 }
9314
9315 fn with_rc<Callback, R>(&self, _rc: &Rc<Root>, _f: Callback) -> R
9316 where
9317 Callback: FnOnce(&Value) -> R,
9318 {
9319 eprintln!("[DEBUG] WritableOptionalKeyPath::with_rc requires &mut Rc<Root> or interior mutability");
9322 unreachable!("WritableOptionalKeyPath::with_rc requires &mut Rc<Root> or interior mutability")
9323 }
9324
9325 fn with_result<Callback, R, E>(&self, _result: &Result<Root, E>, _f: Callback) -> Option<R>
9326 where
9327 Callback: FnOnce(&Value) -> R,
9328 {
9329 None
9332 }
9333
9334 fn with_result_mut<Callback, R, E>(&self, result: &mut Result<Root, E>, f: Callback) -> Option<R>
9335 where
9336 Callback: FnOnce(&mut Value) -> R,
9337 {
9338 result.as_mut().ok().and_then(|root| {
9339 self.get_mut(root).map(|value| f(value))
9340 })
9341 }
9342
9343 fn with_option<Callback, R>(&self, _option: &Option<Root>, _f: Callback) -> Option<R>
9344 where
9345 Callback: FnOnce(&Value) -> R,
9346 {
9347 None
9350 }
9351
9352 fn with_option_mut<Callback, R>(&self, option: &mut Option<Root>, f: Callback) -> Option<R>
9353 where
9354 Callback: FnOnce(&mut Value) -> R,
9355 {
9356 option.as_mut().and_then(|root| {
9357 self.get_mut(root).map(|value| f(value))
9358 })
9359 }
9360
9361 fn with_refcell<Callback, R>(&self, _refcell: &RefCell<Root>, _f: Callback) -> Option<R>
9362 where
9363 Callback: FnOnce(&Value) -> R,
9364 {
9365 None
9368 }
9369
9370 fn with_refcell_mut<Callback, R>(&self, refcell: &RefCell<Root>, f: Callback) -> Option<R>
9371 where
9372 Callback: FnOnce(&mut Value) -> R,
9373 {
9374 refcell.try_borrow_mut().ok().and_then(|mut borrow| {
9375 self.get_mut(&mut *borrow).map(|value| f(value))
9376 })
9377 }
9378
9379 #[cfg(feature = "tagged")]
9380 fn with_tagged<Callback, R, Tag>(&self, _tagged: &Tagged<Root, Tag>, _f: Callback) -> R
9381 where
9382 Tagged<Root, Tag>: std::ops::Deref<Target = Root>,
9383 Callback: FnOnce(&Value) -> R,
9384 {
9385 eprintln!("[DEBUG] WritableOptionalKeyPath::with_tagged requires &mut Tagged<Root, Tag> or interior mutability");
9388 unreachable!("WritableOptionalKeyPath::with_tagged requires &mut Tagged<Root, Tag> or interior mutability")
9389 }
9390
9391 fn with_mutex<Callback, R>(&self, mutex: &Mutex<Root>, f: Callback) -> Option<R>
9392 where
9393 Callback: FnOnce(&Value) -> R,
9394 {
9395 mutex.lock().ok().and_then(|mut guard| {
9396 self.get_mut(&mut *guard).map(|value| f(value))
9397 })
9398 }
9399
9400 fn with_mutex_mut<Callback, R>(&self, mutex: &mut Mutex<Root>, f: Callback) -> Option<R>
9401 where
9402 Callback: FnOnce(&mut Value) -> R,
9403 {
9404 mutex.get_mut().ok().and_then(|root| {
9406 self.get_mut(root).map(|value| f(value))
9407 })
9408 }
9409
9410 fn with_rwlock<Callback, R>(&self, _rwlock: &RwLock<Root>, _f: Callback) -> Option<R>
9411 where
9412 Callback: FnOnce(&Value) -> R,
9413 {
9414 None
9417 }
9418
9419 fn with_rwlock_mut<Callback, R>(&self, rwlock: &mut RwLock<Root>, f: Callback) -> Option<R>
9420 where
9421 Callback: FnOnce(&mut Value) -> R,
9422 {
9423 rwlock.get_mut().ok().and_then(|root| {
9425 self.get_mut(root).map(|value| f(value))
9426 })
9427 }
9428
9429 fn with_arc_rwlock<Callback, R>(&self, _arc_rwlock: &Arc<RwLock<Root>>, _f: Callback) -> Option<R>
9430 where
9431 Callback: FnOnce(&Value) -> R,
9432 {
9433 None
9436 }
9437
9438 fn with_arc_rwlock_mut<Callback, R>(&self, arc_rwlock: &Arc<RwLock<Root>>, f: Callback) -> Option<R>
9439 where
9440 Callback: FnOnce(&mut Value) -> R,
9441 {
9442 arc_rwlock.write().ok().and_then(|mut guard| {
9443 self.get_mut(&mut *guard).map(|value| f(value))
9444 })
9445 }
9446}
9447