field_access 0.1.12

Dynamically access struct fields
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
#![doc = include_str!("../README.md")]
#![warn(missing_docs)]
#![warn(clippy::pedantic)]
#![allow(clippy::must_use_candidate)]
#![no_std]

#[cfg(feature = "alloc")]
extern crate alloc;

#[macro_use]
mod macros;

#[cfg(feature = "alloc")]
use alloc::string::String;
#[cfg(feature = "alloc")]
use alloc::vec::Vec;
use core::any::{Any, TypeId};
use core::fmt;
use core::iter::FusedIterator;
use core::mem;
use core::ops;
use core::slice;
use paste::paste;

/// Derive macro for automatically implementing [`AnyFieldAccess`] on structs.
#[cfg(feature = "derive")]
pub use field_access_derive::FieldAccess;

/// Low-level struct field access.
///
/// In most cases it is more convenient to use the methods of the [`FieldAccess`] trait which has a
/// blanket implementation for any type implementing `AnyFieldAccess`.
///
/// Consider automatically implementing it via `#[derive(FieldAccess)]` for structs where you need
/// dynamic field access.
pub trait AnyFieldAccess: Any {
    /// Provides an immutable reference to a struct field.
    ///
    /// Returns `Some(_)` if the field is accessible, otherwise `None`.
    ///
    /// # Example
    ///
    /// ```
    /// use field_access::{AnyFieldAccess, FieldAccess};
    ///
    /// #[derive(FieldAccess)]
    /// struct Foo {
    ///     a: u8
    /// }
    ///
    /// let foo = Foo { a: 1 };
    /// let field = foo.field_as_any("a");
    ///
    /// assert!(field.is_some());
    /// assert_eq!(field.unwrap().downcast_ref::<u8>(), Some(&1));
    /// ```
    fn field_as_any(&self, field: &str) -> Option<&dyn Any>;

    /// Provides a mutable reference to a struct field.
    ///
    /// Returns `Some(_)` if the field is accessible, otherwise `None`.
    ///
    /// # Example
    ///
    /// ```
    /// use field_access::{AnyFieldAccess, FieldAccess};
    ///
    /// #[derive(FieldAccess)]
    /// struct Foo {
    ///     a: u8
    /// }
    ///
    /// let mut foo = Foo { a: 1 };
    ///
    /// if let Some(field) = foo.field_as_any_mut("a") {
    ///     if let Some(value) = field.downcast_mut::<u8>() {
    ///         *value = 2;
    ///     }
    /// }
    ///
    /// assert_eq!(foo.a, 2);
    /// ```
    fn field_as_any_mut(&mut self, field: &str) -> Option<&mut dyn Any>;

    /// Provides the names of all accessible fields.
    ///
    /// The field name order is undefined and should not be relied upon.
    ///
    /// # Example
    ///
    /// ```
    /// use field_access::{AnyFieldAccess, FieldAccess};
    ///
    /// #[derive(FieldAccess, Default)]
    /// struct Foo {
    ///     a: u8,
    ///     b: bool,
    /// }
    ///
    /// let foo = Foo::default();
    ///
    /// assert_eq!(foo.field_names(), &["a", "b"]);
    /// ```
    fn field_names(&self) -> &'static [&'static str];
}

