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
use crate::{Bitfield, Bits, SetBits};
use core::{
    fmt,
    marker::PhantomData,
    ops::{Deref, DerefMut},
};

#[derive(Clone, Copy)]
#[repr(transparent)]
pub struct NestedRef<'a, T: Bitfield, U: Bitfield, const START: usize, const END: usize>
where
    T::Storage: Bits<U::Storage>,
{
    value: U,
    _parent: PhantomData<&'a T>,
}

impl<'a, T: Bitfield, U: Bitfield, const START: usize, const END: usize> fmt::Debug
    for NestedRef<'a, T, U, START, END>
where
    T::Storage: Bits<U::Storage>,
    U: fmt::Debug,
{
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(&self.value, f)
    }
}

impl<'a, T: Bitfield, U: Bitfield, const START: usize, const END: usize>
    NestedRef<'a, T, U, START, END>
where
    T::Storage: Bits<U::Storage>,
{
    #[inline]
    pub fn new(parent: &'a T) -> Self {
        NestedRef {
            value: U::from_storage(parent.storage().bits::<START, END>()),
            _parent: PhantomData,
        }
    }
}

impl<'a, T: Bitfield, U: Bitfield, const START: usize, const END: usize> Deref
    for NestedRef<'a, T, U, START, END>
where
    T::Storage: Bits<U::Storage>,
{
    type Target = U;

    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.value
    }
}

pub struct NestedRefMut<'a, T: Bitfield, U: Bitfield, const START: usize, const END: usize>
where
    T::Storage: Bits<U::Storage> + SetBits<U::Storage>,
    U::Storage: Clone,
{
    value: U,
    parent: &'a mut T,
}

impl<'a, T: Bitfield, U: Bitfield, const START: usize, const END: usize>
    NestedRefMut<'a, T, U, START, END>
where
    T::Storage: Bits<U::Storage> + SetBits<U::Storage>,
    U::Storage: Clone,
{
    #[inline]
    pub fn new(parent: &'a mut T) -> Self {
        NestedRefMut {
            value: U::from_storage(parent.storage().bits::<START, END>()),
            parent,
        }
    }
}

impl<'a, T: Bitfield, U: Bitfield, const START: usize, const END: usize> Deref
    for NestedRefMut<'a, T, U, START, END>
where
    T::Storage: Bits<U::Storage> + SetBits<U::Storage>,
    U::Storage: Clone,
{
    type Target = U;

    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.value
    }
}

impl<'a, T: Bitfield, U: Bitfield, const START: usize, const END: usize> DerefMut
    for NestedRefMut<'a, T, U, START, END>
where
    T::Storage: Bits<U::Storage> + SetBits<U::Storage>,
    U::Storage: Clone,
{
    #[inline]
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.value
    }
}

impl<'a, T: Bitfield, U: Bitfield, const START: usize, const END: usize> Drop
    for NestedRefMut<'a, T, U, START, END>
where
    T::Storage: Bits<U::Storage> + SetBits<U::Storage>,
    U::Storage: Clone,
{
    #[inline]
    fn drop(&mut self) {
        self.parent
            .storage_mut()
            .set_bits::<START, END>(self.value.storage().clone());
    }
}