1#![no_std]
10
11extern crate alloc;
12
13use core::any::TypeId;
14
15pub mod field_diff;
16pub use field_diff::{FieldDiff, hash_value};
17
18pub trait KeyPathValueTarget {
21 type Target: Sized;
23}
24
25impl<T: Sized> KeyPathValueTarget for &T {
26 type Target = T;
27}
28
29impl<T: Sized> KeyPathValueTarget for &mut T {
30 type Target = T;
31}
32
33pub trait Readable<Root, Value> {
35 fn get(&self, root: Root) -> Option<Value>;
37}
38
39pub trait Writable<MutRoot, MutValue> {
41 fn set(&self, root: MutRoot) -> Option<MutValue>;
43}
44
45pub trait KeyPath<Root, Value, MutRoot, MutValue>:
47 Readable<Root, Value> + Writable<MutRoot, MutValue>
48{
49}
50
51impl<T, Root, Value, MutRoot, MutValue> KeyPath<Root, Value, MutRoot, MutValue> for T where
52 T: Readable<Root, Value> + Writable<MutRoot, MutValue>
53{
54}
55
56pub trait KpTrait<R, V, Root, Value, MutRoot, MutValue>:
58 Readable<Root, Value> + Writable<MutRoot, MutValue>
59{
60 fn type_id_of_root() -> TypeId
62 where
63 R: 'static,
64 {
65 TypeId::of::<R>()
66 }
67
68 fn type_id_of_value() -> TypeId
70 where
71 V: 'static,
72 {
73 TypeId::of::<V>()
74 }
75
76 fn then<SV, SubValue, MutSubValue, Next>(
81 self,
82 next: Next,
83 ) -> impl KeyPath<Root, SubValue, MutRoot, MutSubValue>
84 where
85 Self: Sized,
86 SubValue: core::borrow::Borrow<SV>,
87 MutSubValue: core::borrow::BorrowMut<SV>,
88 Next: Readable<Value, SubValue> + Writable<MutValue, MutSubValue> + Clone;
89}
90
91pub trait RefKpTrait<R, V>:
96 KpTrait<R, V, &'static R, &'static V, &'static mut R, &'static mut V>
97where
98 R: 'static,
99 V: 'static,
100{
101 fn focus<'a>(&self, root: &'a R) -> Option<&'a V>;
102 fn focus_mut<'a>(&self, root: &'a mut R) -> Option<&'a mut V>;
103}
104
105pub trait AccessorTrait<Root, Value, MutRoot, MutValue>:
107 Readable<Root, Value> + Writable<MutRoot, MutValue>
108{
109 fn get_optional(&self, root: Option<Root>) -> Option<Value> {
111 root.and_then(|r| Readable::get(self, r))
112 }
113
114 fn get_mut_optional(&self, root: Option<MutRoot>) -> Option<MutValue> {
116 root.and_then(|r| Writable::set(self, r))
117 }
118
119 fn get_or_else<F>(&self, root: Root, f: F) -> Value
121 where
122 F: FnOnce() -> Value,
123 {
124 Readable::get(self, root).unwrap_or_else(f)
125 }
126
127 fn get_mut_or_else<F>(&self, root: MutRoot, f: F) -> MutValue
129 where
130 F: FnOnce() -> MutValue,
131 {
132 Writable::set(self, root).unwrap_or_else(f)
133 }
134}