rust_key_paths/
lib.rs

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