/// High-level struct field access.
///
/// This trait is automatically implemented by any type implementing
/// [`AnyFieldAccess`]. See its documentation for more details.
pub trait FieldAccess: AnyFieldAccess {
    /// Immutable field access.
    ///
    /// Returns `Some(_)` if the field is accessible, otherwise `None`.
    ///
    /// The returned [`Field`] provides methods to immutably interact with the field. See its
    /// documentation for more.
    ///
    /// # Example
    ///
    /// ```
    /// use field_access::FieldAccess;
    ///
    /// #[derive(FieldAccess)]
    /// struct Foo {
    ///     a: u8
    /// }
    ///
    /// let foo = Foo { a: 1 };
    ///
    /// assert!(foo.field("a").is_some());
    /// assert!(foo.field("b").is_none());
    /// ```
    #[inline]
    fn field(&self, field: &str) -> Option<Field<'_>> {
        self.field_as_any(field).map(Field::new)
    }

    /// Mutable field access.
    ///
    /// Returns `Some(_)` if the field is accessible, otherwise `None`.
    ///
    /// The returned [`FieldMut`] provides methods to mutably interact with the field. See its
    /// documentation for more.
    ///
    /// # Example
    ///
    /// ```
    /// use field_access::FieldAccess;
    ///
    /// #[derive(FieldAccess)]
    /// struct Foo {
    ///     a: u8
    /// }
    ///
    /// let mut foo = Foo { a: 1 };
    ///
    /// assert!(foo.field_mut("a").is_some());
    /// assert!(foo.field_mut("b").is_none());
    /// ```
    #[inline]
    fn field_mut(&mut self, field: &str) -> Option<FieldMut<'_>> {
        self.field_as_any_mut(field).map(FieldMut::new)
    }

    /// Returns an iterator over all struct fields.
    ///
    /// The order of the items yielded by the iterator is undefined and should not be relied upon.
    ///
    /// # Example
    ///
    /// ```
    /// use field_access::FieldAccess;
    ///
    /// #[derive(FieldAccess)]
    /// struct Foo {
    ///     a: u8,
    ///     b: u16,
    ///     c: u32
    /// }
    ///
    /// let foo = Foo { a: 1, b: 2, c: 3 };
    /// let tuples: Vec<_> = foo.fields()
    ///                         .map(|(name, field)| (name, field.as_u8()))
    ///                         .collect();
    /// assert_eq!(tuples, &[("a", Some(1)),
    ///                      ("b", Some(2)),
    ///                      ("c", Some(3))])
    /// ```
    #[inline]
    fn fields(&self) -> Fields<'_>
    where
        Self: Sized,
    {
        Fields::new(self)
    }
}

impl<T> FieldAccess for T where T: AnyFieldAccess {}

/// An immutable struct field reference.
///
/// A `FieldRef` is a proxy for immutable operations on a struct's field.
///
/// Values of this type are created by [`FieldAccess::field`].
#[derive(Debug, Clone)]
pub struct Field<'a> {
    value: &'a dyn Any,
}

impl<'a> Field<'a> {
    fn new(value: &'a dyn Any) -> Self {
        Field { value }
    }

    /// Returns `true` if the field is of type `T`.
    ///
    /// # Example
    ///
    /// ```
    /// use field_access::FieldAccess;
    ///
    /// #[derive(FieldAccess)]
    /// struct Foo {
    ///     a: u8
    /// }
    ///
    /// let foo = Foo { a: 1 };
    /// let field = foo.field("a").unwrap();
    ///
    /// assert!(field.is::<u8>());
    /// assert!(!field.is::<&str>());
    /// ```
    #[inline]
    pub fn is<T: Any>(&self) -> bool {
        self.value.is::<T>()
    }

    /// Gets the `TypeId` of the field's value.
    ///
    /// # Example
    ///
    /// ```
    /// use core::any::TypeId;
    /// use field_access::FieldAccess;
    ///
    /// #[derive(FieldAccess)]
    /// struct Foo {
    ///     a: u8
    /// }
    ///
    /// let foo = Foo { a: 1 };
    /// let field = foo.field("a").unwrap();
    ///
    /// assert_eq!(field.type_id(), TypeId::of::<u8>());
    /// ```
    #[inline]
    pub fn type_id(&self) -> TypeId {
        self.value.type_id()
    }

    /// Obtains an immutable reference to the value of type `T`.
    ///
    /// Returns `Some(_)` if field's value is of type `T`, `None` otherwise.
    ///
    /// # Example
    ///
    /// ```
    /// use field_access::FieldAccess;
    ///
    /// #[derive(FieldAccess)]
    /// struct Foo {
    ///     a: u8
    /// }
    ///
    /// let foo = Foo { a: 42 };
    /// let field = foo.field("a").unwrap();
    ///
    /// assert_eq!(field.get::<u8>(), Some(&42u8));
    /// assert_eq!(field.get::<&str>(), None);
    /// ```
    #[inline]
    pub fn get<T: Any>(&self) -> Option<&T> {
        self.value.downcast_ref::<T>()
    }

