1use std::sync::{Arc, Mutex, RwLock};
2use std::rc::Rc;
3use std::cell::RefCell;
4use std::any::Any;
5
6#[cfg(feature = "tagged_core")]
7use tagged_core::Tagged;
8
9pub trait WithContainer<Root, Value> {
13 fn with_arc<F, R>(self, arc: &Arc<Root>, f: F) -> R
16 where
17 F: FnOnce(&Value) -> R;
18
19 fn with_box<F, R>(self, boxed: &Box<Root>, f: F) -> R
22 where
23 F: FnOnce(&Value) -> R;
24
25 fn with_box_mut<F, R>(self, boxed: &mut Box<Root>, f: F) -> R
28 where
29 F: FnOnce(&mut Value) -> R;
30
31 fn with_rc<F, R>(self, rc: &Rc<Root>, f: F) -> R
34 where
35 F: FnOnce(&Value) -> R;
36
37 fn with_result<F, R, E>(self, result: &Result<Root, E>, f: F) -> Option<R>
40 where
41 F: FnOnce(&Value) -> R;
42
43 fn with_result_mut<F, R, E>(self, result: &mut Result<Root, E>, f: F) -> Option<R>
46 where
47 F: FnOnce(&mut Value) -> R;
48
49 fn with_option<F, R>(self, option: &Option<Root>, f: F) -> Option<R>
52 where
53 F: FnOnce(&Value) -> R;
54
55 fn with_option_mut<F, R>(self, option: &mut Option<Root>, f: F) -> Option<R>
58 where
59 F: FnOnce(&mut Value) -> R;
60
61 fn with_refcell<F, R>(self, refcell: &RefCell<Root>, f: F) -> Option<R>
64 where
65 F: FnOnce(&Value) -> R;
66
67 fn with_refcell_mut<F, R>(self, refcell: &RefCell<Root>, f: F) -> Option<R>
70 where
71 F: FnOnce(&mut Value) -> R;
72
73 #[cfg(feature = "tagged_core")]
76 fn with_tagged<F, R, Tag>(self, tagged: &Tagged<Root, Tag>, f: F) -> R
77 where
78 F: FnOnce(&Value) -> R;
79
80 fn with_mutex<F, R>(self, mutex: &Mutex<Root>, f: F) -> Option<R>
83 where
84 F: FnOnce(&Value) -> R;
85
86 fn with_mutex_mut<F, R>(self, mutex: &mut Mutex<Root>, f: F) -> Option<R>
89 where
90 F: FnOnce(&mut Value) -> R;
91
92 fn with_rwlock<F, R>(self, rwlock: &RwLock<Root>, f: F) -> Option<R>
95 where
96 F: FnOnce(&Value) -> R;
97
98 fn with_rwlock_mut<F, R>(self, rwlock: &mut RwLock<Root>, f: F) -> Option<R>
101 where
102 F: FnOnce(&mut Value) -> R;
103
104 fn with_arc_rwlock<F, R>(self, arc_rwlock: &Arc<RwLock<Root>>, f: F) -> Option<R>
107 where
108 F: FnOnce(&Value) -> R;
109
110 fn with_arc_rwlock_mut<F, R>(self, arc_rwlock: &Arc<RwLock<Root>>, f: F) -> Option<R>
113 where
114 F: FnOnce(&mut Value) -> R;
115}
116
117pub enum KeyPaths<Root, Value> {
120 Readable(Arc<dyn for<'a> Fn(&'a Root) -> &'a Value + Send + Sync>),
121 ReadableEnum {
122 extract: Arc<dyn for<'a> Fn(&'a Root) -> Option<&'a Value> + Send + Sync>,
123 embed: Arc<dyn Fn(Value) -> Root + Send + Sync>,
124 },
125 FailableReadable(Arc<dyn for<'a> Fn(&'a Root) -> Option<&'a Value> + Send + Sync>),
126
127 Writable(Arc<dyn for<'a> Fn(&'a mut Root) -> &'a mut Value + Send + Sync>),
128 FailableWritable(Arc<dyn for<'a> Fn(&'a mut Root) -> Option<&'a mut Value> + Send + Sync>),
129 WritableEnum {
130 extract: Arc<dyn for<'a> Fn(&'a Root) -> Option<&'a Value> + Send + Sync>,
131 extract_mut: Arc<dyn for<'a> Fn(&'a mut Root) -> Option<&'a mut Value> + Send + Sync>,
132 embed: Arc<dyn Fn(Value) -> Root + Send + Sync>,
133 },
134
135 ReferenceWritable(Arc<dyn for<'a> Fn(&'a mut Root) -> &'a mut Value + Send + Sync>),
137
138 Owned(Arc<dyn Fn(Root) -> Value + Send + Sync>),
140 FailableOwned(Arc<dyn Fn(Root) -> Option<Value> + Send + Sync>),
141
142 FailableCombined {
144 readable: Arc<dyn for<'a> Fn(&'a Root) -> Option<&'a Value> + Send + Sync>,
145 writable: Arc<dyn for<'a> Fn(&'a mut Root) -> Option<&'a mut Value> + Send + Sync>,
146 owned: Arc<dyn Fn(Root) -> Option<Value> + Send + Sync>, },
148}
149
150#[derive(Clone)]
154pub enum PartialKeyPath<Root> {
155 Readable(Arc<dyn for<'a> Fn(&'a Root) -> &'a (dyn Any + Send + Sync) + Send + Sync>),
156 ReadableEnum {
157 extract: Arc<dyn for<'a> Fn(&'a Root) -> Option<&'a (dyn Any + Send + Sync)> + Send + Sync>,
158 embed: Arc<dyn Fn(Box<dyn Any>) -> Root + Send + Sync>,
159 },
160 FailableReadable(Arc<dyn for<'a> Fn(&'a Root) -> Option<&'a (dyn Any + Send + Sync)> + Send + Sync>),
161
162 Writable(Arc<dyn for<'a> Fn(&'a mut Root) -> &'a mut (dyn Any + Send + Sync) + Send + Sync>),
163 FailableWritable(Arc<dyn for<'a> Fn(&'a mut Root) -> Option<&'a mut (dyn Any + Send + Sync)> + Send + Sync>),
164 WritableEnum {
165 extract: Arc<dyn for<'a> Fn(&'a Root) -> Option<&'a (dyn Any + Send + Sync)> + Send + Sync>,
166 extract_mut: Arc<dyn for<'a> Fn(&'a mut Root) -> Option<&'a mut (dyn Any + Send + Sync)> + Send + Sync>,
167 embed: Arc<dyn Fn(Box<dyn Any>) -> Root + Send + Sync>,
168 },
169
170 ReferenceWritable(Arc<dyn for<'a> Fn(&'a mut Root) -> &'a mut (dyn Any + Send + Sync) + Send + Sync>),
171
172 Owned(Arc<dyn Fn(Root) -> Box<dyn Any> + Send + Sync>),
173 FailableOwned(Arc<dyn Fn(Root) -> Option<Box<dyn Any>> + Send + Sync>),
174
175 FailableCombined {
177 readable: Arc<dyn for<'a> Fn(&'a Root) -> Option<&'a (dyn Any + Send + Sync)> + Send + Sync>,
178 writable: Arc<dyn for<'a> Fn(&'a mut Root) -> Option<&'a mut (dyn Any + Send + Sync)> + Send + Sync>,
179 owned: Arc<dyn Fn(Root) -> Option<Box<dyn Any>> + Send + Sync>, },
181}
182
183#[derive(Clone)]
187pub enum AnyKeyPath {
188 Readable(Arc<dyn for<'a> Fn(&'a (dyn Any + Send + Sync)) -> &'a (dyn Any + Send + Sync) + Send + Sync>),
189 ReadableEnum {
190 extract: Arc<dyn for<'a> Fn(&'a (dyn Any + Send + Sync)) -> Option<&'a (dyn Any + Send + Sync)> + Send + Sync>,
191 embed: Arc<dyn Fn(Box<dyn Any>) -> Box<dyn Any> + Send + Sync>,
192 },
193 FailableReadable(Arc<dyn for<'a> Fn(&'a (dyn Any + Send + Sync)) -> Option<&'a (dyn Any + Send + Sync)> + Send + Sync>),
194
195 Writable(Arc<dyn for<'a> Fn(&'a mut (dyn Any + Send + Sync)) -> &'a mut (dyn Any + Send + Sync) + Send + Sync>),
196 FailableWritable(Arc<dyn for<'a> Fn(&'a mut (dyn Any + Send + Sync)) -> Option<&'a mut (dyn Any + Send + Sync)> + Send + Sync>),
197 WritableEnum {
198 extract: Arc<dyn for<'a> Fn(&'a (dyn Any + Send + Sync)) -> Option<&'a (dyn Any + Send + Sync)> + Send + Sync>,
199 extract_mut: Arc<dyn for<'a> Fn(&'a mut (dyn Any + Send + Sync)) -> Option<&'a mut (dyn Any + Send + Sync)> + Send + Sync>,
200 embed: Arc<dyn Fn(Box<dyn Any>) -> Box<dyn Any> + Send + Sync>,
201 },
202
203 ReferenceWritable(Arc<dyn for<'a> Fn(&'a mut (dyn Any + Send + Sync)) -> &'a mut (dyn Any + Send + Sync) + Send + Sync>),
204
205 Owned(Arc<dyn Fn(Box<dyn Any>) -> Box<dyn Any> + Send + Sync>),
206 FailableOwned(Arc<dyn Fn(Box<dyn Any>) -> Option<Box<dyn Any>> + Send + Sync>),
207
208 FailableCombined {
210 readable: Arc<dyn for<'a> Fn(&'a (dyn Any + Send + Sync)) -> Option<&'a (dyn Any + Send + Sync)> + Send + Sync>,
211 writable: Arc<dyn for<'a> Fn(&'a mut (dyn Any + Send + Sync)) -> Option<&'a mut (dyn Any + Send + Sync)> + Send + Sync>,
212 owned: Arc<dyn Fn(Box<dyn Any>) -> Option<Box<dyn Any>> + Send + Sync>, },
214}
215
216impl<Root, Value> Clone for KeyPaths<Root, Value> {
217 fn clone(&self) -> Self {
218 match self {
219 KeyPaths::Readable(f) => KeyPaths::Readable(f.clone()),
220 KeyPaths::Writable(f) => KeyPaths::Writable(f.clone()),
221 KeyPaths::FailableReadable(f) => KeyPaths::FailableReadable(f.clone()),
222 KeyPaths::FailableWritable(f) => KeyPaths::FailableWritable(f.clone()),
223 KeyPaths::ReadableEnum { extract, embed } => KeyPaths::ReadableEnum {
224 extract: extract.clone(),
225 embed: embed.clone(),
226 },
227 KeyPaths::WritableEnum { extract, embed, extract_mut } => KeyPaths::WritableEnum {
228 extract: extract.clone(),
229 embed: embed.clone(),
230 extract_mut: extract_mut.clone(),
231 },
232 KeyPaths::ReferenceWritable(f) => KeyPaths::ReferenceWritable(f.clone()),
233 KeyPaths::Owned(f) => KeyPaths::Owned(f.clone()),
234 KeyPaths::FailableOwned(f) => KeyPaths::FailableOwned(f.clone()),
235 KeyPaths::FailableCombined { readable, writable, owned } => KeyPaths::FailableCombined {
236 readable: readable.clone(),
237 writable: writable.clone(),
238 owned: owned.clone(),
239 },
240 }
241 }
242}
243
244impl<Root, Value> KeyPaths<Root, Value> {
245 #[inline]
246 pub fn readable(get: impl for<'a> Fn(&'a Root) -> &'a Value + Send + Sync + 'static) -> Self {
247 Self::Readable(Arc::new(get))
248 }
249
250 #[inline]
251 pub fn writable(get_mut: impl for<'a> Fn(&'a mut Root) -> &'a mut Value + Send + Sync + 'static) -> Self {
252 Self::Writable(Arc::new(get_mut))
253 }
254
255 #[inline]
256 pub fn failable_readable(
257 get: impl for<'a> Fn(&'a Root) -> Option<&'a Value> + Send + Sync + 'static,
258 ) -> Self {
259 Self::FailableReadable(Arc::new(get))
260 }
261
262 #[inline]
263 pub fn failable_writable(
264 get_mut: impl for<'a> Fn(&'a mut Root) -> Option<&'a mut Value> + Send + Sync + 'static,
265 ) -> Self {
266 Self::FailableWritable(Arc::new(get_mut))
267 }
268
269 #[inline]
270 pub fn readable_enum(
271 embed: impl Fn(Value) -> Root + Send + Sync + 'static,
272 extract: impl for<'a> Fn(&'a Root) -> Option<&'a Value> + Send + Sync + 'static,
273 ) -> Self {
274 Self::ReadableEnum {
275 extract: Arc::new(extract),
276 embed: Arc::new(embed),
277 }
278 }
279
280 #[inline]
281 pub fn writable_enum(
282 embed: impl Fn(Value) -> Root + Send + Sync + 'static,
283 extract: impl for<'a> Fn(&'a Root) -> Option<&'a Value> + Send + Sync + 'static,
284 extract_mut: impl for<'a> Fn(&'a mut Root) -> Option<&'a mut Value> + Send + Sync + 'static,
285 ) -> Self {
286 Self::WritableEnum {
287 extract: Arc::new(extract),
288 embed: Arc::new(embed),
289 extract_mut: Arc::new(extract_mut),
290 }
291 }
292
293
294 #[inline]
296 pub fn owned(get: impl Fn(Root) -> Value + Send + Sync + 'static) -> Self {
297 Self::Owned(Arc::new(get))
298 }
299
300 #[inline]
301 pub fn failable_owned(get: impl Fn(Root) -> Option<Value> + Send + Sync + 'static) -> Self {
302 Self::FailableOwned(Arc::new(get))
303 }
304
305 #[inline]
306 pub fn failable_combined(
307 readable: impl for<'a> Fn(&'a Root) -> Option<&'a Value> + Send + Sync + 'static,
308 writable: impl for<'a> Fn(&'a mut Root) -> Option<&'a mut Value> + Send + Sync + 'static,
309 owned: impl Fn(Root) -> Option<Value> + Send + Sync + 'static, ) -> Self {
311 Self::FailableCombined {
312 readable: Arc::new(readable),
313 writable: Arc::new(writable),
314 owned: Arc::new(owned),
315 }
316 }
317
318 #[inline]
319 pub fn owned_writable(get: impl Fn(Root) -> Value + Send + Sync + 'static) -> Self {
320 Self::Owned(Arc::new(get))
321 }
322
323 #[inline]
324 pub fn failable_owned_writable(get: impl Fn(Root) -> Option<Value> + Send + Sync + 'static) -> Self {
325 Self::FailableOwned(Arc::new(get))
326 }
327
328 #[inline]
329 pub fn reference_writable(get_mut: impl for<'a> Fn(&'a mut Root) -> &'a mut Value + Send + Sync + 'static) -> Self {
330 Self::ReferenceWritable(Arc::new(get_mut))
331 }
332
333 pub fn to_partial(self) -> PartialKeyPath<Root>
336 where
337 Root: 'static,
338 Value: 'static + Send + Sync,
339 {
340 match self {
341 KeyPaths::Readable(f) => PartialKeyPath::Readable(Arc::new(move |root| f(root) as &(dyn Any + Send + Sync))),
342 KeyPaths::Writable(f) => PartialKeyPath::Writable(Arc::new(move |root| f(root) as &mut (dyn Any + Send + Sync))),
343 KeyPaths::FailableReadable(f) => PartialKeyPath::FailableReadable(Arc::new(move |root| f(root).map(|v| v as &(dyn Any + Send + Sync)))),
344 KeyPaths::FailableWritable(f) => PartialKeyPath::FailableWritable(Arc::new(move |root| f(root).map(|v| v as &mut (dyn Any + Send + Sync)))),
345 KeyPaths::ReadableEnum { extract, embed } => PartialKeyPath::ReadableEnum {
346 extract: Arc::new(move |root| extract(root).map(|v| v as &(dyn Any + Send + Sync))),
347 embed: Arc::new(move |value| embed(*value.downcast::<Value>().unwrap())),
348 },
349 KeyPaths::WritableEnum { extract, extract_mut, embed } => PartialKeyPath::WritableEnum {
350 extract: Arc::new(move |root| extract(root).map(|v| v as &(dyn Any + Send + Sync))),
351 extract_mut: Arc::new(move |root| extract_mut(root).map(|v| v as &mut (dyn Any + Send + Sync))),
352 embed: Arc::new(move |value| embed(*value.downcast::<Value>().unwrap())),
353 },
354 KeyPaths::ReferenceWritable(f) => PartialKeyPath::ReferenceWritable(Arc::new(move |root| f(root) as &mut (dyn Any + Send + Sync))),
355 KeyPaths::Owned(f) => PartialKeyPath::Owned(Arc::new(move |root| Box::new(f(root)) as Box<dyn Any>)),
356 KeyPaths::FailableOwned(f) => PartialKeyPath::FailableOwned(Arc::new(move |root| f(root).map(|v| Box::new(v) as Box<dyn Any>))),
357 KeyPaths::FailableCombined { readable, writable, owned } => PartialKeyPath::FailableCombined {
358 readable: Arc::new(move |root| readable(root).map(|v| v as &(dyn Any + Send + Sync))),
359 writable: Arc::new(move |root| writable(root).map(|v| v as &mut (dyn Any + Send + Sync))),
360 owned: Arc::new(move |root| owned(root).map(|v| Box::new(v) as Box<dyn Any>)),
361 },
362 }
363 }
364
365 pub fn to_any(self) -> AnyKeyPath
368 where
369 Root: 'static + Send + Sync,
370 Value: 'static + Send + Sync,
371 {
372 match self {
373 KeyPaths::Readable(f) => AnyKeyPath::Readable(Arc::new(move |root| {
374 let typed_root = root.downcast_ref::<Root>().unwrap();
375 f(typed_root) as &(dyn Any + Send + Sync)
376 })),
377 KeyPaths::Writable(f) => AnyKeyPath::Writable(Arc::new(move |root| {
378 let typed_root = root.downcast_mut::<Root>().unwrap();
379 f(typed_root) as &mut (dyn Any + Send + Sync)
380 })),
381 KeyPaths::FailableReadable(f) => AnyKeyPath::FailableReadable(Arc::new(move |root| {
382 let typed_root = root.downcast_ref::<Root>().unwrap();
383 f(typed_root).map(|v| v as &(dyn Any + Send + Sync))
384 })),
385 KeyPaths::FailableWritable(f) => AnyKeyPath::FailableWritable(Arc::new(move |root| {
386 let typed_root = root.downcast_mut::<Root>().unwrap();
387 f(typed_root).map(|v| v as &mut (dyn Any + Send + Sync))
388 })),
389 KeyPaths::ReadableEnum { extract, embed } => AnyKeyPath::ReadableEnum {
390 extract: Arc::new(move |root| {
391 let typed_root = root.downcast_ref::<Root>().unwrap();
392 extract(typed_root).map(|v| v as &(dyn Any + Send + Sync))
393 }),
394 embed: Arc::new(move |value| {
395 let typed_value = *value.downcast::<Value>().unwrap();
396 Box::new(embed(typed_value)) as Box<dyn Any>
397 }),
398 },
399 KeyPaths::WritableEnum { extract, extract_mut, embed } => AnyKeyPath::WritableEnum {
400 extract: Arc::new(move |root| {
401 let typed_root = root.downcast_ref::<Root>().unwrap();
402 extract(typed_root).map(|v| v as &(dyn Any + Send + Sync))
403 }),
404 extract_mut: Arc::new(move |root| {
405 let typed_root = root.downcast_mut::<Root>().unwrap();
406 extract_mut(typed_root).map(|v| v as &mut (dyn Any + Send + Sync))
407 }),
408 embed: Arc::new(move |value| {
409 let typed_value = *value.downcast::<Value>().unwrap();
410 Box::new(embed(typed_value)) as Box<dyn Any>
411 }),
412 },
413 KeyPaths::ReferenceWritable(f) => AnyKeyPath::ReferenceWritable(Arc::new(move |root| {
414 let typed_root = root.downcast_mut::<Root>().unwrap();
415 f(typed_root) as &mut (dyn Any + Send + Sync)
416 })),
417 KeyPaths::Owned(f) => AnyKeyPath::Owned(Arc::new(move |root| {
418 let typed_root = *root.downcast::<Root>().unwrap();
419 Box::new(f(typed_root)) as Box<dyn Any>
420 })),
421 KeyPaths::FailableOwned(f) => AnyKeyPath::FailableOwned(Arc::new(move |root| {
422 let typed_root = *root.downcast::<Root>().unwrap();
423 f(typed_root).map(|v| Box::new(v) as Box<dyn Any>)
424 })),
425 KeyPaths::FailableCombined { readable, writable, owned } => AnyKeyPath::FailableCombined {
426 readable: Arc::new(move |root| {
427 let typed_root = root.downcast_ref::<Root>().unwrap();
428 readable(typed_root).map(|v| v as &(dyn Any + Send + Sync))
429 }),
430 writable: Arc::new(move |root| {
431 let typed_root = root.downcast_mut::<Root>().unwrap();
432 writable(typed_root).map(|v| v as &mut (dyn Any + Send + Sync))
433 }),
434 owned: Arc::new(move |root| {
435 let typed_root = root.downcast_ref::<Root>().unwrap();
436 panic!("Owned access not supported for type-erased keypaths")
438 }),
439 },
440 }
441 }
442
443 #[inline]
453 pub fn extract_from_ref_slice<'a>(&self, slice: &'a [&Root]) -> Vec<&'a Value>
454 where
455 Root: 'static,
456 Value: 'static,
457 {
458 match self {
459 KeyPaths::Readable(f) => {
460 slice.iter().map(|item| f(item)).collect()
461 }
462 KeyPaths::FailableReadable(f) => {
463 slice.iter().filter_map(|item| f(item)).collect()
464 }
465 KeyPaths::ReadableEnum { extract, .. } => {
466 slice.iter().filter_map(|item| extract(item)).collect()
467 }
468 _ => panic!("extract_from_ref_slice only works with readable keypaths"),
469 }
470 }
471
472 #[inline]
482 pub fn extract_mut_from_ref_slice<'a>(&self, slice: &'a mut [&mut Root]) -> Vec<&'a mut Value>
483 where
484 Root: 'static,
485 Value: 'static,
486 {
487 match self {
488 KeyPaths::Writable(f) => {
489 slice.iter_mut().map(|item| f(item)).collect()
490 }
491 KeyPaths::FailableWritable(f) => {
492 slice.iter_mut().filter_map(|item| f(item)).collect()
493 }
494 KeyPaths::WritableEnum { extract_mut, .. } => {
495 slice.iter_mut().filter_map(|item| extract_mut(item)).collect()
496 }
497 _ => panic!("extract_mut_from_ref_slice only works with writable keypaths"),
498 }
499 }
500}
501
502impl<Root, Value> KeyPaths<Root, Value> {
503 #[inline]
505 pub fn get<'a>(&'a self, root: &'a Root) -> Option<&'a Value> {
506 match self {
507 KeyPaths::Readable(f) => Some(f(root)),
508 KeyPaths::Writable(_) => None, KeyPaths::FailableReadable(f) => f(root),
510 KeyPaths::FailableWritable(_) => None, KeyPaths::ReadableEnum { extract, .. } => extract(root),
512 KeyPaths::WritableEnum { extract, .. } => extract(root),
513 KeyPaths::ReferenceWritable(_) => None, KeyPaths::Owned(_) => None, KeyPaths::FailableOwned(_) => None, KeyPaths::FailableCombined { readable, .. } => readable(root),
518 }
519 }
520
521 #[inline]
524 pub fn get_ref<'a, 'b>(&'a self, root: &'b &Root) -> Option<&'b Value>
525 where
526 'a: 'b,
527 {
528 match self {
529 KeyPaths::Readable(f) => Some(f(*root)),
530 KeyPaths::Writable(_) => None, KeyPaths::FailableReadable(f) => f(*root),
532 KeyPaths::FailableWritable(_) => None, KeyPaths::ReadableEnum { extract, .. } => extract(*root),
534 KeyPaths::WritableEnum { extract, .. } => extract(*root),
535 KeyPaths::ReferenceWritable(_) => None, KeyPaths::Owned(_) => None, KeyPaths::FailableOwned(_) => None, KeyPaths::FailableCombined { readable, .. } => readable(*root),
540 }
541 }
542
543 #[inline]
545 pub fn get_mut<'a>(&'a self, root: &'a mut Root) -> Option<&'a mut Value> {
546 match self {
547 KeyPaths::Readable(_) => None, KeyPaths::Writable(f) => Some(f(root)),
549 KeyPaths::FailableReadable(_) => None, KeyPaths::FailableWritable(f) => f(root),
551 KeyPaths::ReadableEnum { .. } => None, KeyPaths::WritableEnum { extract_mut, .. } => extract_mut(root),
553 KeyPaths::ReferenceWritable(f) => Some(f(root)),
554 KeyPaths::Owned(_) => None, KeyPaths::FailableOwned(_) => None, KeyPaths::FailableCombined { writable, .. } => writable(root),
558 }
559 }
560
561 #[inline]
564 pub fn get_mut_ref<'a, 'b>(&'a self, root: &'b mut &mut Root) -> Option<&'b mut Value>
565 where
566 'a: 'b,
567 {
568 match self {
569 KeyPaths::Readable(_) => None, KeyPaths::Writable(f) => Some(f(*root)),
571 KeyPaths::FailableReadable(_) => None, KeyPaths::FailableWritable(f) => f(*root),
573 KeyPaths::ReadableEnum { .. } => None, KeyPaths::WritableEnum { extract_mut, .. } => extract_mut(*root),
575 KeyPaths::ReferenceWritable(f) => Some(f(*root)),
576 KeyPaths::Owned(_) => None, KeyPaths::FailableOwned(_) => None, KeyPaths::FailableCombined { writable, .. } => writable(*root),
580 }
581 }
582
583 #[inline]
590 pub fn for_arc(self) -> KeyPaths<Arc<Root>, Value>
591 where
592 Root: 'static,
593 Value: 'static,
594 {
595 match self {
596 KeyPaths::Readable(f) => KeyPaths::Readable(Arc::new(move |root: &Arc<Root>| {
597 f(&**root)
598 })),
599 KeyPaths::Writable(_) => {
600 panic!("Cannot create writable keypath for Arc (Arc is immutable)")
602 }
603 KeyPaths::FailableReadable(f) => {
604 KeyPaths::FailableReadable(Arc::new(move |root: &Arc<Root>| f(&**root)))
605 }
606 KeyPaths::ReadableEnum { extract, embed } => KeyPaths::ReadableEnum {
607 extract: Arc::new(move |root: &Arc<Root>| extract(&**root)),
608 embed: Arc::new(move |value| Arc::new(embed(value))),
609 },
610 other => panic!("Unsupported keypath variant for Arc adapter: {:?}", kind_name(&other)),
611 }
612 }
613
614 #[inline]
623 pub fn extract_from_slice<'a>(&self, slice: &'a [Root]) -> Vec<&'a Value>
624 where
625 Root: 'static,
626 Value: 'static,
627 {
628 match self {
629 KeyPaths::Readable(f) => {
630 slice.iter().map(|item| f(item)).collect()
631 }
632 KeyPaths::FailableReadable(f) => {
633 slice.iter().filter_map(|item| f(item)).collect()
634 }
635 KeyPaths::ReadableEnum { extract, .. } => {
636 slice.iter().filter_map(|item| extract(item)).collect()
637 }
638 _ => panic!("extract_from_slice only works with readable keypaths"),
639 }
640 }
641
642 #[inline]
651 pub fn extract_from_iter<'a, I>(&self, iter: I) -> Vec<&'a Value>
652 where
653 I: Iterator<Item = &'a Root>,
654 Root: 'static,
655 Value: 'static,
656 {
657 match self {
658 KeyPaths::Readable(f) => {
659 iter.map(|item| f(item)).collect()
660 }
661 KeyPaths::FailableReadable(f) => {
662 iter.filter_map(|item| f(item)).collect()
663 }
664 KeyPaths::ReadableEnum { extract, .. } => {
665 iter.filter_map(|item| extract(item)).collect()
666 }
667 _ => panic!("extract_from_iter only works with readable keypaths"),
668 }
669 }
670
671 #[inline]
680 pub fn extract_mut_from_slice<'a>(&self, slice: &'a mut [Root]) -> Vec<&'a mut Value>
681 where
682 Root: 'static,
683 Value: 'static,
684 {
685 match self {
686 KeyPaths::Writable(f) => {
687 slice.iter_mut().map(|item| f(item)).collect()
688 }
689 KeyPaths::FailableWritable(f) => {
690 slice.iter_mut().filter_map(|item| f(item)).collect()
691 }
692 KeyPaths::WritableEnum { extract_mut, .. } => {
693 slice.iter_mut().filter_map(|item| extract_mut(item)).collect()
694 }
695 _ => panic!("extract_mut_from_slice only works with writable keypaths"),
696 }
697 }
698
699 #[inline]
702 pub fn for_box(self) -> KeyPaths<Box<Root>, Value>
703 where
704 Root: 'static,
705 Value: 'static,
706 {
707 match self {
708 KeyPaths::Readable(f) => KeyPaths::Readable(Arc::new(move |root: &Box<Root>| {
709 f(&**root)
710 })),
711 KeyPaths::Writable(f) => KeyPaths::Writable(Arc::new(move |root: &mut Box<Root>| {
712 f(&mut **root)
713 })),
714 KeyPaths::FailableReadable(f) => {
715 KeyPaths::FailableReadable(Arc::new(move |root: &Box<Root>| f(&**root)))
716 }
717 KeyPaths::FailableWritable(f) => {
718 KeyPaths::FailableWritable(Arc::new(move |root: &mut Box<Root>| f(&mut **root)))
719 }
720 KeyPaths::ReadableEnum { extract, embed } => KeyPaths::ReadableEnum {
721 extract: Arc::new(move |root: &Box<Root>| extract(&**root)),
722 embed: Arc::new(move |value| Box::new(embed(value))),
723 },
724 KeyPaths::WritableEnum { extract, extract_mut, embed } => KeyPaths::WritableEnum {
725 extract: Arc::new(move |root: &Box<Root>| extract(&**root)),
726 extract_mut: Arc::new(move |root: &mut Box<Root>| extract_mut(&mut **root)),
727 embed: Arc::new(move |value| Box::new(embed(value))),
728 },
729 other => panic!("Unsupported keypath variant for Box adapter: {:?}", kind_name(&other)),
730 }
731 }
732
733 #[inline]
736 pub fn for_rc(self) -> KeyPaths<Rc<Root>, Value>
737 where
738 Root: 'static,
739 Value: 'static,
740 {
741 match self {
742 KeyPaths::Readable(f) => KeyPaths::Readable(Arc::new(move |root: &Rc<Root>| {
743 f(&**root)
744 })),
745 KeyPaths::Writable(_) => {
746 panic!("Cannot create writable keypath for Rc (Rc is immutable)")
748 }
749 KeyPaths::FailableReadable(f) => {
750 KeyPaths::FailableReadable(Arc::new(move |root: &Rc<Root>| f(&**root)))
751 }
752 KeyPaths::ReadableEnum { extract, embed } => KeyPaths::ReadableEnum {
753 extract: Arc::new(move |root: &Rc<Root>| extract(&**root)),
754 embed: Arc::new(move |value| Rc::new(embed(value))),
755 },
756 other => panic!("Unsupported keypath variant for Rc adapter: {:?}", kind_name(&other)),
757 }
758 }
759
760 #[inline]
764 pub fn for_result<E>(self) -> KeyPaths<Result<Root, E>, Value>
765 where
766 Root: 'static,
767 Value: 'static,
768 E: 'static,
769 {
770 match self {
771 KeyPaths::Readable(f) => KeyPaths::FailableReadable(Arc::new(move |root: &Result<Root, E>| {
772 root.as_ref().ok().map(|r| f(r))
773 })),
774 KeyPaths::Writable(f) => KeyPaths::FailableWritable(Arc::new(move |root: &mut Result<Root, E>| {
775 root.as_mut().ok().map(|r| f(r))
776 })),
777 KeyPaths::FailableReadable(f) => {
778 KeyPaths::FailableReadable(Arc::new(move |root: &Result<Root, E>| {
779 root.as_ref().ok().and_then(|r| f(r))
780 }))
781 }
782 KeyPaths::FailableWritable(f) => {
783 KeyPaths::FailableWritable(Arc::new(move |root: &mut Result<Root, E>| {
784 root.as_mut().ok().and_then(|r| f(r))
785 }))
786 }
787 KeyPaths::ReadableEnum { extract, embed } => KeyPaths::ReadableEnum {
788 extract: Arc::new(move |root: &Result<Root, E>| {
789 root.as_ref().ok().and_then(|r| extract(r))
790 }),
791 embed: Arc::new(move |value| Ok(embed(value))),
792 },
793 KeyPaths::WritableEnum { extract, extract_mut, embed } => KeyPaths::WritableEnum {
794 extract: Arc::new(move |root: &Result<Root, E>| {
795 root.as_ref().ok().and_then(|r| extract(r))
796 }),
797 extract_mut: Arc::new(move |root: &mut Result<Root, E>| {
798 root.as_mut().ok().and_then(|r| extract_mut(r))
799 }),
800 embed: Arc::new(move |value| Ok(embed(value))),
801 },
802 other => panic!("Unsupported keypath variant for Result adapter: {:?}", kind_name(&other)),
803 }
804 }
805
806 #[inline]
810 pub fn for_option(self) -> KeyPaths<Option<Root>, Value>
811 where
812 Root: 'static,
813 Value: 'static,
814 {
815 match self {
816 KeyPaths::Readable(f) => KeyPaths::FailableReadable(Arc::new(move |root: &Option<Root>| {
817 root.as_ref().map(|r| f(r))
818 })),
819 KeyPaths::Writable(f) => KeyPaths::FailableWritable(Arc::new(move |root: &mut Option<Root>| {
820 root.as_mut().map(|r| f(r))
821 })),
822 KeyPaths::FailableReadable(f) => {
823 KeyPaths::FailableReadable(Arc::new(move |root: &Option<Root>| {
824 root.as_ref().and_then(|r| f(r))
825 }))
826 }
827 KeyPaths::FailableWritable(f) => {
828 KeyPaths::FailableWritable(Arc::new(move |root: &mut Option<Root>| {
829 root.as_mut().and_then(|r| f(r))
830 }))
831 }
832 KeyPaths::ReadableEnum { extract, embed } => KeyPaths::ReadableEnum {
833 extract: Arc::new(move |root: &Option<Root>| {
834 root.as_ref().and_then(|r| extract(r))
835 }),
836 embed: Arc::new(move |value| Some(embed(value))),
837 },
838 KeyPaths::WritableEnum { extract, extract_mut, embed } => KeyPaths::WritableEnum {
839 extract: Arc::new(move |root: &Option<Root>| {
840 root.as_ref().and_then(|r| extract(r))
841 }),
842 extract_mut: Arc::new(move |root: &mut Option<Root>| {
843 root.as_mut().and_then(|r| extract_mut(r))
844 }),
845 embed: Arc::new(move |value| Some(embed(value))),
846 },
847 other => panic!("Unsupported keypath variant for Option adapter: {:?}", kind_name(&other)),
848 }
849 }
850
851 #[inline]
855 pub fn for_arc_rwlock(self) -> KeyPaths<Arc<RwLock<Root>>, Value>
856 where
857 Root: 'static,
858 Value: Clone + 'static,
859 {
860 match self {
861 KeyPaths::Readable(f) => KeyPaths::FailableOwned(Arc::new(move |root: Arc<RwLock<Root>>| {
862 let guard = root.read().ok()?;
863 Some(f(&*guard).clone())
864 })),
865 KeyPaths::Writable(_) => {
866 panic!("Cannot create writable keypath for Arc<RwLock> (use with_arc_rwlock_mut instead)")
868 }
869 KeyPaths::FailableReadable(f) => {
870 KeyPaths::FailableOwned(Arc::new(move |root: Arc<RwLock<Root>>| {
871 let guard = root.read().ok()?;
872 f(&*guard).map(|v| v.clone())
873 }))
874 }
875 KeyPaths::ReadableEnum { extract, embed: _ } => KeyPaths::FailableOwned(Arc::new(move |root: Arc<RwLock<Root>>| {
876 let guard = root.read().ok()?;
877 extract(&*guard).map(|v| v.clone())
878 })),
879 other => panic!("Unsupported keypath variant for Arc<RwLock> adapter: {:?}", kind_name(&other)),
880 }
881 }
882
883 #[inline]
887 pub fn for_arc_mutex(self) -> KeyPaths<Arc<Mutex<Root>>, Value>
888 where
889 Root: 'static,
890 Value: Clone + 'static,
891 {
892 match self {
893 KeyPaths::Readable(f) => KeyPaths::FailableOwned(Arc::new(move |root: Arc<Mutex<Root>>| {
894 let guard = root.lock().ok()?;
895 Some(f(&*guard).clone())
896 })),
897 KeyPaths::Writable(_) => {
898 panic!("Cannot create writable keypath for Arc<Mutex> (use with_arc_mutex_mut instead)")
900 }
901 KeyPaths::FailableReadable(f) => {
902 KeyPaths::FailableOwned(Arc::new(move |root: Arc<Mutex<Root>>| {
903 let guard = root.lock().ok()?;
904 f(&*guard).map(|v| v.clone())
905 }))
906 }
907 KeyPaths::ReadableEnum { extract, embed: _ } => KeyPaths::FailableOwned(Arc::new(move |root: Arc<Mutex<Root>>| {
908 let guard = root.lock().ok()?;
909 extract(&*guard).map(|v| v.clone())
910 })),
911 other => panic!("Unsupported keypath variant for Arc<Mutex> adapter: {:?}", kind_name(&other)),
912 }
913 }
914
915 #[cfg(feature = "parking_lot")]
920 #[inline]
921 pub fn for_arc_parking_mutex(self) -> KeyPaths<Arc<parking_lot::Mutex<Root>>, Value>
922 where
923 Root: 'static,
924 Value: Clone + 'static,
925 {
926 match self {
927 KeyPaths::Readable(f) => KeyPaths::FailableOwned(Arc::new(move |root: Arc<parking_lot::Mutex<Root>>| {
928 let guard = root.lock();
929 Some(f(&*guard).clone())
930 })),
931 KeyPaths::Writable(_) => {
932 panic!("Cannot create writable keypath for Arc<parking_lot::Mutex> (use with_arc_parking_mutex_mut instead)")
934 }
935 KeyPaths::FailableReadable(f) => {
936 KeyPaths::FailableOwned(Arc::new(move |root: Arc<parking_lot::Mutex<Root>>| {
937 let guard = root.lock();
938 f(&*guard).map(|v| v.clone())
939 }))
940 }
941 KeyPaths::ReadableEnum { extract, embed: _ } => KeyPaths::FailableOwned(Arc::new(move |root: Arc<parking_lot::Mutex<Root>>| {
942 let guard = root.lock();
943 extract(&*guard).map(|v| v.clone())
944 })),
945 other => panic!("Unsupported keypath variant for Arc<parking_lot::Mutex> adapter: {:?}", kind_name(&other)),
946 }
947 }
948
949 #[cfg(feature = "parking_lot")]
954 #[inline]
955 pub fn for_arc_parking_rwlock(self) -> KeyPaths<Arc<parking_lot::RwLock<Root>>, Value>
956 where
957 Root: 'static,
958 Value: Clone + 'static,
959 {
960 match self {
961 KeyPaths::Readable(f) => KeyPaths::FailableOwned(Arc::new(move |root: Arc<parking_lot::RwLock<Root>>| {
962 let guard = root.read();
963 Some(f(&*guard).clone())
964 })),
965 KeyPaths::Writable(_) => {
966 panic!("Cannot create writable keypath for Arc<parking_lot::RwLock> (use with_arc_parking_rwlock_mut instead)")
968 }
969 KeyPaths::FailableReadable(f) => {
970 KeyPaths::FailableOwned(Arc::new(move |root: Arc<parking_lot::RwLock<Root>>| {
971 let guard = root.read();
972 f(&*guard).map(|v| v.clone())
973 }))
974 }
975 KeyPaths::ReadableEnum { extract, embed: _ } => KeyPaths::FailableOwned(Arc::new(move |root: Arc<parking_lot::RwLock<Root>>| {
976 let guard = root.read();
977 extract(&*guard).map(|v| v.clone())
978 })),
979 other => panic!("Unsupported keypath variant for Arc<parking_lot::RwLock> adapter: {:?}", kind_name(&other)),
980 }
981 }
982
983 #[cfg(feature = "tagged_core")]
988 #[inline]
989 pub fn for_tagged<Tag>(self) -> KeyPaths<Tagged<Root, Tag>, Value>
990 where
991 Root: Clone + 'static,
992 Value: 'static,
993 Tag: 'static,
994 {
995 match self {
996 KeyPaths::Readable(f) => KeyPaths::Readable(Arc::new(move |root: &Tagged<Root, Tag>| {
997 f(&**root)
998 })),
999 KeyPaths::Writable(_) => {
1000 panic!("Tagged does not support writable keypaths (Tagged only implements Deref, not DerefMut)")
1001 }
1002 KeyPaths::FailableReadable(f) => KeyPaths::FailableReadable(Arc::new(move |root: &Tagged<Root, Tag>| {
1003 f(&**root)
1004 })),
1005 KeyPaths::FailableWritable(_) => {
1006 panic!("Tagged does not support writable keypaths (Tagged only implements Deref, not DerefMut)")
1007 }
1008 KeyPaths::ReadableEnum { extract, embed } => KeyPaths::ReadableEnum {
1009 extract: Arc::new(move |root: &Tagged<Root, Tag>| {
1010 extract(&**root)
1011 }),
1012 embed: Arc::new(move |value: Value| {
1013 Tagged::new(embed(value))
1014 }),
1015 },
1016 KeyPaths::WritableEnum { .. } => {
1017 panic!("Tagged does not support writable keypaths (Tagged only implements Deref, not DerefMut)")
1018 }
1019 KeyPaths::ReferenceWritable(_) => {
1020 panic!("Tagged does not support writable keypaths (Tagged only implements Deref, not DerefMut)")
1021 }
1022 KeyPaths::Owned(f) => KeyPaths::Owned(Arc::new(move |root: Tagged<Root, Tag>| {
1023 f((*root).clone())
1025 })),
1026 KeyPaths::FailableOwned(f) => KeyPaths::FailableOwned(Arc::new(move |root: Tagged<Root, Tag>| {
1027 f((*root).clone())
1028 })),
1029 KeyPaths::FailableCombined { readable, writable, owned } => KeyPaths::FailableCombined {
1030 readable: Arc::new(move |root: &Tagged<Root, Tag>| readable(&**root)),
1031 writable: Arc::new(move |root: &mut Tagged<Root, Tag>| {
1032 panic!("Tagged does not support writable keypaths (Tagged only implements Deref, not DerefMut)")
1033 }),
1034 owned: Arc::new(move |_root: Tagged<Root, Tag>| panic!("Tagged does not support owned keypaths")),
1035 },
1036 }
1037 }
1038
1039 pub fn embed(&self, value: Value) -> Option<Root>
1043 where
1044 Value: Clone,
1045 {
1046 match self {
1047 KeyPaths::ReadableEnum { embed, .. } => Some(embed(value)),
1048 _ => None,
1049 }
1050 }
1051
1052 pub fn embed_mut(&self, value: Value) -> Option<Root>
1053 where
1054 Value: Clone,
1055 {
1056 match self {
1057 KeyPaths::WritableEnum { embed, .. } => Some(embed(value)),
1058 _ => None,
1059 }
1060 }
1061
1062
1063 #[inline]
1067 pub fn get_owned(self, root: Root) -> Value {
1068 match self {
1069 KeyPaths::Owned(f) => f(root),
1070 _ => panic!("get_owned only works with owned keypaths"),
1071 }
1072 }
1073
1074 #[inline]
1076 pub fn get_failable_owned(self, root: Root) -> Option<Value> {
1077 match self {
1078 KeyPaths::FailableOwned(f) => f(root),
1079 KeyPaths::FailableCombined { owned, .. } => owned(root),
1080 _ => panic!("get_failable_owned only works with failable owned keypaths"),
1081 }
1082 }
1083
1084 pub fn iter<'a, T>(&'a self, root: &'a Root) -> Option<<&'a Value as IntoIterator>::IntoIter>
1086 where
1087 &'a Value: IntoIterator<Item = &'a T>,
1088 T: 'a,
1089 {
1090 self.get(root).map(|v| v.into_iter())
1091 }
1092
1093 pub fn iter_mut<'a, T>(
1095 &'a self,
1096 root: &'a mut Root,
1097 ) -> Option<<&'a mut Value as IntoIterator>::IntoIter>
1098 where
1099 &'a mut Value: IntoIterator<Item = &'a mut T>,
1100 T: 'a,
1101 {
1102 self.get_mut(root).map(|v| v.into_iter())
1103 }
1104
1105 #[inline]
1107 pub fn into_iter<T>(self, root: Root) -> Option<<Value as IntoIterator>::IntoIter>
1108 where
1109 Value: IntoIterator<Item = T> + Clone,
1110 {
1111 match self {
1112 KeyPaths::Readable(f) => Some(f(&root).clone().into_iter()), KeyPaths::Writable(_) => None,
1114 KeyPaths::FailableReadable(f) => f(&root).map(|v| v.clone().into_iter()),
1115 KeyPaths::FailableWritable(_) => None,
1116 KeyPaths::ReadableEnum { extract, .. } => extract(&root).map(|v| v.clone().into_iter()),
1117 KeyPaths::WritableEnum { extract, .. } => extract(&root).map(|v| v.clone().into_iter()),
1118 KeyPaths::ReferenceWritable(_) => None, KeyPaths::Owned(f) => Some(f(root).into_iter()),
1121 KeyPaths::FailableOwned(f) => f(root).map(|v| v.into_iter()),
1122 KeyPaths::FailableCombined { owned, .. } => owned(root).map(|v| v.into_iter()),
1123 }
1124 }
1125}
1126
1127impl<Root> PartialKeyPath<Root> {
1129 #[inline]
1131 pub fn get<'a>(&'a self, root: &'a Root) -> Option<&'a (dyn Any + Send + Sync)> {
1132 match self {
1133 PartialKeyPath::Readable(f) => Some(f(root)),
1134 PartialKeyPath::Writable(_) => None, PartialKeyPath::FailableReadable(f) => f(root),
1136 PartialKeyPath::FailableWritable(_) => None, PartialKeyPath::ReadableEnum { extract, .. } => extract(root),
1138 PartialKeyPath::WritableEnum { extract, .. } => extract(root),
1139 PartialKeyPath::ReferenceWritable(_) => None, PartialKeyPath::Owned(_) => None, PartialKeyPath::FailableOwned(_) => None, PartialKeyPath::FailableCombined { readable, .. } => readable(root),
1143 }
1144 }
1145
1146 #[inline]
1148 pub fn get_mut<'a>(&'a self, root: &'a mut Root) -> Option<&'a mut (dyn Any + Send + Sync)> {
1149 match self {
1150 PartialKeyPath::Readable(_) => None, PartialKeyPath::Writable(f) => Some(f(root)),
1152 PartialKeyPath::FailableReadable(_) => None, PartialKeyPath::FailableWritable(f) => f(root),
1154 PartialKeyPath::ReadableEnum { .. } => None, PartialKeyPath::WritableEnum { extract_mut, .. } => extract_mut(root),
1156 PartialKeyPath::ReferenceWritable(f) => Some(f(root)),
1157 PartialKeyPath::Owned(_) => None, PartialKeyPath::FailableOwned(_) => None, PartialKeyPath::FailableCombined { writable, .. } => writable(root),
1160 }
1161 }
1162
1163 #[inline]
1165 pub fn get_owned(self, root: Root) -> Box<dyn Any> {
1166 match self {
1167 PartialKeyPath::Owned(f) => f(root),
1168 _ => panic!("get_owned only works with owned keypaths"),
1169 }
1170 }
1171
1172 #[inline]
1174 pub fn get_failable_owned(self, root: Root) -> Option<Box<dyn Any>> {
1175 match self {
1176 PartialKeyPath::FailableOwned(f) => f(root),
1177 PartialKeyPath::FailableCombined { owned, .. } => owned(root),
1178 _ => panic!("get_failable_owned only works with failable owned keypaths"),
1179 }
1180 }
1181
1182 pub fn to_any(self) -> AnyKeyPath
1184 where
1185 Root: 'static,
1186 {
1187 match self {
1188 PartialKeyPath::Readable(f) => AnyKeyPath::Readable(Arc::new(move |root| {
1189 let typed_root = root.downcast_ref::<Root>().unwrap();
1190 f(typed_root)
1191 })),
1192 PartialKeyPath::Writable(f) => AnyKeyPath::Writable(Arc::new(move |root| {
1193 let typed_root = root.downcast_mut::<Root>().unwrap();
1194 f(typed_root)
1195 })),
1196 PartialKeyPath::FailableReadable(f) => AnyKeyPath::FailableReadable(Arc::new(move |root| {
1197 let typed_root = root.downcast_ref::<Root>().unwrap();
1198 f(typed_root)
1199 })),
1200 PartialKeyPath::FailableWritable(f) => AnyKeyPath::FailableWritable(Arc::new(move |root| {
1201 let typed_root = root.downcast_mut::<Root>().unwrap();
1202 f(typed_root)
1203 })),
1204 PartialKeyPath::ReadableEnum { extract, embed } => AnyKeyPath::ReadableEnum {
1205 extract: Arc::new(move |root| {
1206 let typed_root = root.downcast_ref::<Root>().unwrap();
1207 extract(typed_root)
1208 }),
1209 embed: Arc::new(move |value| {
1210 let typed_value = *value.downcast::<Root>().unwrap();
1211 Box::new(embed(Box::new(typed_value))) as Box<dyn Any>
1212 }),
1213 },
1214 PartialKeyPath::WritableEnum { extract, extract_mut, embed } => AnyKeyPath::WritableEnum {
1215 extract: Arc::new(move |root| {
1216 let typed_root = root.downcast_ref::<Root>().unwrap();
1217 extract(typed_root)
1218 }),
1219 extract_mut: Arc::new(move |root| {
1220 let typed_root = root.downcast_mut::<Root>().unwrap();
1221 extract_mut(typed_root)
1222 }),
1223 embed: Arc::new(move |value| {
1224 let typed_value = *value.downcast::<Root>().unwrap();
1225 Box::new(embed(Box::new(typed_value))) as Box<dyn Any>
1226 }),
1227 },
1228 PartialKeyPath::ReferenceWritable(f) => AnyKeyPath::ReferenceWritable(Arc::new(move |root| {
1229 let typed_root = root.downcast_mut::<Root>().unwrap();
1230 f(typed_root)
1231 })),
1232 PartialKeyPath::Owned(f) => AnyKeyPath::Owned(Arc::new(move |root| {
1233 let typed_root = *root.downcast::<Root>().unwrap();
1234 f(typed_root)
1235 })),
1236 PartialKeyPath::FailableOwned(f) => AnyKeyPath::FailableOwned(Arc::new(move |root| {
1237 let typed_root = *root.downcast::<Root>().unwrap();
1238 f(typed_root)
1239 })),
1240 PartialKeyPath::FailableCombined { readable, writable, owned } => AnyKeyPath::FailableCombined {
1241 readable: Arc::new(move |root| {
1242 let typed_root = root.downcast_ref::<Root>().unwrap();
1243 readable(typed_root)
1244 }),
1245 writable: Arc::new(move |root| {
1246 let typed_root = root.downcast_mut::<Root>().unwrap();
1247 writable(typed_root)
1248 }),
1249 owned: Arc::new(move |root| {
1250 let typed_root = root.downcast_ref::<Root>().unwrap();
1251 panic!("Owned access not supported for type-erased keypaths")
1253 }),
1254 },
1255 }
1256 }
1257
1258 #[inline]
1260 pub fn kind_name(&self) -> &'static str {
1261 match self {
1262 PartialKeyPath::Readable(_) => "PartialKeyPath::Readable",
1263 PartialKeyPath::Writable(_) => "PartialKeyPath::Writable",
1264 PartialKeyPath::FailableReadable(_) => "PartialKeyPath::FailableReadable",
1265 PartialKeyPath::FailableWritable(_) => "PartialKeyPath::FailableWritable",
1266 PartialKeyPath::ReadableEnum { .. } => "PartialKeyPath::ReadableEnum",
1267 PartialKeyPath::WritableEnum { .. } => "PartialKeyPath::WritableEnum",
1268 PartialKeyPath::ReferenceWritable(_) => "PartialKeyPath::ReferenceWritable",
1269 PartialKeyPath::Owned(_) => "PartialKeyPath::Owned",
1270 PartialKeyPath::FailableOwned(_) => "PartialKeyPath::FailableOwned",
1271 PartialKeyPath::FailableCombined { .. } => "PartialKeyPath::FailableCombined",
1272 }
1273 }
1274
1275 pub fn for_arc(self) -> PartialKeyPath<Arc<Root>>
1279 where
1280 Root: 'static + Clone,
1281 {
1282 match self {
1283 PartialKeyPath::Readable(f) => PartialKeyPath::Readable(Arc::new(move |arc: &Arc<Root>| f(&**arc))),
1284 PartialKeyPath::Writable(_) => {
1285 panic!("Arc does not support writable keypaths (Arc only implements Deref, not DerefMut)")
1286 }
1287 PartialKeyPath::FailableReadable(f) => PartialKeyPath::FailableReadable(Arc::new(move |arc: &Arc<Root>| f(&**arc))),
1288 PartialKeyPath::FailableWritable(_) => {
1289 panic!("Arc does not support writable keypaths (Arc only implements Deref, not DerefMut)")
1290 }
1291 PartialKeyPath::ReadableEnum { extract, embed } => PartialKeyPath::ReadableEnum {
1292 extract: Arc::new(move |arc: &Arc<Root>| extract(&**arc)),
1293 embed: Arc::new(move |value| Arc::new(embed(value))),
1294 },
1295 PartialKeyPath::WritableEnum { .. } => {
1296 panic!("Arc does not support writable keypaths (Arc only implements Deref, not DerefMut)")
1297 }
1298 PartialKeyPath::ReferenceWritable(_) => {
1299 panic!("Arc does not support writable keypaths (Arc only implements Deref, not DerefMut)")
1300 }
1301 PartialKeyPath::Owned(f) => PartialKeyPath::Owned(Arc::new(move |arc: Arc<Root>| f((*arc).clone()))),
1302 PartialKeyPath::FailableOwned(f) => PartialKeyPath::FailableOwned(Arc::new(move |arc: Arc<Root>| f((*arc).clone()))),
1303 PartialKeyPath::FailableCombined { readable, writable, owned } => PartialKeyPath::FailableCombined {
1304 readable: Arc::new(move |root| readable(&**root)),
1305 writable: Arc::new(move |_root| panic!("Arc does not support mutable access")),
1306 owned: Arc::new(move |root| panic!("Arc does not support owned keypaths")),
1307 },
1308 }
1309 }
1310
1311 pub fn for_box(self) -> PartialKeyPath<Box<Root>>
1313 where
1314 Root: 'static,
1315 {
1316 match self {
1317 PartialKeyPath::Readable(f) => PartialKeyPath::Readable(Arc::new(move |boxed: &Box<Root>| f(&**boxed))),
1318 PartialKeyPath::Writable(f) => PartialKeyPath::Writable(Arc::new(move |boxed: &mut Box<Root>| f(&mut **boxed))),
1319 PartialKeyPath::FailableReadable(f) => PartialKeyPath::FailableReadable(Arc::new(move |boxed: &Box<Root>| f(&**boxed))),
1320 PartialKeyPath::FailableWritable(f) => PartialKeyPath::FailableWritable(Arc::new(move |boxed: &mut Box<Root>| f(&mut **boxed))),
1321 PartialKeyPath::ReadableEnum { extract, embed } => PartialKeyPath::ReadableEnum {
1322 extract: Arc::new(move |boxed: &Box<Root>| extract(&**boxed)),
1323 embed: Arc::new(move |value| Box::new(embed(value))),
1324 },
1325 PartialKeyPath::WritableEnum { extract, extract_mut, embed } => PartialKeyPath::WritableEnum {
1326 extract: Arc::new(move |boxed: &Box<Root>| extract(&**boxed)),
1327 extract_mut: Arc::new(move |boxed: &mut Box<Root>| extract_mut(&mut **boxed)),
1328 embed: Arc::new(move |value| Box::new(embed(value))),
1329 },
1330 PartialKeyPath::ReferenceWritable(f) => PartialKeyPath::ReferenceWritable(Arc::new(move |boxed: &mut Box<Root>| f(&mut **boxed))),
1331 PartialKeyPath::Owned(f) => PartialKeyPath::Owned(Arc::new(move |boxed: Box<Root>| f(*boxed))),
1332 PartialKeyPath::FailableOwned(f) => PartialKeyPath::FailableOwned(Arc::new(move |boxed: Box<Root>| f(*boxed))),
1333 PartialKeyPath::FailableCombined { readable, writable, owned } => PartialKeyPath::FailableCombined {
1334 readable: Arc::new(move |root| readable(&**root)),
1335 writable: Arc::new(move |_root| panic!("Arc does not support mutable access")),
1336 owned: Arc::new(move |root| panic!("Arc does not support owned keypaths")),
1337 },
1338 }
1339 }
1340
1341 pub fn for_rc(self) -> PartialKeyPath<Rc<Root>>
1343 where
1344 Root: 'static + Clone,
1345 {
1346 match self {
1347 PartialKeyPath::Readable(f) => PartialKeyPath::Readable(Arc::new(move |rc: &Rc<Root>| f(&**rc))),
1348 PartialKeyPath::Writable(_) => {
1349 panic!("Rc does not support writable keypaths (Rc only implements Deref, not DerefMut)")
1350 }
1351 PartialKeyPath::FailableReadable(f) => PartialKeyPath::FailableReadable(Arc::new(move |rc: &Rc<Root>| f(&**rc))),
1352 PartialKeyPath::FailableWritable(_) => {
1353 panic!("Rc does not support writable keypaths (Rc only implements Deref, not DerefMut)")
1354 }
1355 PartialKeyPath::ReadableEnum { extract, embed } => PartialKeyPath::ReadableEnum {
1356 extract: Arc::new(move |rc: &Rc<Root>| extract(&**rc)),
1357 embed: Arc::new(move |value| Rc::new(embed(value))),
1358 },
1359 PartialKeyPath::WritableEnum { .. } => {
1360 panic!("Rc does not support writable keypaths (Rc only implements Deref, not DerefMut)")
1361 }
1362 PartialKeyPath::ReferenceWritable(_) => {
1363 panic!("Rc does not support writable keypaths (Rc only implements Deref, not DerefMut)")
1364 }
1365 PartialKeyPath::Owned(f) => PartialKeyPath::Owned(Arc::new(move |rc: Rc<Root>| f((*rc).clone()))),
1366 PartialKeyPath::FailableOwned(f) => PartialKeyPath::FailableOwned(Arc::new(move |rc: Rc<Root>| f((*rc).clone()))),
1367 PartialKeyPath::FailableCombined { readable, writable, owned } => PartialKeyPath::FailableCombined {
1368 readable: Arc::new(move |root| readable(&**root)),
1369 writable: Arc::new(move |_root| panic!("Arc does not support mutable access")),
1370 owned: Arc::new(move |root| panic!("Arc does not support owned keypaths")),
1371 },
1372 }
1373 }
1374
1375 pub fn for_result<E>(self) -> PartialKeyPath<Result<Root, E>>
1377 where
1378 Root: 'static,
1379 {
1380 match self {
1381 PartialKeyPath::Readable(f) => PartialKeyPath::FailableReadable(Arc::new(move |result: &Result<Root, E>| {
1382 result.as_ref().ok().map(|root| f(root) as &(dyn Any + Send + Sync))
1383 })),
1384 PartialKeyPath::Writable(f) => PartialKeyPath::FailableWritable(Arc::new(move |result: &mut Result<Root, E>| {
1385 result.as_mut().ok().map(|root| f(root) as &mut (dyn Any + Send + Sync))
1386 })),
1387 PartialKeyPath::FailableReadable(f) => PartialKeyPath::FailableReadable(Arc::new(move |result: &Result<Root, E>| {
1388 result.as_ref().ok().and_then(|root| f(root))
1389 })),
1390 PartialKeyPath::FailableWritable(f) => PartialKeyPath::FailableWritable(Arc::new(move |result: &mut Result<Root, E>| {
1391 result.as_mut().ok().and_then(|root| f(root))
1392 })),
1393 PartialKeyPath::ReadableEnum { extract, embed } => PartialKeyPath::ReadableEnum {
1394 extract: Arc::new(move |result: &Result<Root, E>| {
1395 result.as_ref().ok().and_then(|root| extract(root))
1396 }),
1397 embed: Arc::new(move |value| Ok(embed(value))),
1398 },
1399 PartialKeyPath::WritableEnum { extract, extract_mut, embed } => PartialKeyPath::WritableEnum {
1400 extract: Arc::new(move |result: &Result<Root, E>| {
1401 result.as_ref().ok().and_then(|root| extract(root))
1402 }),
1403 extract_mut: Arc::new(move |result: &mut Result<Root, E>| {
1404 result.as_mut().ok().and_then(|root| extract_mut(root))
1405 }),
1406 embed: Arc::new(move |value| Ok(embed(value))),
1407 },
1408 PartialKeyPath::ReferenceWritable(f) => PartialKeyPath::FailableWritable(Arc::new(move |result: &mut Result<Root, E>| {
1409 result.as_mut().ok().map(|root| f(root) as &mut (dyn Any + Send + Sync))
1410 })),
1411 PartialKeyPath::Owned(f) => PartialKeyPath::FailableOwned(Arc::new(move |result: Result<Root, E>| {
1412 result.ok().map(|root| f(root))
1413 })),
1414 PartialKeyPath::FailableOwned(f) => PartialKeyPath::FailableOwned(Arc::new(move |result: Result<Root, E>| {
1415 result.ok().and_then(|root| f(root))
1416 })),
1417 PartialKeyPath::FailableCombined { readable, writable, owned } => PartialKeyPath::FailableCombined {
1418 readable: Arc::new(move |result: &Result<Root, E>| {
1419 result.as_ref().ok().and_then(|root| readable(root))
1420 }),
1421 writable: Arc::new(move |result: &mut Result<Root, E>| {
1422 result.as_mut().ok().and_then(|root| writable(root))
1423 }),
1424 owned: Arc::new(move |result: Result<Root, E>| {
1425 result.ok().and_then(|root| owned(root))
1426 }),
1427 },
1428 }
1429 }
1430
1431 pub fn for_option(self) -> PartialKeyPath<Option<Root>>
1433 where
1434 Root: 'static,
1435 {
1436 match self {
1437 PartialKeyPath::Readable(f) => PartialKeyPath::FailableReadable(Arc::new(move |option: &Option<Root>| {
1438 option.as_ref().map(|root| f(root) as &(dyn Any + Send + Sync))
1439 })),
1440 PartialKeyPath::Writable(f) => PartialKeyPath::FailableWritable(Arc::new(move |option: &mut Option<Root>| {
1441 option.as_mut().map(|root| f(root) as &mut (dyn Any + Send + Sync))
1442 })),
1443 PartialKeyPath::FailableReadable(f) => PartialKeyPath::FailableReadable(Arc::new(move |option: &Option<Root>| {
1444 option.as_ref().and_then(|root| f(root))
1445 })),
1446 PartialKeyPath::FailableWritable(f) => PartialKeyPath::FailableWritable(Arc::new(move |option: &mut Option<Root>| {
1447 option.as_mut().and_then(|root| f(root))
1448 })),
1449 PartialKeyPath::ReadableEnum { extract, embed } => PartialKeyPath::ReadableEnum {
1450 extract: Arc::new(move |option: &Option<Root>| {
1451 option.as_ref().and_then(|root| extract(root))
1452 }),
1453 embed: Arc::new(move |value| Some(embed(value))),
1454 },
1455 PartialKeyPath::WritableEnum { extract, extract_mut, embed } => PartialKeyPath::WritableEnum {
1456 extract: Arc::new(move |option: &Option<Root>| {
1457 option.as_ref().and_then(|root| extract(root))
1458 }),
1459 extract_mut: Arc::new(move |option: &mut Option<Root>| {
1460 option.as_mut().and_then(|root| extract_mut(root))
1461 }),
1462 embed: Arc::new(move |value| Some(embed(value))),
1463 },
1464 PartialKeyPath::ReferenceWritable(f) => PartialKeyPath::FailableWritable(Arc::new(move |option: &mut Option<Root>| {
1465 option.as_mut().map(|root| f(root) as &mut (dyn Any + Send + Sync))
1466 })),
1467 PartialKeyPath::Owned(f) => PartialKeyPath::FailableOwned(Arc::new(move |option: Option<Root>| {
1468 option.map(|root| f(root))
1469 })),
1470 PartialKeyPath::FailableOwned(f) => PartialKeyPath::FailableOwned(Arc::new(move |option: Option<Root>| {
1471 option.and_then(|root| f(root))
1472 })),
1473 PartialKeyPath::FailableCombined { readable, writable, owned } => PartialKeyPath::FailableCombined {
1474 readable: Arc::new(move |option: &Option<Root>| {
1475 option.as_ref().and_then(|root| readable(root))
1476 }),
1477 writable: Arc::new(move |option: &mut Option<Root>| {
1478 option.as_mut().and_then(|root| writable(root))
1479 }),
1480 owned: Arc::new(move |option: Option<Root>| {
1481 option.and_then(|root| owned(root))
1482 }),
1483 },
1484 }
1485 }
1486
1487 pub fn for_arc_rwlock(self) -> PartialKeyPath<Arc<RwLock<Root>>>
1490 where
1491 Root: 'static + Clone,
1492 {
1493 match self {
1494 PartialKeyPath::Readable(_) => {
1495 panic!("Arc<RwLock> does not support readable keypaths due to guard lifetime constraints. Use owned keypaths instead.")
1496 }
1497 PartialKeyPath::Writable(_) => {
1498 panic!("Arc<RwLock> does not support writable keypaths due to guard lifetime constraints. Use owned keypaths instead.")
1499 }
1500 PartialKeyPath::FailableReadable(_) => {
1501 panic!("Arc<RwLock> does not support failable readable keypaths due to guard lifetime constraints. Use owned keypaths instead.")
1502 }
1503 PartialKeyPath::FailableWritable(_) => {
1504 panic!("Arc<RwLock> does not support failable writable keypaths due to guard lifetime constraints. Use owned keypaths instead.")
1505 }
1506 PartialKeyPath::ReadableEnum { .. } => {
1507 panic!("Arc<RwLock> does not support readable enum keypaths due to guard lifetime constraints. Use owned keypaths instead.")
1508 }
1509 PartialKeyPath::WritableEnum { .. } => {
1510 panic!("Arc<RwLock> does not support writable enum keypaths due to guard lifetime constraints. Use owned keypaths instead.")
1511 }
1512 PartialKeyPath::ReferenceWritable(_) => {
1513 panic!("Arc<RwLock> does not support reference writable keypaths due to guard lifetime constraints. Use owned keypaths instead.")
1514 }
1515 PartialKeyPath::Owned(f) => PartialKeyPath::Owned(Arc::new(move |arc_rwlock: Arc<RwLock<Root>>| {
1516 let guard = arc_rwlock.read().unwrap();
1517 let value = f((*guard).clone());
1518 drop(guard); value
1520 })),
1521 PartialKeyPath::FailableOwned(f) => PartialKeyPath::FailableOwned(Arc::new(move |arc_rwlock: Arc<RwLock<Root>>| {
1522 let guard = arc_rwlock.read().unwrap();
1523 let value = f((*guard).clone());
1524 drop(guard); value
1526 })),
1527 PartialKeyPath::FailableCombined { owned, .. } => PartialKeyPath::FailableOwned(Arc::new(move |arc_rwlock: Arc<RwLock<Root>>| {
1528 let guard = arc_rwlock.read().unwrap();
1529 let value = owned((*guard).clone());
1530 drop(guard); value
1532 })),
1533 }
1534 }
1535
1536 pub fn for_arc_mutex(self) -> PartialKeyPath<Arc<Mutex<Root>>>
1539 where
1540 Root: 'static + Clone,
1541 {
1542 match self {
1543 PartialKeyPath::Readable(_) => {
1544 panic!("Arc<Mutex> does not support readable keypaths due to guard lifetime constraints. Use owned keypaths instead.")
1545 }
1546 PartialKeyPath::Writable(_) => {
1547 panic!("Arc<Mutex> does not support writable keypaths due to guard lifetime constraints. Use owned keypaths instead.")
1548 }
1549 PartialKeyPath::FailableReadable(_) => {
1550 panic!("Arc<Mutex> does not support failable readable keypaths due to guard lifetime constraints. Use owned keypaths instead.")
1551 }
1552 PartialKeyPath::FailableWritable(_) => {
1553 panic!("Arc<Mutex> does not support failable writable keypaths due to guard lifetime constraints. Use owned keypaths instead.")
1554 }
1555 PartialKeyPath::ReadableEnum { .. } => {
1556 panic!("Arc<Mutex> does not support readable enum keypaths due to guard lifetime constraints. Use owned keypaths instead.")
1557 }
1558 PartialKeyPath::WritableEnum { .. } => {
1559 panic!("Arc<Mutex> does not support writable enum keypaths due to guard lifetime constraints. Use owned keypaths instead.")
1560 }
1561 PartialKeyPath::ReferenceWritable(_) => {
1562 panic!("Arc<Mutex> does not support reference writable keypaths due to guard lifetime constraints. Use owned keypaths instead.")
1563 }
1564 PartialKeyPath::Owned(f) => PartialKeyPath::Owned(Arc::new(move |arc_mutex: Arc<Mutex<Root>>| {
1565 let guard = arc_mutex.lock().unwrap();
1566 let value = f((*guard).clone());
1567 drop(guard); value
1569 })),
1570 PartialKeyPath::FailableOwned(f) => PartialKeyPath::FailableOwned(Arc::new(move |arc_mutex: Arc<Mutex<Root>>| {
1571 let guard = arc_mutex.lock().unwrap();
1572 let value = f((*guard).clone());
1573 drop(guard); value
1575 })),
1576 PartialKeyPath::FailableCombined { owned, .. } => PartialKeyPath::FailableOwned(Arc::new(move |arc_mutex: Arc<Mutex<Root>>| {
1577 let guard = arc_mutex.lock().unwrap();
1578 let value = owned((*guard).clone());
1579 drop(guard); value
1581 })),
1582 }
1583 }
1584
1585 #[cfg(feature = "tagged_core")]
1587 pub fn for_tagged<Tag>(self) -> PartialKeyPath<Tagged<Root, Tag>>
1588 where
1589 Root: Clone + 'static,
1590 {
1591 match self {
1592 PartialKeyPath::Readable(f) => PartialKeyPath::Readable(Arc::new(move |tagged: &Tagged<Root, Tag>| {
1593 f(&*tagged) as &(dyn Any + Send + Sync)
1594 })),
1595 PartialKeyPath::Writable(_) => {
1596 panic!("Tagged does not support writable keypaths (Tagged only implements Deref, not DerefMut)")
1597 }
1598 PartialKeyPath::FailableReadable(f) => PartialKeyPath::FailableReadable(Arc::new(move |tagged: &Tagged<Root, Tag>| {
1599 f(&*tagged)
1600 })),
1601 PartialKeyPath::FailableWritable(_) => {
1602 panic!("Tagged does not support writable keypaths (Tagged only implements Deref, not DerefMut)")
1603 }
1604 PartialKeyPath::ReadableEnum { extract, embed } => PartialKeyPath::ReadableEnum {
1605 extract: Arc::new(move |tagged: &Tagged<Root, Tag>| {
1606 extract(&*tagged)
1607 }),
1608 embed: Arc::new(move |value| embed(value).into()),
1609 },
1610 PartialKeyPath::WritableEnum { .. } => {
1611 panic!("Tagged does not support writable keypaths (Tagged only implements Deref, not DerefMut)")
1612 }
1613 PartialKeyPath::ReferenceWritable(_) => {
1614 panic!("Tagged does not support writable keypaths (Tagged only implements Deref, not DerefMut)")
1615 }
1616 PartialKeyPath::Owned(f) => PartialKeyPath::Owned(Arc::new(move |tagged: Tagged<Root, Tag>| {
1617 f((*tagged).clone())
1618 })),
1619 PartialKeyPath::FailableOwned(f) => PartialKeyPath::FailableOwned(Arc::new(move |tagged: Tagged<Root, Tag>| {
1620 f((*tagged).clone())
1621 })),
1622 PartialKeyPath::FailableCombined { readable, writable, owned } => PartialKeyPath::FailableCombined {
1623 readable: Arc::new(move |tagged: &Tagged<Root, Tag>| {
1624 readable(&*tagged)
1625 }),
1626 writable: Arc::new(move |_tagged: &mut Tagged<Root, Tag>| {
1627 panic!("Tagged does not support writable keypaths (Tagged only implements Deref, not DerefMut)")
1628 }),
1629 owned: Arc::new(move |tagged: Tagged<Root, Tag>| {
1630 owned((*tagged).clone())
1631 }),
1632 },
1633 }
1634 }
1635}
1636
1637impl AnyKeyPath {
1639 #[inline]
1641 pub fn get<'a>(&'a self, root: &'a (dyn Any + Send + Sync)) -> Option<&'a (dyn Any + Send + Sync)> {
1642 match self {
1643 AnyKeyPath::Readable(f) => Some(f(root)),
1644 AnyKeyPath::Writable(_) => None, AnyKeyPath::FailableReadable(f) => f(root),
1646 AnyKeyPath::FailableWritable(_) => None, AnyKeyPath::ReadableEnum { extract, .. } => extract(root),
1648 AnyKeyPath::WritableEnum { extract, .. } => extract(root),
1649 AnyKeyPath::ReferenceWritable(_) => None, AnyKeyPath::Owned(_) => None, AnyKeyPath::FailableOwned(_) => None, AnyKeyPath::FailableCombined { readable, .. } => readable(root),
1653 }
1654 }
1655
1656 #[inline]
1658 pub fn get_mut<'a>(&'a self, root: &'a mut (dyn Any + Send + Sync)) -> Option<&'a mut (dyn Any + Send + Sync)> {
1659 match self {
1660 AnyKeyPath::Readable(_) => None, AnyKeyPath::Writable(f) => Some(f(root)),
1662 AnyKeyPath::FailableReadable(_) => None, AnyKeyPath::FailableWritable(f) => f(root),
1664 AnyKeyPath::ReadableEnum { .. } => None, AnyKeyPath::WritableEnum { extract_mut, .. } => extract_mut(root),
1666 AnyKeyPath::ReferenceWritable(f) => Some(f(root)),
1667 AnyKeyPath::Owned(_) => None, AnyKeyPath::FailableOwned(_) => None, AnyKeyPath::FailableCombined { writable, .. } => writable(root),
1670 }
1671 }
1672
1673 #[inline]
1675 pub fn get_owned(self, root: Box<dyn Any>) -> Box<dyn Any> {
1676 match self {
1677 AnyKeyPath::Owned(f) => f(root),
1678 _ => panic!("get_owned only works with owned keypaths"),
1679 }
1680 }
1681
1682 #[inline]
1684 pub fn get_failable_owned(self, root: Box<dyn Any>) -> Option<Box<dyn Any>> {
1685 match self {
1686 AnyKeyPath::FailableOwned(f) => f(root),
1687 AnyKeyPath::FailableCombined { owned, .. } => owned(root),
1688 _ => panic!("get_failable_owned only works with failable owned keypaths"),
1689 }
1690 }
1691
1692 #[inline]
1694 pub fn kind_name(&self) -> &'static str {
1695 match self {
1696 AnyKeyPath::Readable(_) => "AnyKeyPath::Readable",
1697 AnyKeyPath::Writable(_) => "AnyKeyPath::Writable",
1698 AnyKeyPath::FailableReadable(_) => "AnyKeyPath::FailableReadable",
1699 AnyKeyPath::FailableWritable(_) => "AnyKeyPath::FailableWritable",
1700 AnyKeyPath::ReadableEnum { .. } => "AnyKeyPath::ReadableEnum",
1701 AnyKeyPath::WritableEnum { .. } => "AnyKeyPath::WritableEnum",
1702 AnyKeyPath::ReferenceWritable(_) => "AnyKeyPath::ReferenceWritable",
1703 AnyKeyPath::Owned(_) => "AnyKeyPath::Owned",
1704 AnyKeyPath::FailableOwned(_) => "AnyKeyPath::FailableOwned",
1705 AnyKeyPath::FailableCombined { .. } => "AnyKeyPath::FailableCombined",
1706 }
1707 }
1708
1709 pub fn for_arc<Root>(self) -> AnyKeyPath
1713 where
1714 Root: 'static + Send + Sync + Clone,
1715 {
1716 match self {
1717 AnyKeyPath::Readable(f) => AnyKeyPath::Readable(Arc::new(move |root| {
1718 let arc = root.downcast_ref::<Arc<Root>>().unwrap();
1719 f(&**arc as &(dyn Any + Send + Sync))
1720 })),
1721 AnyKeyPath::Writable(_) => {
1722 panic!("Arc does not support writable keypaths (Arc only implements Deref, not DerefMut)")
1723 }
1724 AnyKeyPath::FailableReadable(f) => AnyKeyPath::FailableReadable(Arc::new(move |root| {
1725 let arc = root.downcast_ref::<Arc<Root>>().unwrap();
1726 f(&**arc as &(dyn Any + Send + Sync))
1727 })),
1728 AnyKeyPath::FailableWritable(_) => {
1729 panic!("Arc does not support writable keypaths (Arc only implements Deref, not DerefMut)")
1730 }
1731 AnyKeyPath::ReadableEnum { extract, embed } => AnyKeyPath::ReadableEnum {
1732 extract: Arc::new(move |root| {
1733 let arc = root.downcast_ref::<Arc<Root>>().unwrap();
1734 extract(&**arc as &(dyn Any + Send + Sync))
1735 }),
1736 embed: Arc::new(move |value| {
1737 let inner = embed(value);
1738 Box::new(Arc::new(*inner.downcast::<Root>().unwrap())) as Box<dyn Any>
1739 }),
1740 },
1741 AnyKeyPath::WritableEnum { .. } => {
1742 panic!("Arc does not support writable keypaths (Arc only implements Deref, not DerefMut)")
1743 }
1744 AnyKeyPath::ReferenceWritable(_) => {
1745 panic!("Arc does not support writable keypaths (Arc only implements Deref, not DerefMut)")
1746 }
1747 AnyKeyPath::Owned(f) => AnyKeyPath::Owned(Arc::new(move |root| {
1748 let arc = *root.downcast::<Arc<Root>>().unwrap();
1749 f(Box::new((*arc).clone()) as Box<dyn Any>)
1750 })),
1751 AnyKeyPath::FailableOwned(f) => AnyKeyPath::FailableOwned(Arc::new(move |root| {
1752 let arc = *root.downcast::<Arc<Root>>().unwrap();
1753 f(Box::new((*arc).clone()) as Box<dyn Any>)
1754 })),
1755 AnyKeyPath::FailableCombined { readable, writable, owned } => AnyKeyPath::FailableCombined {
1756 readable: Arc::new(move |root| {
1757 let arc = root.downcast_ref::<Arc<Root>>().unwrap();
1758 readable(&**arc as &(dyn Any + Send + Sync))
1759 }),
1760 writable: Arc::new(move |_root| {
1761 panic!("Arc does not support writable keypaths (Arc only implements Deref, not DerefMut)")
1762 }),
1763 owned: Arc::new(move |root| {
1764 let arc = *root.downcast::<Arc<Root>>().unwrap();
1765 owned(Box::new((*arc).clone()) as Box<dyn Any>)
1766 }),
1767 },
1768 }
1769 }
1770
1771 pub fn for_box<Root>(self) -> AnyKeyPath
1773 where
1774 Root: 'static + Send + Sync,
1775 {
1776 match self {
1777 AnyKeyPath::Readable(f) => AnyKeyPath::Readable(Arc::new(move |root| {
1778 let boxed = root.downcast_ref::<Box<Root>>().unwrap();
1779 f(&**boxed as &(dyn Any + Send + Sync))
1780 })),
1781 AnyKeyPath::Writable(f) => AnyKeyPath::Writable(Arc::new(move |root| {
1782 let boxed = root.downcast_mut::<Box<Root>>().unwrap();
1783 f(&mut **boxed as &mut (dyn Any + Send + Sync))
1784 })),
1785 AnyKeyPath::FailableReadable(f) => AnyKeyPath::FailableReadable(Arc::new(move |root| {
1786 let boxed = root.downcast_ref::<Box<Root>>().unwrap();
1787 f(&**boxed as &(dyn Any + Send + Sync))
1788 })),
1789 AnyKeyPath::FailableWritable(f) => AnyKeyPath::FailableWritable(Arc::new(move |root| {
1790 let boxed = root.downcast_mut::<Box<Root>>().unwrap();
1791 f(&mut **boxed as &mut (dyn Any + Send + Sync))
1792 })),
1793 AnyKeyPath::ReadableEnum { extract, embed } => AnyKeyPath::ReadableEnum {
1794 extract: Arc::new(move |root| {
1795 let boxed = root.downcast_ref::<Box<Root>>().unwrap();
1796 extract(&**boxed as &(dyn Any + Send + Sync))
1797 }),
1798 embed: Arc::new(move |value| {
1799 let inner = embed(value);
1800 Box::new(Box::new(*inner.downcast::<Root>().unwrap())) as Box<dyn Any>
1801 }),
1802 },
1803 AnyKeyPath::WritableEnum { extract, extract_mut, embed } => AnyKeyPath::WritableEnum {
1804 extract: Arc::new(move |root| {
1805 let boxed = root.downcast_ref::<Box<Root>>().unwrap();
1806 extract(&**boxed as &(dyn Any + Send + Sync))
1807 }),
1808 extract_mut: Arc::new(move |root| {
1809 let boxed = root.downcast_mut::<Box<Root>>().unwrap();
1810 extract_mut(&mut **boxed as &mut (dyn Any + Send + Sync))
1811 }),
1812 embed: Arc::new(move |value| {
1813 let inner = embed(value);
1814 Box::new(Box::new(*inner.downcast::<Root>().unwrap())) as Box<dyn Any>
1815 }),
1816 },
1817 AnyKeyPath::ReferenceWritable(f) => AnyKeyPath::ReferenceWritable(Arc::new(move |root| {
1818 let boxed = root.downcast_mut::<Box<Root>>().unwrap();
1819 f(&mut **boxed as &mut (dyn Any + Send + Sync))
1820 })),
1821 AnyKeyPath::Owned(f) => AnyKeyPath::Owned(Arc::new(move |root| {
1822 let boxed = *root.downcast::<Box<Root>>().unwrap();
1823 f(Box::new(*boxed) as Box<dyn Any>)
1824 })),
1825 AnyKeyPath::FailableOwned(f) => AnyKeyPath::FailableOwned(Arc::new(move |root| {
1826 let boxed = *root.downcast::<Box<Root>>().unwrap();
1827 f(Box::new(*boxed) as Box<dyn Any>)
1828 })),
1829 AnyKeyPath::FailableCombined { readable, writable, owned } => AnyKeyPath::FailableCombined {
1830 readable: Arc::new(move |root| {
1831 let boxed = root.downcast_ref::<Box<Root>>().unwrap();
1832 readable(&**boxed as &(dyn Any + Send + Sync))
1833 }),
1834 writable: Arc::new(move |root| {
1835 let boxed = root.downcast_mut::<Box<Root>>().unwrap();
1836 writable(&mut **boxed as &mut (dyn Any + Send + Sync))
1837 }),
1838 owned: Arc::new(move |root| {
1839 let boxed = *root.downcast::<Box<Root>>().unwrap();
1840 owned(Box::new(*boxed) as Box<dyn Any>)
1841 }),
1842 },
1843 }
1844 }
1845
1846 pub fn for_rc<Root>(self) -> AnyKeyPath
1848 where
1849 Root: 'static + Send + Sync + Clone,
1850 {
1851 match self {
1852 AnyKeyPath::Readable(f) => AnyKeyPath::Readable(Arc::new(move |root| {
1853 let rc = root.downcast_ref::<Rc<Root>>().unwrap();
1854 f(&**rc as &(dyn Any + Send + Sync))
1855 })),
1856 AnyKeyPath::Writable(_) => {
1857 panic!("Rc does not support writable keypaths (Rc only implements Deref, not DerefMut)")
1858 }
1859 AnyKeyPath::FailableReadable(f) => AnyKeyPath::FailableReadable(Arc::new(move |root| {
1860 let rc = root.downcast_ref::<Rc<Root>>().unwrap();
1861 f(&**rc as &(dyn Any + Send + Sync))
1862 })),
1863 AnyKeyPath::FailableWritable(_) => {
1864 panic!("Rc does not support writable keypaths (Rc only implements Deref, not DerefMut)")
1865 }
1866 AnyKeyPath::ReadableEnum { extract, embed } => AnyKeyPath::ReadableEnum {
1867 extract: Arc::new(move |root| {
1868 let rc = root.downcast_ref::<Rc<Root>>().unwrap();
1869 extract(&**rc as &(dyn Any + Send + Sync))
1870 }),
1871 embed: Arc::new(move |value| {
1872 let inner = embed(value);
1873 Box::new(Rc::new(*inner.downcast::<Root>().unwrap())) as Box<dyn Any>
1874 }),
1875 },
1876 AnyKeyPath::WritableEnum { .. } => {
1877 panic!("Rc does not support writable keypaths (Rc only implements Deref, not DerefMut)")
1878 }
1879 AnyKeyPath::ReferenceWritable(_) => {
1880 panic!("Rc does not support writable keypaths (Rc only implements Deref, not DerefMut)")
1881 }
1882 AnyKeyPath::Owned(f) => AnyKeyPath::Owned(Arc::new(move |root| {
1883 let rc = *root.downcast::<Rc<Root>>().unwrap();
1884 f(Box::new((*rc).clone()) as Box<dyn Any>)
1885 })),
1886 AnyKeyPath::FailableOwned(f) => AnyKeyPath::FailableOwned(Arc::new(move |root| {
1887 let rc = *root.downcast::<Rc<Root>>().unwrap();
1888 f(Box::new((*rc).clone()) as Box<dyn Any>)
1889 })),
1890 AnyKeyPath::FailableCombined { readable, writable, owned } => AnyKeyPath::FailableCombined {
1891 readable: Arc::new(move |root| {
1892 let rc = root.downcast_ref::<Rc<Root>>().unwrap();
1893 readable(&**rc as &(dyn Any + Send + Sync))
1894 }),
1895 writable: Arc::new(move |_root| {
1896 panic!("Rc does not support writable keypaths (Rc only implements Deref, not DerefMut)")
1897 }),
1898 owned: Arc::new(move |root| {
1899 let rc = *root.downcast::<Rc<Root>>().unwrap();
1900 owned(Box::new((*rc).clone()) as Box<dyn Any>)
1901 }),
1902 },
1903 }
1904 }
1905
1906 pub fn for_result<Root, E>(self) -> AnyKeyPath
1908 where
1909 Root: 'static + Send + Sync,
1910 E: 'static,
1911 {
1912 match self {
1913 AnyKeyPath::Readable(f) => AnyKeyPath::FailableReadable(Arc::new(move |root| {
1914 let result = root.downcast_ref::<Result<Root, E>>().unwrap();
1915 result.as_ref().ok().map(|inner| f(inner as &(dyn Any + Send + Sync)))
1916 })),
1917 AnyKeyPath::Writable(f) => AnyKeyPath::FailableWritable(Arc::new(move |root| {
1918 let result = root.downcast_mut::<Result<Root, E>>().unwrap();
1919 result.as_mut().ok().map(|inner| f(inner as &mut (dyn Any + Send + Sync)))
1920 })),
1921 AnyKeyPath::FailableReadable(f) => AnyKeyPath::FailableReadable(Arc::new(move |root| {
1922 let result = root.downcast_ref::<Result<Root, E>>().unwrap();
1923 result.as_ref().ok().and_then(|inner| f(inner as &(dyn Any + Send + Sync)))
1924 })),
1925 AnyKeyPath::FailableWritable(f) => AnyKeyPath::FailableWritable(Arc::new(move |root| {
1926 let result = root.downcast_mut::<Result<Root, E>>().unwrap();
1927 result.as_mut().ok().and_then(|inner| f(inner as &mut (dyn Any + Send + Sync)))
1928 })),
1929 AnyKeyPath::ReadableEnum { extract, embed } => AnyKeyPath::ReadableEnum {
1930 extract: Arc::new(move |root| {
1931 let result = root.downcast_ref::<Result<Root, E>>().unwrap();
1932 result.as_ref().ok().and_then(|inner| extract(inner as &(dyn Any + Send + Sync)))
1933 }),
1934 embed: Arc::new(move |value| {
1935 let inner = embed(value);
1936 Box::new(Ok::<Root, E>(*inner.downcast::<Root>().unwrap())) as Box<dyn Any>
1937 }),
1938 },
1939 AnyKeyPath::WritableEnum { extract, extract_mut, embed } => AnyKeyPath::WritableEnum {
1940 extract: Arc::new(move |root| {
1941 let result = root.downcast_ref::<Result<Root, E>>().unwrap();
1942 result.as_ref().ok().and_then(|inner| extract(inner as &(dyn Any + Send + Sync)))
1943 }),
1944 extract_mut: Arc::new(move |root| {
1945 let result = root.downcast_mut::<Result<Root, E>>().unwrap();
1946 result.as_mut().ok().and_then(|inner| extract_mut(inner as &mut (dyn Any + Send + Sync)))
1947 }),
1948 embed: Arc::new(move |value| {
1949 let inner = embed(value);
1950 Box::new(Ok::<Root, E>(*inner.downcast::<Root>().unwrap())) as Box<dyn Any>
1951 }),
1952 },
1953 AnyKeyPath::ReferenceWritable(f) => AnyKeyPath::FailableWritable(Arc::new(move |root| {
1954 let result = root.downcast_mut::<Result<Root, E>>().unwrap();
1955 result.as_mut().ok().map(|inner| f(inner as &mut (dyn Any + Send + Sync)))
1956 })),
1957 AnyKeyPath::Owned(f) => AnyKeyPath::FailableOwned(Arc::new(move |root| {
1958 let result = *root.downcast::<Result<Root, E>>().unwrap();
1959 result.ok().map(|inner| f(Box::new(inner) as Box<dyn Any>))
1960 })),
1961 AnyKeyPath::FailableOwned(f) => AnyKeyPath::FailableOwned(Arc::new(move |root| {
1962 let result = *root.downcast::<Result<Root, E>>().unwrap();
1963 result.ok().and_then(|inner| f(Box::new(inner) as Box<dyn Any>))
1964 })),
1965 AnyKeyPath::FailableCombined { readable, writable, owned } => AnyKeyPath::FailableCombined {
1966 readable: Arc::new(move |root| {
1967 let result = root.downcast_ref::<Result<Root, E>>().unwrap();
1968 result.as_ref().ok().and_then(|inner| readable(inner as &(dyn Any + Send + Sync)))
1969 }),
1970 writable: Arc::new(move |root| {
1971 let result = root.downcast_mut::<Result<Root, E>>().unwrap();
1972 result.as_mut().ok().and_then(|inner| writable(inner as &mut (dyn Any + Send + Sync)))
1973 }),
1974 owned: Arc::new(move |root| {
1975 let result = *root.downcast::<Result<Root, E>>().unwrap();
1976 result.ok().and_then(|inner| owned(Box::new(inner) as Box<dyn Any>))
1977 }),
1978 },
1979 }
1980 }
1981
1982 pub fn for_option<Root>(self) -> AnyKeyPath
1984 where
1985 Root: 'static + Send + Sync,
1986 {
1987 match self {
1988 AnyKeyPath::Readable(f) => AnyKeyPath::FailableReadable(Arc::new(move |root| {
1989 let option = root.downcast_ref::<Option<Root>>().unwrap();
1990 option.as_ref().map(|inner| f(inner as &(dyn Any + Send + Sync)))
1991 })),
1992 AnyKeyPath::Writable(f) => AnyKeyPath::FailableWritable(Arc::new(move |root| {
1993 let option = root.downcast_mut::<Option<Root>>().unwrap();
1994 option.as_mut().map(|inner| f(inner as &mut (dyn Any + Send + Sync)))
1995 })),
1996 AnyKeyPath::FailableReadable(f) => AnyKeyPath::FailableReadable(Arc::new(move |root| {
1997 let option = root.downcast_ref::<Option<Root>>().unwrap();
1998 option.as_ref().and_then(|inner| f(inner as &(dyn Any + Send + Sync)))
1999 })),
2000 AnyKeyPath::FailableWritable(f) => AnyKeyPath::FailableWritable(Arc::new(move |root| {
2001 let option = root.downcast_mut::<Option<Root>>().unwrap();
2002 option.as_mut().and_then(|inner| f(inner as &mut (dyn Any + Send + Sync)))
2003 })),
2004 AnyKeyPath::ReadableEnum { extract, embed } => AnyKeyPath::ReadableEnum {
2005 extract: Arc::new(move |root| {
2006 let option = root.downcast_ref::<Option<Root>>().unwrap();
2007 option.as_ref().and_then(|inner| extract(inner as &(dyn Any + Send + Sync)))
2008 }),
2009 embed: Arc::new(move |value| {
2010 let inner = embed(value);
2011 Box::new(Some(*inner.downcast::<Root>().unwrap())) as Box<dyn Any>
2012 }),
2013 },
2014 AnyKeyPath::WritableEnum { extract, extract_mut, embed } => AnyKeyPath::WritableEnum {
2015 extract: Arc::new(move |root| {
2016 let option = root.downcast_ref::<Option<Root>>().unwrap();
2017 option.as_ref().and_then(|inner| extract(inner as &(dyn Any + Send + Sync)))
2018 }),
2019 extract_mut: Arc::new(move |root| {
2020 let option = root.downcast_mut::<Option<Root>>().unwrap();
2021 option.as_mut().and_then(|inner| extract_mut(inner as &mut (dyn Any + Send + Sync)))
2022 }),
2023 embed: Arc::new(move |value| {
2024 let inner = embed(value);
2025 Box::new(Some(*inner.downcast::<Root>().unwrap())) as Box<dyn Any>
2026 }),
2027 },
2028 AnyKeyPath::ReferenceWritable(f) => AnyKeyPath::FailableWritable(Arc::new(move |root| {
2029 let option = root.downcast_mut::<Option<Root>>().unwrap();
2030 option.as_mut().map(|inner| f(inner as &mut (dyn Any + Send + Sync)))
2031 })),
2032 AnyKeyPath::Owned(f) => AnyKeyPath::FailableOwned(Arc::new(move |root| {
2033 let option = *root.downcast::<Option<Root>>().unwrap();
2034 option.map(|inner| f(Box::new(inner) as Box<dyn Any>))
2035 })),
2036 AnyKeyPath::FailableOwned(f) => AnyKeyPath::FailableOwned(Arc::new(move |root| {
2037 let option = *root.downcast::<Option<Root>>().unwrap();
2038 option.and_then(|inner| f(Box::new(inner) as Box<dyn Any>))
2039 })),
2040 AnyKeyPath::FailableCombined { readable, writable, owned } => AnyKeyPath::FailableCombined {
2041 readable: Arc::new(move |root| {
2042 let option = root.downcast_ref::<Option<Root>>().unwrap();
2043 option.as_ref().and_then(|inner| readable(inner as &(dyn Any + Send + Sync)))
2044 }),
2045 writable: Arc::new(move |root| {
2046 let option = root.downcast_mut::<Option<Root>>().unwrap();
2047 option.as_mut().and_then(|inner| writable(inner as &mut (dyn Any + Send + Sync)))
2048 }),
2049 owned: Arc::new(move |root| {
2050 let option = *root.downcast::<Option<Root>>().unwrap();
2051 option.and_then(|inner| owned(Box::new(inner) as Box<dyn Any>))
2052 }),
2053 },
2054 }
2055 }
2056
2057 pub fn for_arc_rwlock<Root>(self) -> AnyKeyPath
2060 where
2061 Root: 'static + Send + Sync + Clone,
2062 {
2063 match self {
2064 AnyKeyPath::Readable(_) => {
2065 panic!("Arc<RwLock> does not support readable keypaths due to guard lifetime constraints. Use owned keypaths instead.")
2066 }
2067 AnyKeyPath::Writable(_) => {
2068 panic!("Arc<RwLock> does not support writable keypaths due to guard lifetime constraints. Use owned keypaths instead.")
2069 }
2070 AnyKeyPath::FailableReadable(_) => {
2071 panic!("Arc<RwLock> does not support failable readable keypaths due to guard lifetime constraints. Use owned keypaths instead.")
2072 }
2073 AnyKeyPath::FailableWritable(_) => {
2074 panic!("Arc<RwLock> does not support failable writable keypaths due to guard lifetime constraints. Use owned keypaths instead.")
2075 }
2076 AnyKeyPath::ReadableEnum { .. } => {
2077 panic!("Arc<RwLock> does not support readable enum keypaths due to guard lifetime constraints. Use owned keypaths instead.")
2078 }
2079 AnyKeyPath::WritableEnum { .. } => {
2080 panic!("Arc<RwLock> does not support writable enum keypaths due to guard lifetime constraints. Use owned keypaths instead.")
2081 }
2082 AnyKeyPath::ReferenceWritable(_) => {
2083 panic!("Arc<RwLock> does not support reference writable keypaths due to guard lifetime constraints. Use owned keypaths instead.")
2084 }
2085 AnyKeyPath::Owned(f) => AnyKeyPath::Owned(Arc::new(move |root| {
2086 let arc_rwlock = *root.downcast::<Arc<RwLock<Root>>>().unwrap();
2087 let guard = arc_rwlock.read().unwrap();
2088 let value = f(Box::new((*guard).clone()) as Box<dyn Any>);
2089 drop(guard); value
2091 })),
2092 AnyKeyPath::FailableOwned(f) => AnyKeyPath::FailableOwned(Arc::new(move |root| {
2093 let arc_rwlock = *root.downcast::<Arc<RwLock<Root>>>().unwrap();
2094 let guard = arc_rwlock.read().unwrap();
2095 let value = f(Box::new((*guard).clone()) as Box<dyn Any>);
2096 drop(guard); value
2098 })),
2099 AnyKeyPath::FailableCombined { owned, .. } => AnyKeyPath::FailableOwned(Arc::new(move |root| {
2100 let arc_rwlock = *root.downcast::<Arc<RwLock<Root>>>().unwrap();
2101 let guard = arc_rwlock.read().unwrap();
2102 let value = owned(Box::new((*guard).clone()) as Box<dyn Any>);
2103 drop(guard); value
2105 })),
2106 }
2107 }
2108
2109 pub fn for_arc_mutex<Root>(self) -> AnyKeyPath
2112 where
2113 Root: 'static + Send + Sync + Clone,
2114 {
2115 match self {
2116 AnyKeyPath::Readable(_) => {
2117 panic!("Arc<Mutex> does not support readable keypaths due to guard lifetime constraints. Use owned keypaths instead.")
2118 }
2119 AnyKeyPath::Writable(_) => {
2120 panic!("Arc<Mutex> does not support writable keypaths due to guard lifetime constraints. Use owned keypaths instead.")
2121 }
2122 AnyKeyPath::FailableReadable(_) => {
2123 panic!("Arc<Mutex> does not support failable readable keypaths due to guard lifetime constraints. Use owned keypaths instead.")
2124 }
2125 AnyKeyPath::FailableWritable(_) => {
2126 panic!("Arc<Mutex> does not support failable writable keypaths due to guard lifetime constraints. Use owned keypaths instead.")
2127 }
2128 AnyKeyPath::ReadableEnum { .. } => {
2129 panic!("Arc<Mutex> does not support readable enum keypaths due to guard lifetime constraints. Use owned keypaths instead.")
2130 }
2131 AnyKeyPath::WritableEnum { .. } => {
2132 panic!("Arc<Mutex> does not support writable enum keypaths due to guard lifetime constraints. Use owned keypaths instead.")
2133 }
2134 AnyKeyPath::ReferenceWritable(_) => {
2135 panic!("Arc<Mutex> does not support reference writable keypaths due to guard lifetime constraints. Use owned keypaths instead.")
2136 }
2137 AnyKeyPath::Owned(f) => AnyKeyPath::Owned(Arc::new(move |root| {
2138 let arc_mutex = *root.downcast::<Arc<Mutex<Root>>>().unwrap();
2139 let guard = arc_mutex.lock().unwrap();
2140 let value = f(Box::new((*guard).clone()) as Box<dyn Any>);
2141 drop(guard); value
2143 })),
2144 AnyKeyPath::FailableOwned(f) => AnyKeyPath::FailableOwned(Arc::new(move |root| {
2145 let arc_mutex = *root.downcast::<Arc<Mutex<Root>>>().unwrap();
2146 let guard = arc_mutex.lock().unwrap();
2147 let value = f(Box::new((*guard).clone()) as Box<dyn Any>);
2148 drop(guard); value
2150 })),
2151 AnyKeyPath::FailableCombined { owned, .. } => AnyKeyPath::FailableOwned(Arc::new(move |root| {
2152 let arc_mutex = *root.downcast::<Arc<Mutex<Root>>>().unwrap();
2153 let guard = arc_mutex.lock().unwrap();
2154 let value = owned(Box::new((*guard).clone()) as Box<dyn Any>);
2155 drop(guard); value
2157 })),
2158 }
2159 }
2160
2161 #[cfg(feature = "tagged_core")]
2163 pub fn for_tagged<Root, Tag>(self) -> AnyKeyPath
2164 where
2165 Root: Clone + 'static + Send + Sync,
2166 Tag: Send + Sync + 'static,
2167 {
2168 match self {
2169 AnyKeyPath::Readable(f) => AnyKeyPath::Readable(Arc::new(move |root| {
2170 let tagged = root.downcast_ref::<Tagged<Root, Tag>>().unwrap();
2171 f(&*tagged as &(dyn Any + Send + Sync))
2172 })),
2173 AnyKeyPath::Writable(_) => {
2174 panic!("Tagged does not support writable keypaths (Tagged only implements Deref, not DerefMut)")
2175 }
2176 AnyKeyPath::FailableReadable(f) => AnyKeyPath::FailableReadable(Arc::new(move |root| {
2177 let tagged = root.downcast_ref::<Tagged<Root, Tag>>().unwrap();
2178 f(&*tagged as &(dyn Any + Send + Sync))
2179 })),
2180 AnyKeyPath::FailableWritable(_) => {
2181 panic!("Tagged does not support writable keypaths (Tagged only implements Deref, not DerefMut)")
2182 }
2183 AnyKeyPath::ReadableEnum { extract, embed } => AnyKeyPath::ReadableEnum {
2184 extract: Arc::new(move |root| {
2185 let tagged = root.downcast_ref::<Tagged<Root, Tag>>().unwrap();
2186 extract(&*tagged as &(dyn Any + Send + Sync))
2187 }),
2188 embed: Arc::new(move |value| {
2189 let inner = embed(value);
2190 Box::new(Tagged::<Root, Tag>::new(*inner.downcast::<Root>().unwrap())) as Box<dyn Any>
2191 }),
2192 },
2193 AnyKeyPath::WritableEnum { .. } => {
2194 panic!("Tagged does not support writable keypaths (Tagged only implements Deref, not DerefMut)")
2195 }
2196 AnyKeyPath::ReferenceWritable(_) => {
2197 panic!("Tagged does not support writable keypaths (Tagged only implements Deref, not DerefMut)")
2198 }
2199 AnyKeyPath::Owned(f) => AnyKeyPath::Owned(Arc::new(move |root| {
2200 let tagged = *root.downcast::<Tagged<Root, Tag>>().unwrap();
2201 f(Box::new((*tagged).clone()) as Box<dyn Any>)
2202 })),
2203 AnyKeyPath::FailableOwned(f) => AnyKeyPath::FailableOwned(Arc::new(move |root| {
2204 let tagged = *root.downcast::<Tagged<Root, Tag>>().unwrap();
2205 f(Box::new((*tagged).clone()) as Box<dyn Any>)
2206 })),
2207 AnyKeyPath::FailableCombined { readable, writable, owned } => AnyKeyPath::FailableCombined {
2208 readable: Arc::new(move |root| {
2209 let tagged = root.downcast_ref::<Tagged<Root, Tag>>().unwrap();
2210 readable(&*tagged as &(dyn Any + Send + Sync))
2211 }),
2212 writable: Arc::new(move |_root| {
2213 panic!("Tagged does not support writable keypaths (Tagged only implements Deref, not DerefMut)")
2214 }),
2215 owned: Arc::new(move |root| {
2216 let tagged = *root.downcast::<Tagged<Root, Tag>>().unwrap();
2217 owned(Box::new((*tagged).clone()) as Box<dyn Any>)
2218 }),
2219 },
2220 }
2221 }
2222}
2223
2224impl<Root, Value> WithContainer<Root, Value> for KeyPaths<Root, Value> {
2226 #[inline]
2229 fn with_arc<F, R>(self, arc: &Arc<Root>, f: F) -> R
2230 where
2231 F: FnOnce(&Value) -> R,
2232 {
2233 match self {
2234 KeyPaths::Readable(get) => f(get(&**arc)),
2235 KeyPaths::FailableReadable(get) => {
2236 if let Some(value) = get(&**arc) {
2237 f(value)
2238 } else {
2239 panic!("FailableReadable keypath returned None for Arc")
2240 }
2241 }
2242 _ => panic!("with_arc only works with readable keypaths"),
2243 }
2244 }
2245
2246 #[inline]
2249 fn with_box<F, R>(self, boxed: &Box<Root>, f: F) -> R
2250 where
2251 F: FnOnce(&Value) -> R,
2252 {
2253 match self {
2254 KeyPaths::Readable(get) => f(get(&**boxed)),
2255 KeyPaths::FailableReadable(get) => {
2256 if let Some(value) = get(&**boxed) {
2257 f(value)
2258 } else {
2259 panic!("FailableReadable keypath returned None for Box")
2260 }
2261 }
2262 _ => panic!("with_box only works with readable keypaths"),
2263 }
2264 }
2265
2266 #[inline]
2269 fn with_box_mut<F, R>(self, boxed: &mut Box<Root>, f: F) -> R
2270 where
2271 F: FnOnce(&mut Value) -> R,
2272 {
2273 match self {
2274 KeyPaths::Writable(get) => f(get(&mut **boxed)),
2275 KeyPaths::FailableWritable(get) => {
2276 if let Some(value) = get(&mut **boxed) {
2277 f(value)
2278 } else {
2279 panic!("FailableWritable keypath returned None for Box")
2280 }
2281 }
2282 _ => panic!("with_box_mut only works with writable keypaths"),
2283 }
2284 }
2285
2286 #[inline]
2289 fn with_rc<F, R>(self, rc: &Rc<Root>, f: F) -> R
2290 where
2291 F: FnOnce(&Value) -> R,
2292 {
2293 match self {
2294 KeyPaths::Readable(get) => f(get(&**rc)),
2295 KeyPaths::FailableReadable(get) => {
2296 if let Some(value) = get(&**rc) {
2297 f(value)
2298 } else {
2299 panic!("FailableReadable keypath returned None for Rc")
2300 }
2301 }
2302 _ => panic!("with_rc only works with readable keypaths"),
2303 }
2304 }
2305
2306 #[inline]
2309 fn with_result<F, R, E>(self, result: &Result<Root, E>, f: F) -> Option<R>
2310 where
2311 F: FnOnce(&Value) -> R,
2312 {
2313 match self {
2314 KeyPaths::Readable(get) => {
2315 result.as_ref().ok().map(|root| f(get(root)))
2316 }
2317 KeyPaths::FailableReadable(get) => {
2318 result.as_ref().ok().and_then(|root| get(root).map(|v| f(v)))
2319 }
2320 _ => panic!("with_result only works with readable keypaths"),
2321 }
2322 }
2323
2324 #[inline]
2327 fn with_result_mut<F, R, E>(self, result: &mut Result<Root, E>, f: F) -> Option<R>
2328 where
2329 F: FnOnce(&mut Value) -> R,
2330 {
2331 match self {
2332 KeyPaths::Writable(get) => {
2333 result.as_mut().ok().map(|root| f(get(root)))
2334 }
2335 KeyPaths::FailableWritable(get) => {
2336 result.as_mut().ok().and_then(|root| get(root).map(|v| f(v)))
2337 }
2338 _ => panic!("with_result_mut only works with writable keypaths"),
2339 }
2340 }
2341
2342 #[inline]
2345 fn with_option<F, R>(self, option: &Option<Root>, f: F) -> Option<R>
2346 where
2347 F: FnOnce(&Value) -> R,
2348 {
2349 match self {
2350 KeyPaths::Readable(get) => {
2351 option.as_ref().map(|root| f(get(root)))
2352 }
2353 KeyPaths::FailableReadable(get) => {
2354 option.as_ref().and_then(|root| get(root).map(|v| f(v)))
2355 }
2356 _ => panic!("with_option only works with readable keypaths"),
2357 }
2358 }
2359
2360 #[inline]
2363 fn with_option_mut<F, R>(self, option: &mut Option<Root>, f: F) -> Option<R>
2364 where
2365 F: FnOnce(&mut Value) -> R,
2366 {
2367 match self {
2368 KeyPaths::Writable(get) => {
2369 option.as_mut().map(|root| f(get(root)))
2370 }
2371 KeyPaths::FailableWritable(get) => {
2372 option.as_mut().and_then(|root| get(root).map(|v| f(v)))
2373 }
2374 _ => panic!("with_option_mut only works with writable keypaths"),
2375 }
2376 }
2377
2378 #[inline]
2381 fn with_refcell<F, R>(self, refcell: &RefCell<Root>, f: F) -> Option<R>
2382 where
2383 F: FnOnce(&Value) -> R,
2384 {
2385 match self {
2386 KeyPaths::Readable(get) => {
2387 refcell.try_borrow().ok().map(|borrow| f(get(&*borrow)))
2388 }
2389 KeyPaths::FailableReadable(get) => {
2390 refcell.try_borrow().ok().and_then(|borrow| get(&*borrow).map(|v| f(v)))
2391 }
2392 _ => panic!("with_refcell only works with readable keypaths"),
2393 }
2394 }
2395
2396 #[inline]
2399 fn with_refcell_mut<F, R>(self, refcell: &RefCell<Root>, f: F) -> Option<R>
2400 where
2401 F: FnOnce(&mut Value) -> R,
2402 {
2403 match self {
2404 KeyPaths::Writable(get) => {
2405 refcell.try_borrow_mut().ok().map(|mut borrow| f(get(&mut *borrow)))
2406 }
2407 KeyPaths::FailableWritable(get) => {
2408 refcell.try_borrow_mut().ok().and_then(|mut borrow| get(&mut *borrow).map(|v| f(v)))
2409 }
2410 _ => panic!("with_refcell_mut only works with writable keypaths"),
2411 }
2412 }
2413
2414 #[cfg(feature = "tagged_core")]
2417 #[inline]
2418 fn with_tagged<F, R, Tag>(self, tagged: &Tagged<Root, Tag>, f: F) -> R
2419 where
2420 F: FnOnce(&Value) -> R,
2421 {
2422 match self {
2423 KeyPaths::Readable(get) => f(get(&**tagged)),
2424 KeyPaths::FailableReadable(get) => {
2425 get(&**tagged).map_or_else(|| panic!("Tagged value is None"), f)
2426 }
2427 KeyPaths::ReadableEnum { extract, .. } => {
2428 extract(&**tagged).map_or_else(|| panic!("Tagged value is None"), f)
2429 }
2430 _ => panic!("with_tagged only works with readable keypaths"),
2431 }
2432 }
2433
2434 #[inline]
2437 fn with_mutex<F, R>(self, mutex: &Mutex<Root>, f: F) -> Option<R>
2438 where
2439 F: FnOnce(&Value) -> R,
2440 {
2441 match self {
2442 KeyPaths::Readable(get) => {
2443 mutex.try_lock().ok().map(|guard| f(get(&*guard)))
2444 }
2445 KeyPaths::FailableReadable(get) => {
2446 mutex.try_lock().ok().and_then(|guard| get(&*guard).map(|v| f(v)))
2447 }
2448 _ => panic!("with_mutex only works with readable keypaths"),
2449 }
2450 }
2451
2452 #[inline]
2455 fn with_mutex_mut<F, R>(self, mutex: &mut Mutex<Root>, f: F) -> Option<R>
2456 where
2457 F: FnOnce(&mut Value) -> R,
2458 {
2459 match self {
2460 KeyPaths::Writable(get) => {
2461 mutex.try_lock().ok().map(|mut guard| f(get(&mut *guard)))
2462 }
2463 KeyPaths::FailableWritable(get) => {
2464 mutex.try_lock().ok().and_then(|mut guard| get(&mut *guard).map(|v| f(v)))
2465 }
2466 _ => panic!("with_mutex_mut only works with writable keypaths"),
2467 }
2468 }
2469
2470 #[inline]
2473 fn with_rwlock<F, R>(self, rwlock: &RwLock<Root>, f: F) -> Option<R>
2474 where
2475 F: FnOnce(&Value) -> R,
2476 {
2477 match self {
2478 KeyPaths::Readable(get) => {
2479 rwlock.try_read().ok().map(|guard| f(get(&*guard)))
2480 }
2481 KeyPaths::FailableReadable(get) => {
2482 rwlock.try_read().ok().and_then(|guard| get(&*guard).map(|v| f(v)))
2483 }
2484 _ => panic!("with_rwlock only works with readable keypaths"),
2485 }
2486 }
2487
2488 #[inline]
2491 fn with_rwlock_mut<F, R>(self, rwlock: &mut RwLock<Root>, f: F) -> Option<R>
2492 where
2493 F: FnOnce(&mut Value) -> R,
2494 {
2495 match self {
2496 KeyPaths::Writable(get) => {
2497 rwlock.try_write().ok().map(|mut guard| f(get(&mut *guard)))
2498 }
2499 KeyPaths::FailableWritable(get) => {
2500 rwlock.try_write().ok().and_then(|mut guard| get(&mut *guard).map(|v| f(v)))
2501 }
2502 _ => panic!("with_rwlock_mut only works with writable keypaths"),
2503 }
2504 }
2505
2506 fn with_arc_rwlock<F, R>(self, arc_rwlock: &Arc<RwLock<Root>>, f: F) -> Option<R>
2509 where
2510 F: FnOnce(&Value) -> R,
2511 {
2512 match self {
2513 KeyPaths::Readable(get) => {
2514 arc_rwlock.try_read().ok().map(|guard| f(get(&*guard)))
2515 }
2516 KeyPaths::FailableReadable(get) => {
2517 arc_rwlock.try_read().ok().and_then(|guard| get(&*guard).map(|v| f(v)))
2518 }
2519 _ => panic!("with_arc_rwlock only works with readable keypaths"),
2520 }
2521 }
2522
2523 fn with_arc_rwlock_mut<F, R>(self, arc_rwlock: &Arc<RwLock<Root>>, f: F) -> Option<R>
2526 where
2527 F: FnOnce(&mut Value) -> R,
2528 {
2529 match self {
2530 KeyPaths::Writable(get) => {
2531 arc_rwlock.try_write().ok().map(|mut guard| f(get(&mut *guard)))
2532 }
2533 KeyPaths::FailableWritable(get) => {
2534 arc_rwlock.try_write().ok().and_then(|mut guard| get(&mut *guard).map(|v| f(v)))
2535 }
2536 _ => panic!("with_arc_rwlock_mut only works with writable keypaths"),
2537 }
2538 }
2539}
2540
2541impl<Root, Mid> KeyPaths<Root, Mid>
2542where
2543 Root: 'static,
2544 Mid: 'static,
2545{
2546 #[inline]
2548 pub fn then<Value>(self, mid: KeyPaths<Mid, Value>) -> KeyPaths<Root, Value>
2549 where
2550 Value: 'static,
2551 {
2552 self.compose(mid)
2553 }
2554
2555 pub fn compose<Value>(self, mid: KeyPaths<Mid, Value>) -> KeyPaths<Root, Value>
2556 where
2557 Value: 'static,
2558 {
2559 use KeyPaths::*;
2560
2561 match (self, mid) {
2562 (Readable(f1), Readable(f2)) => Readable(Arc::new(move |r| f2(f1(r)))),
2563
2564 (Writable(f1), Writable(f2)) => Writable(Arc::new(move |r| f2(f1(r)))),
2565
2566 (FailableReadable(f1), Readable(f2)) => {
2567 FailableReadable(Arc::new(move |r| f1(r).map(|m| f2(m))))
2568 }
2569
2570 (Readable(f1), FailableReadable(f2)) => FailableReadable(Arc::new(move |r| f2(f1(r)))),
2571
2572 (FailableReadable(f1), FailableReadable(f2)) => {
2573 FailableReadable(Arc::new(move |r| f1(r).and_then(|m| f2(m))))
2574 }
2575
2576 (FailableWritable(f1), Writable(f2)) => {
2577 FailableWritable(Arc::new(move |r| f1(r).map(|m| f2(m))))
2578 }
2579
2580 (Writable(f1), FailableWritable(f2)) => FailableWritable(Arc::new(move |r| f2(f1(r)))),
2581
2582 (FailableWritable(f1), FailableWritable(f2)) => {
2583 FailableWritable(Arc::new(move |r| f1(r).and_then(|m| f2(m))))
2584 }
2585 (FailableReadable(f1), ReadableEnum { extract, .. }) => {
2586 FailableReadable(Arc::new(move |r| f1(r).and_then(|m| extract(m))))
2587 }
2588 (ReadableEnum { extract, .. }, Readable(f2)) => {
2592 FailableReadable(Arc::new(move |r| extract(r).map(|m| f2(m))))
2593 }
2594
2595 (ReadableEnum { extract, .. }, FailableReadable(f2)) => {
2596 FailableReadable(Arc::new(move |r| extract(r).and_then(|m| f2(m))))
2597 }
2598
2599 (WritableEnum { extract, .. }, Readable(f2)) => {
2600 FailableReadable(Arc::new(move |r| extract(r).map(|m| f2(m))))
2601 }
2602
2603 (WritableEnum { extract, .. }, FailableReadable(f2)) => {
2604 FailableReadable(Arc::new(move |r| extract(r).and_then(|m| f2(m))))
2605 }
2606
2607 (WritableEnum { extract_mut, .. }, Writable(f2)) => {
2608 FailableWritable(Arc::new(move |r| extract_mut(r).map(|m| f2(m))))
2609 }
2610
2611 (
2612 FailableWritable(f_root_mid),
2613 WritableEnum {
2614 extract_mut: exm_mid_val,
2615 ..
2616 },
2617 ) => {
2618 FailableWritable(Arc::new(move |r: &mut Root| {
2619 let intermediate_mid_ref = f_root_mid(r);
2622
2623 intermediate_mid_ref.and_then(|intermediate_mid| exm_mid_val(intermediate_mid))
2626 }))
2627 }
2628
2629 (WritableEnum { extract_mut, .. }, FailableWritable(f2)) => {
2630 FailableWritable(Arc::new(move |r| extract_mut(r).and_then(|m| f2(m))))
2631 }
2632
2633 (Writable(f1), WritableEnum { extract_mut, .. }) => {
2635 FailableWritable(Arc::new(move |r: &mut Root| {
2636 let mid: &mut Mid = f1(r);
2637 extract_mut(mid)
2638 }))
2639 }
2640
2641 (
2642 ReadableEnum {
2643 extract: ex1,
2644 embed: em1,
2645 },
2646 ReadableEnum {
2647 extract: ex2,
2648 embed: em2,
2649 },
2650 ) => ReadableEnum {
2651 extract: Arc::new(move |r| ex1(r).and_then(|m| ex2(m))),
2652 embed: Arc::new(move |v| em1(em2(v))),
2653 },
2654
2655 (
2656 WritableEnum {
2657 extract: ex1,
2658 extract_mut: _,
2659 embed: em1,
2660 },
2661 ReadableEnum {
2662 extract: ex2,
2663 embed: em2,
2664 },
2665 ) => ReadableEnum {
2666 extract: Arc::new(move |r| ex1(r).and_then(|m| ex2(m))),
2667 embed: Arc::new(move |v| em1(em2(v))),
2668 },
2669
2670 (
2671 WritableEnum {
2672 extract: ex1,
2673 extract_mut: exm1,
2674 embed: em1,
2675 },
2676 WritableEnum {
2677 extract: ex2,
2678 extract_mut: exm2,
2679 embed: em2,
2680 },
2681 ) => WritableEnum {
2682 extract: Arc::new(move |r| ex1(r).and_then(|m| ex2(m))),
2683 extract_mut: Arc::new(move |r| exm1(r).and_then(|m| exm2(m))),
2684 embed: Arc::new(move |v| em1(em2(v))),
2685 },
2686
2687
2688 (Owned(f1), Owned(f2)) => {
2690 Owned(Arc::new(move |r| f2(f1(r))))
2691 }
2692 (FailableOwned(f1), Owned(f2)) => {
2693 FailableOwned(Arc::new(move |r| f1(r).map(|m| f2(m))))
2694 }
2695 (Owned(f1), FailableOwned(f2)) => {
2696 FailableOwned(Arc::new(move |r| f2(f1(r))))
2697 }
2698 (FailableOwned(f1), FailableOwned(f2)) => {
2699 FailableOwned(Arc::new(move |r| f1(r).and_then(|m| f2(m))))
2700 }
2701
2702 (a, b) => panic!(
2707 "Unsupported composition: {:?} then {:?}",
2708 kind_name(&a),
2709 kind_name(&b)
2710 ),
2711 }
2712 }
2713
2714 #[inline]
2716 pub fn kind_name(&self) -> &'static str {
2717 kind_name(self)
2718 }
2719}
2720
2721fn kind_name<Root, Value>(k: &KeyPaths<Root, Value>) -> &'static str {
2722 use KeyPaths::*;
2723 match k {
2724 Readable(_) => "Readable",
2725 Writable(_) => "Writable",
2726 FailableReadable(_) => "FailableReadable",
2727 FailableWritable(_) => "FailableWritable",
2728 ReadableEnum { .. } => "ReadableEnum",
2729 WritableEnum { .. } => "WritableEnum",
2730 ReferenceWritable(_) => "ReferenceWritable",
2731 Owned(_) => "Owned",
2733 FailableOwned(_) => "FailableOwned",
2734 FailableCombined { .. } => "FailableCombined",
2735 }
2736}
2737
2738pub fn compose<Root, Mid, Value>(
2747 kp1: KeyPaths<Root, Mid>,
2748 kp2: KeyPaths<Mid, Value>,
2749) -> KeyPaths<Root, Value>
2750where
2751 Root: 'static,
2752 Mid: 'static,
2753 Value: 'static,
2754{
2755 kp1.compose(kp2)
2756}
2757
2758#[macro_export]
2761macro_rules! readable_enum_macro {
2762 ($enum:path, $variant:ident) => {{
2764 $crate::KeyPaths::readable_enum(
2765 |_| <$enum>::$variant,
2766 |e: &$enum| match e {
2767 <$enum>::$variant => Some(&()),
2768 _ => None,
2769 },
2770 )
2771 }};
2772 ($enum:path, $variant:ident($inner:ty)) => {{
2774 $crate::KeyPaths::readable_enum(
2775 |v: $inner| <$enum>::$variant(v),
2776 |e: &$enum| match e {
2777 <$enum>::$variant(v) => Some(v),
2778 _ => None,
2779 },
2780 )
2781 }};
2782}
2783
2784#[macro_export]
2785macro_rules! writable_enum_macro {
2786 ($enum:path, $variant:ident) => {{
2788 $crate::KeyPaths::writable_enum(
2789 |_| <$enum>::$variant,
2790 |e: &$enum| match e {
2791 <$enum>::$variant => Some(&()),
2792 _ => None,
2793 },
2794 |e: &mut $enum| match e {
2795 <$enum>::$variant => Some(&mut ()),
2796 _ => None,
2797 },
2798 )
2799 }};
2800 ($enum:path, $variant:ident($inner:ty)) => {{
2802 $crate::KeyPaths::writable_enum(
2803 |v: $inner| <$enum>::$variant(v),
2804 |e: &$enum| match e {
2805 <$enum>::$variant(v) => Some(v),
2806 _ => None,
2807 },
2808 |e: &mut $enum| match e {
2809 <$enum>::$variant(v) => Some(v),
2810 _ => None,
2811 },
2812 )
2813 }};
2814}