1use std::fmt;
14use std::sync::Arc;
15
16pub mod sync_kp;
18pub mod prelude;
19
20pub use sync_kp::{
21 ArcMutexAccess, ArcRwLockAccess, LockAccess, SyncKp, SyncKpType, RcRefCellAccess,
22 StdMutexAccess, StdRwLockAccess,
23};
24
25#[cfg(feature = "parking_lot")]
26pub use sync_kp::{
27 DirectParkingLotMutexAccess, DirectParkingLotRwLockAccess, ParkingLotMutexAccess,
28 ParkingLotRwLockAccess,
29};
30
31#[cfg(feature = "arc-swap")]
32pub use sync_kp::{ArcArcSwapAccess, ArcArcSwapOptionAccess};
33
34pub mod async_lock;
36
37pub mod kptrait;
38
39pub use kptrait::{
40 AccessorTrait, ChainExt, CoercionTrait, HofTrait, KeyPathValueTarget, KpReadable, KpTrait,
41 KPWritable,
42};
43
44#[cfg(feature = "pin_project")]
87pub mod pin;
88
89#[macro_export]
91macro_rules! keypath {
92 { $root:ident . $field:ident } => { $root::$field() };
93 { $root:ident . $field:ident . $($ty:ident . $f:ident).+ } => {
94 $root::$field() $(.then($ty::$f()))+
95 };
96 ($root:ident . $field:ident) => { $root::$field() };
97 ($root:ident . $field:ident . $($ty:ident . $f:ident).+) => {
98 $root::$field() $(.then($ty::$f()))+
99 };
100}
101
102#[macro_export]
106macro_rules! get_or {
107 ($kp:expr, $root:expr, $default:expr) => {
108 $kp.get($root).unwrap_or($default)
109 };
110 ($root:expr => $($path:tt)*, $default:expr) => {
111 $crate::get_or!($crate::keypath!($($path)*), $root, $default)
112 };
113}
114
115#[macro_export]
120macro_rules! get_or_else {
121 ($kp:expr, $root:expr, $closure:expr) => {
122 $kp.get($root).map(|r| r.clone()).unwrap_or_else($closure)
123 };
124 ($root:expr => ($($path:tt)*), $closure:expr) => {
125 $crate::get_or_else!($crate::keypath!($($path)*), $root, $closure)
126 };
127}
128
129#[macro_export]
150macro_rules! zip_with_kp {
151 ($root:expr, $closure:expr => $kp1:expr, $kp2:expr) => {
152 match ($kp1.get($root), $kp2.get($root)) {
153 (Some(__a), Some(__b)) => Some($closure((__a, __b))),
154 _ => None,
155 }
156 };
157 ($root:expr, $closure:expr => $kp1:expr, $kp2:expr, $kp3:expr) => {
158 match ($kp1.get($root), $kp2.get($root), $kp3.get($root)) {
159 (Some(__a), Some(__b), Some(__c)) => Some($closure((__a, __b, __c))),
160 _ => None,
161 }
162 };
163 ($root:expr, $closure:expr => $kp1:expr, $kp2:expr, $kp3:expr, $kp4:expr) => {
164 match (
165 $kp1.get($root),
166 $kp2.get($root),
167 $kp3.get($root),
168 $kp4.get($root),
169 ) {
170 (Some(__a), Some(__b), Some(__c), Some(__d)) => Some($closure((__a, __b, __c, __d))),
171 _ => None,
172 }
173 };
174 ($root:expr, $closure:expr => $kp1:expr, $kp2:expr, $kp3:expr, $kp4:expr, $kp5:expr) => {
175 match (
176 $kp1.get($root),
177 $kp2.get($root),
178 $kp3.get($root),
179 $kp4.get($root),
180 $kp5.get($root),
181 ) {
182 (Some(__a), Some(__b), Some(__c), Some(__d), Some(__e)) => {
183 Some($closure((__a, __b, __c, __d, __e)))
184 }
185 _ => None,
186 }
187 };
188 ($root:expr, $closure:expr => $kp1:expr, $kp2:expr, $kp3:expr, $kp4:expr, $kp5:expr, $kp6:expr) => {
189 match (
190 $kp1.get($root),
191 $kp2.get($root),
192 $kp3.get($root),
193 $kp4.get($root),
194 $kp5.get($root),
195 $kp6.get($root),
196 ) {
197 (Some(__a), Some(__b), Some(__c), Some(__d), Some(__e), Some(__f)) => {
198 Some($closure((__a, __b, __c, __d, __e, __f)))
199 }
200 _ => None,
201 }
202 };
203}
204
205pub type KpValue<'a, R, V> = Kp<
207 R,
208 V,
209 &'a R,
210 V, &'a mut R,
212 V, for<'b> fn(&'b R) -> Option<V>,
214 for<'b> fn(&'b mut R) -> Option<V>,
215>;
216
217pub type KpOwned<R, V> = Kp<
219 R,
220 V,
221 R,
222 V, R,
224 V, fn(R) -> Option<V>,
226 fn(R) -> Option<V>,
227>;
228
229pub type KpRoot<R> = Kp<
231 R,
232 R,
233 R,
234 R, R,
236 R, fn(R) -> Option<R>,
238 fn(R) -> Option<R>,
239>;
240
241pub type KpVoid = Kp<(), (), (), (), (), (), fn() -> Option<()>, fn() -> Option<()>>;
243
244pub type KpDynamic<R, V> = Kp<
245 R,
246 V,
247 &'static R,
248 &'static V,
249 &'static mut R,
250 &'static mut V,
251 Box<dyn for<'a> Fn(&'a R) -> Option<&'a V> + Send + Sync>,
252 Box<dyn for<'a> Fn(&'a mut R) -> Option<&'a mut V> + Send + Sync>,
253>;
254
255pub type KpBox<'a, R, V> = Kp<
256 R,
257 V,
258 &'a R,
259 &'a V,
260 &'a mut R,
261 &'a mut V,
262 Box<dyn Fn(&'a R) -> Option<&'a V> + 'a>,
263 Box<dyn Fn(&'a mut R) -> Option<&'a mut V> + 'a>,
264>;
265
266pub type KpArc<'a, R, V> = Kp<
267 R,
268 V,
269 &'a R,
270 &'a V,
271 &'a mut R,
272 &'a mut V,
273 Arc<dyn Fn(&'a R) -> Option<&'a V> + Send + Sync + 'a>,
274 Arc<dyn Fn(&'a mut R) -> Option<&'a mut V> + Send + Sync + 'a>,
275>;
276
277pub type KpType<'a, R, V> = Kp<
278 R,
279 V,
280 &'a R,
281 &'a V,
282 &'a mut R,
283 &'a mut V,
284 for<'b> fn(&'b R) -> Option<&'b V>,
285 for<'b> fn(&'b mut R) -> Option<&'b mut V>,
286>;
287
288pub type KpTraitType<'a, R, V> = dyn KpTrait<
289 R,
290 V,
291 &'a R,
292 &'a V,
293 &'a mut R,
294 &'a mut V,
295 for<'b> fn(&'b R) -> Option<&'b V>,
296 for<'b> fn(&'b mut R) -> Option<&'b mut V>,
297 >;
298
299pub type KpOptionRefCellType<'a, R, V> = Kp<
302 R,
303 V,
304 &'a R,
305 std::cell::Ref<'a, V>,
306 &'a mut R,
307 std::cell::RefMut<'a, V>,
308 for<'b> fn(&'b R) -> Option<std::cell::Ref<'b, V>>,
309 for<'b> fn(&'b mut R) -> Option<std::cell::RefMut<'b, V>>,
310>;
311
312impl<'a, R, V> KpType<'a, R, V> {
313 #[inline]
315 pub fn to_dynamic(self) -> KpDynamic<R, V> {
316 self.into()
317 }
318}
319
320impl<'a, R, V> From<KpType<'a, R, V>> for KpDynamic<R, V> {
321 #[inline]
322 fn from(kp: KpType<'a, R, V>) -> Self {
323 let get_fn = kp.get;
324 let set_fn = kp.set;
325 Kp::new(
326 Box::new(move |t: &R| get_fn(t)),
327 Box::new(move |t: &mut R| set_fn(t)),
328 )
329 }
330}
331
332impl<R, V, Root, Value, MutRoot, MutValue, G, S> Kp<R, V, Root, Value, MutRoot, MutValue, G, S>
333where
334 Root: std::borrow::Borrow<R>,
335 Value: std::borrow::Borrow<V>,
336 MutRoot: std::borrow::BorrowMut<R>,
337 MutValue: std::borrow::BorrowMut<V>,
338 G: Fn(Root) -> Option<Value> + Send + Sync + 'static,
339 S: Fn(MutRoot) -> Option<MutValue> + Send + Sync + 'static,
340 R: 'static,
341 V: 'static,
342{
343 #[inline]
357 pub fn into_dynamic(self) -> KpDynamic<R, V> {
358 let g = self.get;
359 let s = self.set;
360 Kp::new(
361 Box::new(move |t: &R| unsafe {
362 let root: Root = std::mem::transmute_copy(&t);
365 match g(root) {
366 None => None,
367 Some(v) => {
368 let r: &V = std::borrow::Borrow::borrow(&v);
369 Some(std::mem::transmute::<&V, &V>(r))
371 }
372 }
373 }),
374 Box::new(move |t: &mut R| unsafe {
375 let root: MutRoot = std::mem::transmute_copy(&t);
377 match s(root) {
378 None => None,
379 Some(mut v) => {
380 let r: &mut V = std::borrow::BorrowMut::borrow_mut(&mut v);
381 Some(std::mem::transmute::<&mut V, &mut V>(r))
382 }
383 }
384 }),
385 )
386 }
387}
388
389pub type KpComposed<R, V> = Kp<
425 R,
426 V,
427 &'static R,
428 &'static V,
429 &'static mut R,
430 &'static mut V,
431 Box<dyn for<'b> Fn(&'b R) -> Option<&'b V> + Send + Sync>,
432 Box<dyn for<'b> Fn(&'b mut R) -> Option<&'b mut V> + Send + Sync>,
433>;
434
435impl<R, V>
436 Kp<
437 R,
438 V,
439 &'static R,
440 &'static V,
441 &'static mut R,
442 &'static mut V,
443 Box<dyn for<'b> Fn(&'b R) -> Option<&'b V> + Send + Sync>,
444 Box<dyn for<'b> Fn(&'b mut R) -> Option<&'b mut V> + Send + Sync>,
445 >
446{
447 pub fn from_closures<G, S>(get: G, set: S) -> Self
450 where
451 G: for<'b> Fn(&'b R) -> Option<&'b V> + Send + Sync + 'static,
452 S: for<'b> Fn(&'b mut R) -> Option<&'b mut V> + Send + Sync + 'static,
453 {
454 Self::new(Box::new(get), Box::new(set))
455 }
456}
457
458pub struct AKp {
459 getter: Rc<dyn for<'r> Fn(&'r dyn Any) -> Option<&'r dyn Any>>,
460 root_type_id: TypeId,
461 value_type_id: TypeId,
462}
463
464impl AKp {
465 pub fn new<'a, R, V>(keypath: KpType<'a, R, V>) -> Self
467 where
468 R: Any + 'static,
469 V: Any + 'static,
470 {
471 let root_type_id = TypeId::of::<R>();
472 let value_type_id = TypeId::of::<V>();
473 let getter_fn = keypath.get;
474
475 Self {
476 getter: Rc::new(move |any: &dyn Any| {
477 if let Some(root) = any.downcast_ref::<R>() {
478 getter_fn(root).map(|value: &V| value as &dyn Any)
479 } else {
480 None
481 }
482 }),
483 root_type_id,
484 value_type_id,
485 }
486 }
487
488 pub fn from<'a, R, V>(keypath: KpType<'a, R, V>) -> Self
490 where
491 R: Any + 'static,
492 V: Any + 'static,
493 {
494 Self::new(keypath)
495 }
496
497 pub fn get<'r>(&self, root: &'r dyn Any) -> Option<&'r dyn Any> {
499 (self.getter)(root)
500 }
501
502 pub fn root_type_id(&self) -> TypeId {
504 self.root_type_id
505 }
506
507 pub fn value_type_id(&self) -> TypeId {
509 self.value_type_id
510 }
511
512 pub fn get_as<'a, Root: Any, Value: Any>(&self, root: &'a Root) -> Option<Option<&'a Value>> {
514 if self.root_type_id == TypeId::of::<Root>() && self.value_type_id == TypeId::of::<Value>()
515 {
516 Some(
517 self.get(root as &dyn Any)
518 .and_then(|any| any.downcast_ref::<Value>()),
519 )
520 } else {
521 None
522 }
523 }
524
525 pub fn kind_name(&self) -> String {
527 format!("{:?}", self.value_type_id)
528 }
529
530 pub fn root_kind_name(&self) -> String {
532 format!("{:?}", self.root_type_id)
533 }
534
535 pub fn for_arc<Root>(&self) -> AKp
537 where
538 Root: Any + 'static,
539 {
540 let value_type_id = self.value_type_id;
541 let getter = self.getter.clone();
542
543 AKp {
544 getter: Rc::new(move |any: &dyn Any| {
545 if let Some(arc) = any.downcast_ref::<Arc<Root>>() {
546 getter(arc.as_ref() as &dyn Any)
547 } else {
548 None
549 }
550 }),
551 root_type_id: TypeId::of::<Arc<Root>>(),
552 value_type_id,
553 }
554 }
555
556 pub fn for_box<Root>(&self) -> AKp
558 where
559 Root: Any + 'static,
560 {
561 let value_type_id = self.value_type_id;
562 let getter = self.getter.clone();
563
564 AKp {
565 getter: Rc::new(move |any: &dyn Any| {
566 if let Some(boxed) = any.downcast_ref::<Box<Root>>() {
567 getter(boxed.as_ref() as &dyn Any)
568 } else {
569 None
570 }
571 }),
572 root_type_id: TypeId::of::<Box<Root>>(),
573 value_type_id,
574 }
575 }
576
577 pub fn for_rc<Root>(&self) -> AKp
579 where
580 Root: Any + 'static,
581 {
582 let value_type_id = self.value_type_id;
583 let getter = self.getter.clone();
584
585 AKp {
586 getter: Rc::new(move |any: &dyn Any| {
587 if let Some(rc) = any.downcast_ref::<Rc<Root>>() {
588 getter(rc.as_ref() as &dyn Any)
589 } else {
590 None
591 }
592 }),
593 root_type_id: TypeId::of::<Rc<Root>>(),
594 value_type_id,
595 }
596 }
597
598 pub fn for_option<Root>(&self) -> AKp
600 where
601 Root: Any + 'static,
602 {
603 let value_type_id = self.value_type_id;
604 let getter = self.getter.clone();
605
606 AKp {
607 getter: Rc::new(move |any: &dyn Any| {
608 if let Some(opt) = any.downcast_ref::<Option<Root>>() {
609 opt.as_ref().and_then(|root| getter(root as &dyn Any))
610 } else {
611 None
612 }
613 }),
614 root_type_id: TypeId::of::<Option<Root>>(),
615 value_type_id,
616 }
617 }
618
619 pub fn for_result<Root, E>(&self) -> AKp
621 where
622 Root: Any + 'static,
623 E: Any + 'static,
624 {
625 let value_type_id = self.value_type_id;
626 let getter = self.getter.clone();
627
628 AKp {
629 getter: Rc::new(move |any: &dyn Any| {
630 if let Some(result) = any.downcast_ref::<Result<Root, E>>() {
631 result
632 .as_ref()
633 .ok()
634 .and_then(|root| getter(root as &dyn Any))
635 } else {
636 None
637 }
638 }),
639 root_type_id: TypeId::of::<Result<Root, E>>(),
640 value_type_id,
641 }
642 }
643
644 pub fn map<Root, OrigValue, MappedValue, F>(&self, mapper: F) -> AKp
657 where
658 Root: Any + 'static,
659 OrigValue: Any + 'static,
660 MappedValue: Any + 'static,
661 F: Fn(&OrigValue) -> MappedValue + 'static,
662 {
663 let orig_root_type_id = self.root_type_id;
664 let orig_value_type_id = self.value_type_id;
665 let getter = self.getter.clone();
666 let mapped_type_id = TypeId::of::<MappedValue>();
667
668 AKp {
669 getter: Rc::new(move |any_root: &dyn Any| {
670 if any_root.type_id() == orig_root_type_id {
672 getter(any_root).and_then(|any_value| {
673 if orig_value_type_id == TypeId::of::<OrigValue>() {
675 any_value.downcast_ref::<OrigValue>().map(|orig_val| {
676 let mapped = mapper(orig_val);
677 Box::leak(Box::new(mapped)) as &dyn Any
679 })
680 } else {
681 None
682 }
683 })
684 } else {
685 None
686 }
687 }),
688 root_type_id: orig_root_type_id,
689 value_type_id: mapped_type_id,
690 }
691 }
692
693 pub fn filter<Root, Value, F>(&self, predicate: F) -> AKp
706 where
707 Root: Any + 'static,
708 Value: Any + 'static,
709 F: Fn(&Value) -> bool + 'static,
710 {
711 let orig_root_type_id = self.root_type_id;
712 let orig_value_type_id = self.value_type_id;
713 let getter = self.getter.clone();
714
715 AKp {
716 getter: Rc::new(move |any_root: &dyn Any| {
717 if any_root.type_id() == orig_root_type_id {
719 getter(any_root).filter(|any_value| {
720 if orig_value_type_id == TypeId::of::<Value>() {
722 any_value
723 .downcast_ref::<Value>()
724 .map(|val| predicate(val))
725 .unwrap_or(false)
726 } else {
727 false
728 }
729 })
730 } else {
731 None
732 }
733 }),
734 root_type_id: orig_root_type_id,
735 value_type_id: orig_value_type_id,
736 }
737 }
738}
739
740impl fmt::Debug for AKp {
741 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
742 f.debug_struct("AKp")
743 .field("root_type_id", &self.root_type_id)
744 .field("value_type_id", &self.value_type_id)
745 .finish_non_exhaustive()
746 }
747}
748
749impl fmt::Display for AKp {
750 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
751 write!(
752 f,
753 "AKp(root_type_id={:?}, value_type_id={:?})",
754 self.root_type_id, self.value_type_id
755 )
756 }
757}
758
759pub struct PKp<Root> {
760 getter: Rc<dyn for<'r> Fn(&'r Root) -> Option<&'r dyn Any>>,
761 value_type_id: TypeId,
762 _phantom: std::marker::PhantomData<Root>,
763}
764
765impl<Root> PKp<Root>
766where
767 Root: 'static,
768{
769 pub fn new<'a, V>(keypath: KpType<'a, Root, V>) -> Self
771 where
772 V: Any + 'static,
773 {
774 let value_type_id = TypeId::of::<V>();
775 let getter_fn = keypath.get;
776
777 Self {
778 getter: Rc::new(move |root: &Root| getter_fn(root).map(|val: &V| val as &dyn Any)),
779 value_type_id,
780 _phantom: std::marker::PhantomData,
781 }
782 }
783
784 pub fn from<'a, V>(keypath: KpType<'a, Root, V>) -> Self
786 where
787 V: Any + 'static,
788 {
789 Self::new(keypath)
790 }
791
792 pub fn get<'r>(&self, root: &'r Root) -> Option<&'r dyn Any> {
794 (self.getter)(root)
795 }
796
797 pub fn value_type_id(&self) -> TypeId {
799 self.value_type_id
800 }
801
802 pub fn get_as<'a, Value: Any>(&self, root: &'a Root) -> Option<&'a Value> {
804 if self.value_type_id == TypeId::of::<Value>() {
805 self.get(root).and_then(|any| any.downcast_ref::<Value>())
806 } else {
807 None
808 }
809 }
810
811 pub fn kind_name(&self) -> String {
813 format!("{:?}", self.value_type_id)
814 }
815
816 pub fn for_arc(&self) -> PKp<Arc<Root>> {
818 let getter = self.getter.clone();
819 let value_type_id = self.value_type_id;
820
821 PKp {
822 getter: Rc::new(move |arc: &Arc<Root>| getter(arc.as_ref())),
823 value_type_id,
824 _phantom: std::marker::PhantomData,
825 }
826 }
827
828 pub fn for_box(&self) -> PKp<Box<Root>> {
830 let getter = self.getter.clone();
831 let value_type_id = self.value_type_id;
832
833 PKp {
834 getter: Rc::new(move |boxed: &Box<Root>| getter(boxed.as_ref())),
835 value_type_id,
836 _phantom: std::marker::PhantomData,
837 }
838 }
839
840 pub fn for_rc(&self) -> PKp<Rc<Root>> {
842 let getter = self.getter.clone();
843 let value_type_id = self.value_type_id;
844
845 PKp {
846 getter: Rc::new(move |rc: &Rc<Root>| getter(rc.as_ref())),
847 value_type_id,
848 _phantom: std::marker::PhantomData,
849 }
850 }
851
852 pub fn for_option(&self) -> PKp<Option<Root>> {
854 let getter = self.getter.clone();
855 let value_type_id = self.value_type_id;
856
857 PKp {
858 getter: Rc::new(move |opt: &Option<Root>| opt.as_ref().and_then(|root| getter(root))),
859 value_type_id,
860 _phantom: std::marker::PhantomData,
861 }
862 }
863
864 pub fn for_result<E>(&self) -> PKp<Result<Root, E>>
866 where
867 E: 'static,
868 {
869 let getter = self.getter.clone();
870 let value_type_id = self.value_type_id;
871
872 PKp {
873 getter: Rc::new(move |result: &Result<Root, E>| {
874 result.as_ref().ok().and_then(|root| getter(root))
875 }),
876 value_type_id,
877 _phantom: std::marker::PhantomData,
878 }
879 }
880
881 pub fn map<OrigValue, MappedValue, F>(&self, mapper: F) -> PKp<Root>
895 where
896 OrigValue: Any + 'static,
897 MappedValue: Any + 'static,
898 F: Fn(&OrigValue) -> MappedValue + 'static,
899 {
900 let orig_type_id = self.value_type_id;
901 let getter = self.getter.clone();
902 let mapped_type_id = TypeId::of::<MappedValue>();
903
904 PKp {
905 getter: Rc::new(move |root: &Root| {
906 getter(root).and_then(|any_value| {
907 if orig_type_id == TypeId::of::<OrigValue>() {
909 any_value.downcast_ref::<OrigValue>().map(|orig_val| {
910 let mapped = mapper(orig_val);
911 Box::leak(Box::new(mapped)) as &dyn Any
914 })
915 } else {
916 None
917 }
918 })
919 }),
920 value_type_id: mapped_type_id,
921 _phantom: std::marker::PhantomData,
922 }
923 }
924
925 pub fn filter<Value, F>(&self, predicate: F) -> PKp<Root>
939 where
940 Value: Any + 'static,
941 F: Fn(&Value) -> bool + 'static,
942 {
943 let orig_type_id = self.value_type_id;
944 let getter = self.getter.clone();
945
946 PKp {
947 getter: Rc::new(move |root: &Root| {
948 getter(root).filter(|any_value| {
949 if orig_type_id == TypeId::of::<Value>() {
951 any_value
952 .downcast_ref::<Value>()
953 .map(|val| predicate(val))
954 .unwrap_or(false)
955 } else {
956 false
957 }
958 })
959 }),
960 value_type_id: orig_type_id,
961 _phantom: std::marker::PhantomData,
962 }
963 }
964}
965
966impl<Root> fmt::Debug for PKp<Root> {
967 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
968 f.debug_struct("PKp")
969 .field("root_ty", &std::any::type_name::<Root>())
970 .field("value_type_id", &self.value_type_id)
971 .finish_non_exhaustive()
972 }
973}
974
975impl<Root> fmt::Display for PKp<Root> {
976 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
977 write!(
978 f,
979 "PKp<{}, value_type_id={:?}>",
980 std::any::type_name::<Root>(),
981 self.value_type_id
982 )
983 }
984}
985
986#[derive(Clone)]
999pub struct Kp<R, V, Root, Value, MutRoot, MutValue, G, S>
1000where
1001 Root: std::borrow::Borrow<R>,
1002 MutRoot: std::borrow::BorrowMut<R>,
1003 MutValue: std::borrow::BorrowMut<V>,
1004 G: Fn(Root) -> Option<Value>,
1005 S: Fn(MutRoot) -> Option<MutValue>,
1006{
1007 get: G,
1009 set: S,
1011 _p: std::marker::PhantomData<(R, V, Root, Value, MutRoot, MutValue)>,
1012}
1013
1014#[inline]
1016pub fn constrain_get<R, V, F>(f: F) -> F
1017where
1018 F: for<'b> Fn(&'b R) -> Option<&'b V>,
1019{
1020 f
1021}
1022
1023#[inline]
1025pub fn constrain_set<R, V, F>(f: F) -> F
1026where
1027 F: for<'b> Fn(&'b mut R) -> Option<&'b mut V>,
1028{
1029 f
1030}
1031
1032impl<R, V, Root, Value, MutRoot, MutValue, G, S> Kp<R, V, Root, Value, MutRoot, MutValue, G, S>
1033where
1034 Root: std::borrow::Borrow<R>,
1035 Value: std::borrow::Borrow<V>,
1036 MutRoot: std::borrow::BorrowMut<R>,
1037 MutValue: std::borrow::BorrowMut<V>,
1038 G: Fn(Root) -> Option<Value>,
1039 S: Fn(MutRoot) -> Option<MutValue>,
1040{
1041 pub fn new(get: G, set: S) -> Self {
1042 Self {
1043 get,
1044 set,
1045 _p: std::marker::PhantomData,
1046 }
1047 }
1048
1049 #[inline]
1052 pub fn get(&self, root: Root) -> Option<Value> {
1053 (self.get)(root)
1054 }
1055
1056 #[inline]
1058 pub fn get_mut(&self, root: MutRoot) -> Option<MutValue> {
1059 (self.set)(root)
1060 }
1061
1062 #[inline]
1065 pub fn get_ref<'a>(&self, root: &'a R) -> Option<&'a V>
1066 where
1067 G: for<'b> Fn(&'b R) -> Option<&'b V>,
1068 {
1069 (self.get)(root)
1070 }
1071
1072 #[inline]
1074 pub fn get_mut_ref<'a>(&self, root: &'a mut R) -> Option<&'a mut V>
1075 where
1076 S: for<'b> Fn(&'b mut R) -> Option<&'b mut V>,
1077 {
1078 (self.set)(root)
1079 }
1080
1081 #[inline]
1082 pub fn then<SV, G2, S2>(
1083 self,
1084 next: Kp<
1085 V,
1086 SV,
1087 &'static V, &'static SV,
1089 &'static mut V,
1090 &'static mut SV,
1091 G2,
1092 S2,
1093 >,
1094 ) -> Kp<
1095 R,
1096 SV,
1097 &'static R,
1098 &'static SV,
1099 &'static mut R,
1100 &'static mut SV,
1101 impl for<'b> Fn(&'b R) -> Option<&'b SV>,
1102 impl for<'b> Fn(&'b mut R) -> Option<&'b mut SV>,
1103 >
1104 where
1105 G: for<'b> Fn(&'b R) -> Option<&'b V>,
1106 S: for<'b> Fn(&'b mut R) -> Option<&'b mut V>,
1107 G2: for<'b> Fn(&'b V) -> Option<&'b SV>,
1108 S2: for<'b> Fn(&'b mut V) -> Option<&'b mut SV>,
1109 {
1110 let first_get = self.get;
1111 let first_set = self.set;
1112 let second_get = next.get;
1113 let second_set = next.set;
1114
1115 Kp::new(
1116 constrain_get(move |root: &R| first_get(root).and_then(|value| second_get(value))),
1117 constrain_set(move |root: &mut R| first_set(root).and_then(|value| second_set(value))),
1118 )
1119 }
1120
1121}
1122
1123impl<R, V, Root, Value, MutRoot, MutValue, G, S> fmt::Debug
1124 for Kp<R, V, Root, Value, MutRoot, MutValue, G, S>
1125where
1126 Root: std::borrow::Borrow<R>,
1127 Value: std::borrow::Borrow<V>,
1128 MutRoot: std::borrow::BorrowMut<R>,
1129 MutValue: std::borrow::BorrowMut<V>,
1130 G: Fn(Root) -> Option<Value>,
1131 S: Fn(MutRoot) -> Option<MutValue>,
1132{
1133 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1134 f.debug_struct("Kp")
1135 .field("root_ty", &std::any::type_name::<R>())
1136 .field("value_ty", &std::any::type_name::<V>())
1137 .finish_non_exhaustive()
1138 }
1139}
1140
1141impl<R, V, Root, Value, MutRoot, MutValue, G, S> fmt::Display
1142 for Kp<R, V, Root, Value, MutRoot, MutValue, G, S>
1143where
1144 Root: std::borrow::Borrow<R>,
1145 Value: std::borrow::Borrow<V>,
1146 MutRoot: std::borrow::BorrowMut<R>,
1147 MutValue: std::borrow::BorrowMut<V>,
1148 G: Fn(Root) -> Option<Value>,
1149 S: Fn(MutRoot) -> Option<MutValue>,
1150{
1151 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1152 write!(
1153 f,
1154 "Kp<{}, {}>",
1155 std::any::type_name::<R>(),
1156 std::any::type_name::<V>()
1157 )
1158 }
1159}
1160
1161pub fn zip_kps<'a, RootType, Value1, Value2>(
1175 kp1: &'a KpType<'a, RootType, Value1>,
1176 kp2: &'a KpType<'a, RootType, Value2>,
1177) -> impl Fn(&'a RootType) -> Option<(&'a Value1, &'a Value2)> + 'a
1178where
1179 RootType: 'a,
1180 Value1: 'a,
1181 Value2: 'a,
1182{
1183 move |root: &'a RootType| {
1184 let val1 = (kp1.get)(root)?;
1185 let val2 = (kp2.get)(root)?;
1186 Some((val1, val2))
1187 }
1188}
1189
1190impl<R, Root, MutRoot, G, S> Kp<R, R, Root, Root, MutRoot, MutRoot, G, S>
1191where
1192 Root: std::borrow::Borrow<R>,
1193 MutRoot: std::borrow::BorrowMut<R>,
1194 G: Fn(Root) -> Option<Root>,
1195 S: Fn(MutRoot) -> Option<MutRoot>,
1196{
1197 pub fn identity_typed() -> Kp<
1198 R,
1199 R,
1200 Root,
1201 Root,
1202 MutRoot,
1203 MutRoot,
1204 fn(Root) -> Option<Root>,
1205 fn(MutRoot) -> Option<MutRoot>,
1206 > {
1207 Kp::new(|r: Root| Some(r), |r: MutRoot| Some(r))
1208 }
1209
1210 pub fn identity<'a>() -> KpType<'a, R, R> {
1211 KpType::new(|r| Some(r), |r| Some(r))
1212 }
1213}
1214
1215pub struct EnumKp<Enum, Variant, Root, Value, MutRoot, MutValue, G, S, E>
1224where
1225 Root: std::borrow::Borrow<Enum>,
1226 Value: std::borrow::Borrow<Variant>,
1227 MutRoot: std::borrow::BorrowMut<Enum>,
1228 MutValue: std::borrow::BorrowMut<Variant>,
1229 G: Fn(Root) -> Option<Value>,
1230 S: Fn(MutRoot) -> Option<MutValue>,
1231 E: Fn(Variant) -> Enum,
1232{
1233 extractor: Kp<Enum, Variant, Root, Value, MutRoot, MutValue, G, S>,
1234 embedder: E,
1235}
1236
1237unsafe impl<Enum, Variant, Root, Value, MutRoot, MutValue, G, S, E> Send
1239 for EnumKp<Enum, Variant, Root, Value, MutRoot, MutValue, G, S, E>
1240where
1241 Root: std::borrow::Borrow<Enum>,
1242 Value: std::borrow::Borrow<Variant>,
1243 MutRoot: std::borrow::BorrowMut<Enum>,
1244 MutValue: std::borrow::BorrowMut<Variant>,
1245 G: Fn(Root) -> Option<Value> + Send,
1246 S: Fn(MutRoot) -> Option<MutValue> + Send,
1247 E: Fn(Variant) -> Enum + Send,
1248{
1249}
1250unsafe impl<Enum, Variant, Root, Value, MutRoot, MutValue, G, S, E> Sync
1251 for EnumKp<Enum, Variant, Root, Value, MutRoot, MutValue, G, S, E>
1252where
1253 Root: std::borrow::Borrow<Enum>,
1254 Value: std::borrow::Borrow<Variant>,
1255 MutRoot: std::borrow::BorrowMut<Enum>,
1256 MutValue: std::borrow::BorrowMut<Variant>,
1257 G: Fn(Root) -> Option<Value> + Sync,
1258 S: Fn(MutRoot) -> Option<MutValue> + Sync,
1259 E: Fn(Variant) -> Enum + Sync,
1260{
1261}
1262
1263impl<Enum, Variant, Root, Value, MutRoot, MutValue, G, S, E>
1264 EnumKp<Enum, Variant, Root, Value, MutRoot, MutValue, G, S, E>
1265where
1266 Root: std::borrow::Borrow<Enum>,
1267 Value: std::borrow::Borrow<Variant>,
1268 MutRoot: std::borrow::BorrowMut<Enum>,
1269 MutValue: std::borrow::BorrowMut<Variant>,
1270 G: Fn(Root) -> Option<Value>,
1271 S: Fn(MutRoot) -> Option<MutValue>,
1272 E: Fn(Variant) -> Enum,
1273{
1274 pub fn new(
1276 extractor: Kp<Enum, Variant, Root, Value, MutRoot, MutValue, G, S>,
1277 embedder: E,
1278 ) -> Self {
1279 Self {
1280 extractor,
1281 embedder,
1282 }
1283 }
1284
1285 pub fn get(&self, enum_value: Root) -> Option<Value> {
1287 (self.extractor.get)(enum_value)
1288 }
1289
1290 pub fn get_mut(&self, enum_value: MutRoot) -> Option<MutValue> {
1292 (self.extractor.set)(enum_value)
1293 }
1294
1295 pub fn embed(&self, value: Variant) -> Enum {
1297 (self.embedder)(value)
1298 }
1299
1300 pub fn as_kp(&self) -> &Kp<Enum, Variant, Root, Value, MutRoot, MutValue, G, S> {
1302 &self.extractor
1303 }
1304
1305 pub fn into_kp(self) -> Kp<Enum, Variant, Root, Value, MutRoot, MutValue, G, S> {
1307 self.extractor
1308 }
1309
1310 pub fn map<MappedValue, F>(
1321 &self,
1322 mapper: F,
1323 ) -> EnumKp<
1324 Enum,
1325 MappedValue,
1326 Root,
1327 MappedValue,
1328 MutRoot,
1329 MappedValue,
1330 impl Fn(Root) -> Option<MappedValue>,
1331 impl Fn(MutRoot) -> Option<MappedValue>,
1332 impl Fn(MappedValue) -> Enum,
1333 >
1334 where
1335 F: Fn(&Variant) -> MappedValue + Copy + 'static,
1338 Variant: 'static,
1339 MappedValue: 'static,
1340 E: Fn(Variant) -> Enum + Copy + 'static,
1342 {
1343 let mapped_extractor = self.extractor.map(mapper);
1344
1345 let new_embedder = move |_value: MappedValue| -> Enum {
1349 panic!(
1350 "Cannot embed mapped values back into enum. Use the original EnumKp for embedding."
1351 )
1352 };
1353
1354 EnumKp::new(mapped_extractor, new_embedder)
1355 }
1356
1357 pub fn filter<F>(
1369 &self,
1370 predicate: F,
1371 ) -> EnumKp<
1372 Enum,
1373 Variant,
1374 Root,
1375 Value,
1376 MutRoot,
1377 MutValue,
1378 impl Fn(Root) -> Option<Value>,
1379 impl Fn(MutRoot) -> Option<MutValue>,
1380 E,
1381 >
1382 where
1383 F: Fn(&Variant) -> bool + Copy + 'static,
1386 Variant: 'static,
1387 E: Copy,
1389 {
1390 let filtered_extractor = self.extractor.filter(predicate);
1391 EnumKp::new(filtered_extractor, self.embedder)
1392 }
1393}
1394
1395impl<Enum, Variant, Root, Value, MutRoot, MutValue, G, S, E> fmt::Debug
1396 for EnumKp<Enum, Variant, Root, Value, MutRoot, MutValue, G, S, E>
1397where
1398 Root: std::borrow::Borrow<Enum>,
1399 Value: std::borrow::Borrow<Variant>,
1400 MutRoot: std::borrow::BorrowMut<Enum>,
1401 MutValue: std::borrow::BorrowMut<Variant>,
1402 G: Fn(Root) -> Option<Value>,
1403 S: Fn(MutRoot) -> Option<MutValue>,
1404 E: Fn(Variant) -> Enum,
1405{
1406 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1407 f.debug_struct("EnumKp")
1408 .field("enum_ty", &std::any::type_name::<Enum>())
1409 .field("variant_ty", &std::any::type_name::<Variant>())
1410 .finish_non_exhaustive()
1411 }
1412}
1413
1414impl<Enum, Variant, Root, Value, MutRoot, MutValue, G, S, E> fmt::Display
1415 for EnumKp<Enum, Variant, Root, Value, MutRoot, MutValue, G, S, E>
1416where
1417 Root: std::borrow::Borrow<Enum>,
1418 Value: std::borrow::Borrow<Variant>,
1419 MutRoot: std::borrow::BorrowMut<Enum>,
1420 MutValue: std::borrow::BorrowMut<Variant>,
1421 G: Fn(Root) -> Option<Value>,
1422 S: Fn(MutRoot) -> Option<MutValue>,
1423 E: Fn(Variant) -> Enum,
1424{
1425 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1426 write!(
1427 f,
1428 "EnumKp<{}, {}>",
1429 std::any::type_name::<Enum>(),
1430 std::any::type_name::<Variant>()
1431 )
1432 }
1433}
1434
1435pub type EnumKpType<'a, Enum, Variant> = EnumKp<
1437 Enum,
1438 Variant,
1439 &'a Enum,
1440 &'a Variant,
1441 &'a mut Enum,
1442 &'a mut Variant,
1443 for<'b> fn(&'b Enum) -> Option<&'b Variant>,
1444 for<'b> fn(&'b mut Enum) -> Option<&'b mut Variant>,
1445 fn(Variant) -> Enum,
1446>;
1447
1448pub fn enum_variant<'a, Enum, Variant>(
1466 getter: for<'b> fn(&'b Enum) -> Option<&'b Variant>,
1467 setter: for<'b> fn(&'b mut Enum) -> Option<&'b mut Variant>,
1468 embedder: fn(Variant) -> Enum,
1469) -> EnumKpType<'a, Enum, Variant> {
1470 EnumKp::new(Kp::new(getter, setter), embedder)
1471}
1472
1473pub fn enum_ok<'a, T, E>() -> EnumKpType<'a, Result<T, E>, T> {
1483 EnumKp::new(
1484 Kp::new(
1485 |r: &Result<T, E>| r.as_ref().ok(),
1486 |r: &mut Result<T, E>| r.as_mut().ok(),
1487 ),
1488 |t: T| Ok(t),
1489 )
1490}
1491
1492pub fn enum_err<'a, T, E>() -> EnumKpType<'a, Result<T, E>, E> {
1502 EnumKp::new(
1503 Kp::new(
1504 |r: &Result<T, E>| r.as_ref().err(),
1505 |r: &mut Result<T, E>| r.as_mut().err(),
1506 ),
1507 |e: E| Err(e),
1508 )
1509}
1510
1511pub fn enum_some<'a, T>() -> EnumKpType<'a, Option<T>, T> {
1521 EnumKp::new(
1522 Kp::new(|o: &Option<T>| o.as_ref(), |o: &mut Option<T>| o.as_mut()),
1523 |t: T| Some(t),
1524 )
1525}
1526
1527pub fn variant_of<'a, Enum, Variant>(
1545 getter: for<'b> fn(&'b Enum) -> Option<&'b Variant>,
1546 setter: for<'b> fn(&'b mut Enum) -> Option<&'b mut Variant>,
1547 embedder: fn(Variant) -> Enum,
1548) -> EnumKpType<'a, Enum, Variant> {
1549 enum_variant(getter, setter, embedder)
1550}
1551
1552pub fn kp_box<'a, T>() -> KpType<'a, Box<T>, T> {
1565 Kp::new(
1566 |b: &Box<T>| Some(b.as_ref()),
1567 |b: &mut Box<T>| Some(b.as_mut()),
1568 )
1569}
1570
1571pub fn kp_arc<'a, T>() -> Kp<
1582 Arc<T>,
1583 T,
1584 &'a Arc<T>,
1585 &'a T,
1586 &'a mut Arc<T>,
1587 &'a mut T,
1588 for<'b> fn(&'b Arc<T>) -> Option<&'b T>,
1589 for<'b> fn(&'b mut Arc<T>) -> Option<&'b mut T>,
1590> {
1591 Kp::new(
1592 |arc: &Arc<T>| Some(arc.as_ref()),
1593 |arc: &mut Arc<T>| Arc::get_mut(arc),
1594 )
1595}
1596
1597pub fn kp_rc<'a, T>() -> Kp<
1608 std::rc::Rc<T>,
1609 T,
1610 &'a std::rc::Rc<T>,
1611 &'a T,
1612 &'a mut std::rc::Rc<T>,
1613 &'a mut T,
1614 for<'b> fn(&'b std::rc::Rc<T>) -> Option<&'b T>,
1615 for<'b> fn(&'b mut std::rc::Rc<T>) -> Option<&'b mut T>,
1616> {
1617 Kp::new(
1618 |rc: &std::rc::Rc<T>| Some(rc.as_ref()),
1619 |rc: &mut std::rc::Rc<T>| std::rc::Rc::get_mut(rc),
1620 )
1621}
1622
1623use std::any::{Any, TypeId};
1626use std::rc::Rc;