    /// Obtains an immutable reference to the value as `&dyn Any`.
    ///
    /// # Example
    ///
    /// ```
    /// use field_access::FieldAccess;
    ///
    /// #[derive(FieldAccess)]
    /// struct Foo {
    ///     a: u8
    /// }
    ///
    /// let foo = Foo { a: 42 };
    /// let field = foo.field("a").unwrap();
    /// let any = field.as_any();
    ///
    /// assert_eq!(any.downcast_ref::<u8>(), Some(&42u8));
    /// ```
    #[inline]
    pub fn as_any(&self) -> &dyn Any {
        self.value
    }

    /// Returns `true` if the field value is of type `&[T]`.
    ///
    /// # Example
    ///
    /// ```
    /// use field_access::FieldAccess;
    ///
    /// #[derive(FieldAccess)]
    /// struct Foo {
    ///     a: &'static [u8]
    /// }
    ///
    /// let foo = Foo { a: &[1, 2, 3] };
    /// let field = foo.field("a").unwrap();
    ///
    /// assert!(field.is_slice::<u8>());
    /// ```
    #[inline]
    pub fn is_slice<T: Any>(&self) -> bool {
        self.is::<&[T]>()
    }

    /// Obtain an immutable reference to the value as `&[T]`.
    ///
    /// Returns `Some(_)` if [`.is_slice::<T>()`](Self::is_slice) or
    /// [`.is_vec::<T>()`][Self::is_vec] would return `true`, `None` otherwise.
    ///
    /// # Example
    ///
    /// ```
    /// use field_access::FieldAccess;
    ///
    /// #[derive(FieldAccess)]
    /// struct Foo {
    ///     a: Vec<u8>
    /// }
    ///
    /// let foo = Foo { a: vec![1, 2, 3] };
    /// let field = foo.field("a").unwrap();
    ///
    /// assert_eq!(field.as_slice(), Some(&[1u8, 2, 3][..]));
    /// ```
    #[cfg(feature = "alloc")]
    pub fn as_slice<T: Any>(&self) -> Option<&[T]> {
        get_downcast_ref!(
            self.value,
            &[T] => |&v| Some(v),
            Vec<T> => |v| Some(v.as_slice())
        )
    }

    /// Obtain an immutable reference to the value as `&[T]`.
    ///
    /// Returns `Some(_)` if field's value is of type `&[T]`, `None` otherwise.
    ///
    /// # Example
    ///
    /// ```
    /// use field_access::FieldAccess;
    ///
    /// #[derive(FieldAccess)]
    /// struct Foo {
    ///     a: &'static [u8]
    /// }
    ///
    /// let foo = Foo { a: &[1, 2, 3] };
    /// let field = foo.field("a").unwrap();
    ///
    /// assert_eq!(field.as_slice(), Some(&[1u8, 2, 3][..]));
    /// ```
    #[cfg(not(feature = "alloc"))]
    #[inline]
    pub fn as_slice<T: Any>(&self) -> Option<&[T]> {
        self.get().copied()
    }

    /// Returns `true` if the field value is of type `Vec<T>`.
    ///
    /// # Example
    ///
    /// ```
    /// use field_access::FieldAccess;
    ///
    /// #[derive(FieldAccess)]
    /// struct Foo {
    ///     a: Vec<u8>
    /// }
    ///
    /// let foo = Foo { a: vec![1, 2, 3] };
    /// let field = foo.field("a").unwrap();
    ///
    /// assert!(field.is_vec::<u8>());
    /// assert!(!field.is_vec::<u16>());
    /// ```
    #[cfg(feature = "alloc")]
    #[inline]
    pub fn is_vec<T: Any>(&self) -> bool {
        self.is::<Vec<T>>()
    }

    /// Returns `true` if the field value is of type `String`.
    ///
    /// # Example
    ///
    /// ```
    /// use field_access::FieldAccess;
    ///
    /// #[derive(FieldAccess)]
    /// struct Foo {
    ///     a: String,
    /// }
    ///
    /// let foo = Foo { a: String::from("bar") };
    /// let field = foo.field("a").unwrap();
    ///
    /// assert!(field.is_string());
    /// assert!(!field.is_str());
    /// ```
    #[cfg(feature = "alloc")]
    #[inline]
    pub fn is_string(&self) -> bool {
        self.is::<String>()
    }

