rust_key_paths/lib.rs
1// pub use key_paths_core::*;
2pub use rust_keypaths::*;
3
4use rust_keypaths::{
5 KeyPath as RustKeyPath,
6 OptionalKeyPath as RustOptionalKeyPath,
7 WritableKeyPath as RustWritableKeyPath,
8 WritableOptionalKeyPath as RustWritableOptionalKeyPath,
9 PartialKeyPath as RustPartialKeyPath,
10 PartialOptionalKeyPath as RustPartialOptionalKeyPath,
11 PartialWritableKeyPath as RustPartialWritableKeyPath,
12 PartialWritableOptionalKeyPath as RustPartialWritableOptionalKeyPath,
13 AnyKeyPath as RustAnyKeyPath,
14 AnyWritableKeyPath as RustAnyWritableKeyPath,
15};
16
17// ========== Basic KeyPath Enum ==========
18
19/// Enum for basic keypath types (KeyPath, OptionalKeyPath, WritableKeyPath, WritableOptionalKeyPath).
20///
21/// Provides syntactic sugar for functions accepting any basic keypath type.
22///
23/// # Example
24///
25/// ```rust,ignore
26/// fn process_keypath<Root>(kp: KP<Root>) {
27/// match kp {
28/// KP::KeyPath(k) => { /* handle KeyPath */ },
29/// KP::OptionalKeyPath(k) => { /* handle OptionalKeyPath */ },
30/// KP::WritableKeyPath(k) => { /* handle WritableKeyPath */ },
31/// KP::WritableOptionalKeyPath(k) => { /* handle WritableOptionalKeyPath */ },
32/// }
33/// }
34/// ```
35pub enum KP<Root> {
36 /// A readable keypath that always succeeds.
37 KeyPath(RustPartialKeyPath<Root>),
38
39 /// A readable keypath that may return `None`.
40 OptionalKeyPath(RustPartialOptionalKeyPath<Root>),
41
42 /// A writable keypath that always succeeds.
43 WritableKeyPath(RustPartialWritableKeyPath<Root>),
44
45 /// A writable keypath that may return `None`.
46 WritableOptionalKeyPath(RustPartialWritableOptionalKeyPath<Root>),
47}
48
49impl<Root> KP<Root> {
50 /// Convert from a concrete `rust_keypaths::KeyPath`.
51 pub fn from_keypath<Value, F>(kp: RustKeyPath<Root, Value, F>) -> Self
52 where
53 F: for<'r> Fn(&'r Root) -> &'r Value + 'static,
54 Root: 'static,
55 Value: std::any::Any + 'static,
56 {
57 Self::KeyPath(kp.to_partial())
58 }
59
60 /// Convert from a concrete `rust_keypaths::OptionalKeyPath`.
61 pub fn from_optional_keypath<Value, F>(kp: RustOptionalKeyPath<Root, Value, F>) -> Self
62 where
63 F: for<'r> Fn(&'r Root) -> Option<&'r Value> + 'static,
64 Root: 'static,
65 Value: std::any::Any + 'static,
66 {
67 Self::OptionalKeyPath(kp.to_partial())
68 }
69
70 /// Convert from a concrete `rust_keypaths::WritableKeyPath`.
71 pub fn from_writable_keypath<Value, F>(kp: RustWritableKeyPath<Root, Value, F>) -> Self
72 where
73 F: for<'r> Fn(&'r mut Root) -> &'r mut Value + 'static,
74 Root: 'static,
75 Value: std::any::Any + 'static,
76 {
77 Self::WritableKeyPath(kp.to_partial())
78 }
79
80 /// Convert from a concrete `rust_keypaths::WritableOptionalKeyPath`.
81 pub fn from_writable_optional_keypath<Value, F>(kp: RustWritableOptionalKeyPath<Root, Value, F>) -> Self
82 where
83 F: for<'r> Fn(&'r mut Root) -> Option<&'r mut Value> + 'static,
84 Root: 'static,
85 Value: std::any::Any + 'static,
86 {
87 Self::WritableOptionalKeyPath(kp.to_partial())
88 }
89}
90
91// From implementations for easier conversion
92impl<Root, Value, F> From<RustKeyPath<Root, Value, F>> for KP<Root>
93where
94 F: for<'r> Fn(&'r Root) -> &'r Value + 'static,
95 Root: 'static,
96 Value: std::any::Any + 'static,
97{
98 fn from(kp: RustKeyPath<Root, Value, F>) -> Self {
99 Self::from_keypath(kp)
100 }
101}
102
103impl<Root, Value, F> From<RustOptionalKeyPath<Root, Value, F>> for KP<Root>
104where
105 F: for<'r> Fn(&'r Root) -> Option<&'r Value> + 'static,
106 Root: 'static,
107 Value: std::any::Any + 'static,
108{
109 fn from(kp: RustOptionalKeyPath<Root, Value, F>) -> Self {
110 Self::from_optional_keypath(kp)
111 }
112}
113
114impl<Root, Value, F> From<RustWritableKeyPath<Root, Value, F>> for KP<Root>
115where
116 F: for<'r> Fn(&'r mut Root) -> &'r mut Value + 'static,
117 Root: 'static,
118 Value: std::any::Any + 'static,
119{
120 fn from(kp: RustWritableKeyPath<Root, Value, F>) -> Self {
121 Self::from_writable_keypath(kp)
122 }
123}
124
125impl<Root, Value, F> From<RustWritableOptionalKeyPath<Root, Value, F>> for KP<Root>
126where
127 F: for<'r> Fn(&'r mut Root) -> Option<&'r mut Value> + 'static,
128 Root: 'static,
129 Value: std::any::Any + 'static,
130{
131 fn from(kp: RustWritableOptionalKeyPath<Root, Value, F>) -> Self {
132 Self::from_writable_optional_keypath(kp)
133 }
134}
135
136// From implementations for PKP enum
137impl<Root> From<RustPartialKeyPath<Root>> for PKP<Root> {
138 fn from(kp: RustPartialKeyPath<Root>) -> Self {
139 Self::PartialKeyPath(kp)
140 }
141}
142
143impl<Root> From<RustPartialOptionalKeyPath<Root>> for PKP<Root> {
144 fn from(kp: RustPartialOptionalKeyPath<Root>) -> Self {
145 Self::PartialOptionalKeyPath(kp)
146 }
147}
148
149impl<Root> From<RustPartialWritableKeyPath<Root>> for PKP<Root> {
150 fn from(kp: RustPartialWritableKeyPath<Root>) -> Self {
151 Self::PartialWritableKeyPath(kp)
152 }
153}
154
155impl<Root> From<RustPartialWritableOptionalKeyPath<Root>> for PKP<Root> {
156 fn from(kp: RustPartialWritableOptionalKeyPath<Root>) -> Self {
157 Self::PartialWritableOptionalKeyPath(kp)
158 }
159}
160
161// From implementations for AKP enum
162impl From<RustAnyKeyPath> for AKP {
163 fn from(kp: RustAnyKeyPath) -> Self {
164 Self::AnyKeyPath(kp)
165 }
166}
167
168impl From<RustAnyWritableKeyPath> for AKP {
169 fn from(kp: RustAnyWritableKeyPath) -> Self {
170 Self::AnyWritableKeyPath(kp)
171 }
172}
173
174// ========== PartialKeyPath Enum ==========
175
176/// Enum for partial keypath types (PartialKeyPath, PartialOptionalKeyPath, PartialWritableKeyPath, PartialWritableOptionalKeyPath).
177///
178/// Provides syntactic sugar for functions accepting any partial keypath type.
179///
180/// # Example
181///
182/// ```rust,ignore
183/// fn process_partial_keypath<Root>(pkp: PKP<Root>) {
184/// match pkp {
185/// PKP::PartialKeyPath(k) => { /* handle PartialKeyPath */ },
186/// PKP::PartialOptionalKeyPath(k) => { /* handle PartialOptionalKeyPath */ },
187/// PKP::PartialWritableKeyPath(k) => { /* handle PartialWritableKeyPath */ },
188/// PKP::PartialWritableOptionalKeyPath(k) => { /* handle PartialWritableOptionalKeyPath */ },
189/// }
190/// }
191/// ```
192pub enum PKP<Root> {
193 /// Type-erased keypath with known Root but unknown Value.
194 PartialKeyPath(RustPartialKeyPath<Root>),
195
196 /// Type-erased optional keypath with known Root.
197 PartialOptionalKeyPath(RustPartialOptionalKeyPath<Root>),
198
199 /// Type-erased writable keypath with known Root.
200 PartialWritableKeyPath(RustPartialWritableKeyPath<Root>),
201
202 /// Type-erased writable optional keypath with known Root.
203 PartialWritableOptionalKeyPath(RustPartialWritableOptionalKeyPath<Root>),
204}
205
206// ========== AnyKeyPath Enum ==========
207
208/// Enum for fully type-erased keypath types (AnyKeyPath, AnyWritableKeyPath).
209///
210/// Provides syntactic sugar for functions accepting any fully type-erased keypath type.
211///
212/// # Example
213///
214/// ```rust,ignore
215/// fn process_any_keypath(anykp: AKP) {
216/// match anykp {
217/// AKP::AnyKeyPath(k) => { /* handle AnyKeyPath */ },
218/// AKP::AnyWritableKeyPath(k) => { /* handle AnyWritableKeyPath */ },
219/// }
220/// }
221/// ```
222pub enum AKP {
223 /// Fully type-erased keypath (unknown Root and Value).
224 AnyKeyPath(RustAnyKeyPath),
225
226 /// Fully type-erased writable keypath.
227 AnyWritableKeyPath(RustAnyWritableKeyPath),
228}
229
230// ========== Chain KeyPath Enums ==========
231
232/// Enum for Arc<Mutex<T>> chain keypath types.
233///
234/// Represents all chain types that traverse through `Arc<Mutex<T>>`:
235/// - ArcMutexKeyPathChain
236/// - ArcMutexWritableKeyPathChain
237/// - ArcMutexOptionalKeyPathChain
238/// - ArcMutexWritableOptionalKeyPathChain
239///
240/// The outer keypath (to the `Arc<Mutex<T>>`) is stored as a `PartialKeyPath`.
241pub enum AMKP<Root> {
242 /// Chain through `Arc<Mutex<T>>` with readable inner keypath.
243 ArcMutexKeyPathChain(RustPartialKeyPath<Root>),
244
245 /// Chain through `Arc<Mutex<T>>` with optional readable inner keypath.
246 ArcMutexOptionalKeyPathChain(RustPartialKeyPath<Root>),
247}
248
249/// Enum for Arc<RwLock<T>> chain keypath types.
250///
251/// Represents all chain types that traverse through `Arc<RwLock<T>>`:
252/// - ArcRwLockKeyPathChain
253/// - ArcRwLockWritableKeyPathChain
254/// - ArcRwLockOptionalKeyPathChain
255/// - ArcRwLockWritableOptionalKeyPathChain
256///
257/// The outer keypath (to the `Arc<RwLock<T>>`) is stored as a `PartialKeyPath`.
258pub enum ARKP<Root> {
259 /// Chain through `Arc<RwLock<T>>` with readable inner keypath.
260 ArcRwLockKeyPathChain(RustPartialKeyPath<Root>),
261
262 /// Chain through `Arc<RwLock<T>>` with optional readable inner keypath.
263 ArcRwLockOptionalKeyPathChain(RustPartialKeyPath<Root>),
264}
265
266/// Enum for optional Arc<Mutex<T>> chain keypath types.
267///
268/// Represents all chain types that traverse through optional `Arc<Mutex<T>>`:
269/// - OptionalArcMutexKeyPathChain
270/// - OptionalArcMutexWritableKeyPathChain
271/// - OptionalArcMutexOptionalKeyPathChain
272/// - OptionalArcMutexWritableOptionalKeyPathChain
273///
274/// The outer keypath (to the optional `Arc<Mutex<T>>`) is stored as a `PartialOptionalKeyPath`.
275pub enum OAMKP<Root> {
276 /// Chain through optional `Arc<Mutex<T>>` with readable inner keypath.
277 OptionalArcMutexKeyPathChain(RustPartialOptionalKeyPath<Root>),
278
279 /// Chain through optional `Arc<Mutex<T>>` with optional readable inner keypath.
280 OptionalArcMutexOptionalKeyPathChain(RustPartialOptionalKeyPath<Root>),
281}
282
283/// Enum for optional Arc<RwLock<T>> chain keypath types.
284///
285/// Represents all chain types that traverse through optional `Arc<RwLock<T>>`:
286/// - OptionalArcRwLockKeyPathChain
287/// - OptionalArcRwLockWritableKeyPathChain
288/// - OptionalArcRwLockOptionalKeyPathChain
289/// - OptionalArcRwLockWritableOptionalKeyPathChain
290///
291/// The outer keypath (to the optional `Arc<RwLock<T>>`) is stored as a `PartialOptionalKeyPath`.
292pub enum OARKP<Root> {
293 /// Chain through optional `Arc<RwLock<T>>` with readable inner keypath.
294 OptionalArcRwLockKeyPathChain(RustPartialOptionalKeyPath<Root>),
295
296 /// Chain through optional `Arc<RwLock<T>>` with optional readable inner keypath.
297 OptionalArcRwLockOptionalKeyPathChain(RustPartialOptionalKeyPath<Root>),
298}
299
300/// Enum for Arc<parking_lot::Mutex<T>> chain keypath types.
301///
302/// Represents all chain types that traverse through `Arc<parking_lot::Mutex<T>>`:
303/// - ArcParkingMutexKeyPathChain
304/// - ArcParkingMutexWritableKeyPathChain
305/// - ArcParkingMutexOptionalKeyPathChain
306/// - ArcParkingMutexWritableOptionalKeyPathChain
307///
308/// Requires the `parking_lot` feature to be enabled.
309/// The outer keypath (to the `Arc<parking_lot::Mutex<T>>`) is stored as a `PartialKeyPath`.
310#[cfg(feature = "parking_lot")]
311pub enum APMKP<Root> {
312 /// Chain through `Arc<parking_lot::Mutex<T>>` with readable inner keypath.
313 ArcParkingMutexKeyPathChain(RustPartialKeyPath<Root>),
314
315 /// Chain through `Arc<parking_lot::Mutex<T>>` with optional readable inner keypath.
316 ArcParkingMutexOptionalKeyPathChain(RustPartialKeyPath<Root>),
317}
318
319/// Enum for Arc<parking_lot::RwLock<T>> chain keypath types.
320///
321/// Represents all chain types that traverse through `Arc<parking_lot::RwLock<T>>`:
322/// - ArcParkingRwLockKeyPathChain
323/// - ArcParkingRwLockWritableKeyPathChain
324/// - ArcParkingRwLockOptionalKeyPathChain
325/// - ArcParkingRwLockWritableOptionalKeyPathChain
326///
327/// Requires the `parking_lot` feature to be enabled.
328/// The outer keypath (to the `Arc<parking_lot::RwLock<T>>`) is stored as a `PartialKeyPath`.
329#[cfg(feature = "parking_lot")]
330pub enum APRKP<Root> {
331 /// Chain through `Arc<parking_lot::RwLock<T>>` with readable inner keypath.
332 ArcParkingRwLockKeyPathChain(RustPartialKeyPath<Root>),
333
334 /// Chain through `Arc<parking_lot::RwLock<T>>` with optional readable inner keypath.
335 ArcParkingRwLockOptionalKeyPathChain(RustPartialKeyPath<Root>),
336}
337
338/// Enum for optional Arc<parking_lot::Mutex<T>> chain keypath types.
339///
340/// Represents all chain types that traverse through optional `Arc<parking_lot::Mutex<T>>`:
341/// - OptionalArcParkingMutexKeyPathChain
342/// - OptionalArcParkingMutexWritableKeyPathChain
343/// - OptionalArcParkingMutexOptionalKeyPathChain
344/// - OptionalArcParkingMutexWritableOptionalKeyPathChain
345///
346/// Requires the `parking_lot` feature to be enabled.
347/// The outer keypath (to the optional `Arc<parking_lot::Mutex<T>>`) is stored as a `PartialOptionalKeyPath`.
348#[cfg(feature = "parking_lot")]
349pub enum OAPMKP<Root> {
350 /// Chain through optional `Arc<parking_lot::Mutex<T>>` with readable inner keypath.
351 OptionalArcParkingMutexKeyPathChain(RustPartialOptionalKeyPath<Root>),
352
353 /// Chain through optional `Arc<parking_lot::Mutex<T>>` with optional readable inner keypath.
354 OptionalArcParkingMutexOptionalKeyPathChain(RustPartialOptionalKeyPath<Root>),
355}
356
357/// Enum for optional Arc<parking_lot::RwLock<T>> chain keypath types.
358///
359/// Represents all chain types that traverse through optional `Arc<parking_lot::RwLock<T>>`:
360/// - OptionalArcParkingRwLockKeyPathChain
361/// - OptionalArcParkingRwLockWritableKeyPathChain
362/// - OptionalArcParkingRwLockOptionalKeyPathChain
363/// - OptionalArcParkingRwLockWritableOptionalKeyPathChain
364///
365/// Requires the `parking_lot` feature to be enabled.
366/// The outer keypath (to the optional `Arc<parking_lot::RwLock<T>>`) is stored as a `PartialOptionalKeyPath`.
367#[cfg(feature = "parking_lot")]
368pub enum OAPRKP<Root> {
369 /// Chain through optional `Arc<parking_lot::RwLock<T>>` with readable inner keypath.
370 OptionalArcParkingRwLockKeyPathChain(RustPartialOptionalKeyPath<Root>),
371
372 /// Chain through optional `Arc<parking_lot::RwLock<T>>` with optional readable inner keypath.
373 OptionalArcParkingRwLockOptionalKeyPathChain(RustPartialOptionalKeyPath<Root>),
374}