    /// Returns `true` if the field value is of type `&str`.
    ///
    /// # Example
    ///
    /// ```
    /// use field_access::FieldAccess;
    ///
    /// #[derive(FieldAccess)]
    /// struct Foo {
    ///     a: &'static str,
    /// }
    ///
    /// let foo = Foo { a: "bar" };
    /// let field = foo.field("a").unwrap();
    ///
    /// assert!(field.is_str());
    /// ```
    #[inline]
    pub fn is_str(&self) -> bool {
        self.is::<&str>()
    }

    /// Obtain an immutable reference to the value as `&str`.
    ///
    /// Returns `Some(_)` if [`.is_str()`](Self::is_str) or [`.is_string()`][Self::is_string] would
    /// return `true`, `None` otherwise.
    ///
    /// # Example
    ///
    /// ```
    /// use field_access::FieldAccess;
    ///
    /// #[derive(FieldAccess)]
    /// struct Foo {
    ///     a: String
    /// }
    ///
    /// let foo = Foo { a: String::from("bar") };
    /// let field = foo.field("a").unwrap();
    ///
    /// assert_eq!(field.as_str(), Some("bar"));
    /// ```
    #[cfg(feature = "alloc")]
    pub fn as_str(&self) -> Option<&str> {
        get_downcast_ref!(
            self.value,
            &str => |&v| Some(v),
            String => |v| Some(v.as_str())
        )
    }

    /// Obtain an immutable reference to the value as `&str`.
    ///
    /// Returns `Some(_)` if field's value is of type `&str`, `None` otherwise.
    ///
    /// # Example
    ///
    /// ```
    /// use field_access::FieldAccess;
    ///
    /// #[derive(FieldAccess)]
    /// struct Foo {
    ///     a: &'static str
    /// }
    ///
    /// let foo = Foo { a: "bar" };
    /// let field = foo.field("a").unwrap();
    ///
    /// assert_eq!(field.as_str(), Some("bar"));
    /// ```
    #[cfg(not(feature = "alloc"))]
    #[inline]
    pub fn as_str(&self) -> Option<&str> {
        self.get().copied()
    }

    is_type_method!(bool);
    is_type_method!(u8, u16, u32, u64, u128, usize);
    is_type_method!(i8, i16, i32, i64, i128, isize);
    is_type_method!(f32, f64);

    as_type_method!(bool);
    as_type_method! {
        u8 { u16 | u32 | u64 | u128 | usize => |&v| v.try_into().ok() },
        u16 {
            u8 => |&v| Some(v.into()),
            u32 | u64 | u128 | usize => |&v| v.try_into().ok(),
        },
        u32 {
            u16 | u8 => |&v| Some(v.into()),
            u64 | u128 | usize => |&v| v.try_into().ok(),
        },
        u64 {
            u32 | u16 | u8 => |&v| Some(v.into()),
            u128 | usize => |&v| v.try_into().ok(),
        },
        u128 {
            u8 | u16 | u32 | u64 => |&v| Some(v.into()),
            usize => |&v| v.try_into().ok(),
        },
        usize {
            u16 | u8 => |&v| Some(v.into()),
            u32 | u64 | u128 => |&v| v.try_into().ok(),
        },
    }
    as_type_method! {
        i8 { i16 | i32 | i64 | i128 | isize => |&v| v.try_into().ok() },
        i16 {
            i8 => |&v| Some(v.into()),
            i32 | i64 | i128 | isize => |&v| v.try_into().ok(),
        },
        i32 {
            i16 | i8 => |&v| Some(v.into()),
            i64 | i128 | isize => |&v| v.try_into().ok(),
        },
        i64 {
            i32 | i16 | i8 => |&v| Some(v.into()),
            i128 | isize => |&v| v.try_into().ok(),
        },
        i128 {
            i8 | i16 | i32 | i64 => |&v| Some(v.into()),
            isize => |&v| v.try_into().ok(),
        },
        isize {
            i16 | i8 => |&v| Some(v.into()),
            i32 | i64 | i128 => |&v| v.try_into().ok(),
        },
    }
    as_type_method! {
        f32,
        f64 { f32 => |&v| Some(v.into()) }
    }
}

/// A mutable struct field reference.
///
/// A `FieldMut` is a proxy for mutable operations on a struct's field.
///
/// Values of this type are created by [`FieldAccess::field_mut`].
#[derive(Debug)]
pub struct FieldMut<'a> {
    value: &'a mut dyn Any,
}

impl<'a> FieldMut<'a> {
    fn new(value: &'a mut dyn Any) -> Self {
        FieldMut { value }
    }

    /// Obtains a mutable reference to the value of type `T`.
    ///
    /// Returns `Some(_)` if field's value is of type `T`, `None` otherwise.
    ///
    /// # Example
    ///
    /// ```
    /// use field_access::FieldAccess;
    ///
    /// #[derive(FieldAccess)]
    /// struct Foo {
    ///     a: u8
    /// }
    ///
    /// let mut foo = Foo { a: 1 };
    /// let mut field = foo.field_mut("a").unwrap();
    ///
    /// if let Some(field) = field.get_mut::<u8>() {
    ///     *field = 42;
    /// }
    ///
    /// assert_eq!(field.as_u8(), Some(42u8));
    /// assert_eq!(field.get_mut::<&str>(), None);
    /// ```
    #[inline]
    pub fn get_mut<T: Any>(&mut self) -> Option<&mut T> {
        self.value.downcast_mut::<T>()
    }

    /// Obtains a mutable reference to the value as `&mut dyn Any`.
    ///
    /// # Example
    ///
    /// ```
    /// use field_access::FieldAccess;
    ///
    /// #[derive(FieldAccess)]
    /// struct Foo {
    ///     a: u8
    /// }
    ///
    /// let mut foo = Foo { a: 42 };
    ///
    /// let mut field = foo.field_mut("a").unwrap();
    /// let any = field.as_any_mut();
    ///
    /// if let Some(value) = any.downcast_mut::<u8>() {
    ///     *value = 42;
    /// }
    ///
    /// assert_eq!(foo.a, 42);
    /// ```
    #[inline]
    pub fn as_any_mut(&mut self) -> &mut dyn Any {
        self.value
    }

    /// Sets the value of the field.
    ///
    /// Returns `true` if it was possible to replace the field's value with a value of type `T`,
    /// `false` otherwise.
    ///
    /// # Example
    ///
    /// ```
    /// use field_access::FieldAccess;
    ///
    /// #[derive(FieldAccess)]
    /// struct Foo {
    ///     a: u8
    /// }
    ///
    /// let mut foo = Foo { a: 1 };
    /// let mut field = foo.field_mut("a").unwrap();
    ///
    /// assert!(field.set(42u8));
    /// assert_eq!(foo.a, 42);
    /// ```
    #[inline]
    pub fn set<T: Any>(&mut self, value: T) -> bool {
        self.replace(value).is_some()
    }

    /// Replaces the value of the field, returning the previous value.
    ///
    /// Returns `Some(old_value)` if it was possible to replace the field's value with a value of
    /// type `T`, `None` otherwise.
    ///
    /// # Example
    ///
    /// ```
    /// use field_access::FieldAccess;
    ///
    /// #[derive(FieldAccess)]
    /// struct Foo {
    ///     a: u8
    /// }
    ///
    /// let mut foo = Foo { a: 1 };
    /// let mut field = foo.field_mut("a").unwrap();
    ///
    /// assert_eq!(field.replace(42u8), Some(1));
    /// assert_eq!(foo.a, 42);
    /// ```
    #[inline]
    pub fn replace<T: Any>(&mut self, value: T) -> Option<T> {
        self.get_mut().map(|dest| mem::replace(dest, value))
    }

    /// Swaps the value of the field and another mutable location.
    ///
    /// Returns `true` if it was possible to replace the field's value with a value of type `T`,
    /// `false` otherwise.
    ///
    /// # Example
    ///
    /// ```
    /// use field_access::FieldAccess;
    ///
    /// #[derive(FieldAccess)]
    /// struct Foo {
    ///     a: u8
    /// }
    ///
    /// let mut foo = Foo { a: 1 };
    /// let mut value = 2u8;
    /// let mut field = foo.field_mut("a").unwrap();
    ///
    /// assert!(field.swap(&mut value));
    /// assert_eq!(foo.a, 2);
    /// assert_eq!(value, 1);
    /// ```
    #[inline]
    pub fn swap<T: Any>(&mut self, value: &mut T) -> bool {
        self.get_mut().map(|dest| mem::swap(dest, value)).is_some()
    }

    /// Takes the value of the field, replacing it with its default value.
    ///
    /// Returns `Some(_)` if it was possible to replace the field's value with the default value of
    /// type `T`, `None` otherwise.
    ///
    /// # Example
    ///
    /// ```
    /// use field_access::FieldAccess;
    ///
    /// #[derive(FieldAccess)]
    /// struct Foo {
    ///     a: u8
    /// }
    ///
    /// let mut foo = Foo { a: 42 };
    /// let mut field = foo.field_mut("a").unwrap();
    ///
    /// assert_eq!(field.take(), Some(42u8));
    /// assert_eq!(foo.a, 0);
    /// ```
    #[inline]
    pub fn take<T: Any + Default>(&mut self) -> Option<T> {
        self.replace(T::default())
    }

    /// Returns a mutable reference to the value as `&mut Vec<T>`.
    ///
    /// Returns `Some(_)` if the field's value is of type `Vec<T>`, `None` otherwise.
    ///
    /// # Example
    ///
    /// ```
    /// use field_access::FieldAccess;
    ///
    /// #[derive(FieldAccess)]
    /// struct Foo {
    ///     a: Vec<u8>
    /// }
    ///
    /// let mut foo = Foo { a: vec![1, 2, 3] };
    /// let mut field = foo.field_mut("a").unwrap();
    ///
    /// if let Some(vec) = field.as_vec_mut::<u8>() {
    ///     vec.push(4);
    /// }
    ///
    /// assert_eq!(foo.a, vec![1, 2, 3, 4]);
    /// ```
    #[cfg(feature = "alloc")]
    #[inline]
    pub fn as_vec_mut<T: Any>(&mut self) -> Option<&mut Vec<T>> {
        self.get_mut::<Vec<T>>()
    }

    #[cfg(feature = "alloc")]
    as_type_mut_method!(String {
        example_value: String::from("bar")
    });

    as_type_mut_method!(bool {
        example_value: true
    });
    as_type_mut_method!(u8, u16, u32, u64, u128, usize);
    as_type_mut_method!(i8, i16, i32, i64, i128, isize);
    as_type_mut_method!(f32, f64);
}

impl<'a> AsRef<Field<'a>> for FieldMut<'a> {
    fn as_ref(&self) -> &Field<'a> {
        // SAFETY: `FieldMut` and `Field` share the same memory layout and we're holding an
        // immutable reference.
        unsafe { &*(self as *const FieldMut).cast::<Field>() }
    }
}

impl<'a> ops::Deref for FieldMut<'a> {
    type Target = Field<'a>;

    fn deref(&self) -> &Self::Target {
        self.as_ref()
    }
}

/// An immutable iterator over all fields of a struct.
///
/// Values of this type are created by [`FieldAccess::fields`].
#[derive(Clone)]
pub struct Fields<'a> {
    access: &'a dyn FieldAccess,
    field_names: slice::Iter<'a, &'static str>,
}

impl<'a> Fields<'a> {
    fn new(access: &'a dyn FieldAccess) -> Self {
        Fields {
            access,
            field_names: access.field_names().iter(),
        }
    }
}

impl<'a> fmt::Debug for Fields<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_list().entries(self.access.field_names()).finish()
    }
}

impl<'a> Iterator for Fields<'a> {
    type Item = (&'static str, Field<'a>);

    fn next(&mut self) -> Option<Self::Item> {
        self.field_names
            .next()
            .and_then(|&name| self.access.field(name).map(|field| (name, field)))
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.field_names.size_hint()
    }
}

impl<'a> DoubleEndedIterator for Fields<'a> {
    fn next_back(&mut self) -> Option<Self::Item> {
        self.field_names
            .next_back()
            .and_then(|&name| self.access.field(name).map(|field| (name, field)))
    }
}

impl<'a> ExactSizeIterator for Fields<'a> {}
impl<'a> FusedIterator for Fields<'a> {}