proc_bitfield/
nested.rs

1use crate::{Bitfield, Bits, SetBits};
2use core::{
3    fmt,
4    marker::PhantomData,
5    ops::{Deref, DerefMut},
6};
7
8#[derive(Clone, Copy)]
9#[repr(transparent)]
10pub struct NestedRef<'a, T: Bitfield, U: Bitfield, const START: usize, const END: usize>
11where
12    T::Storage: Bits<U::Storage>,
13{
14    value: U,
15    _parent: PhantomData<&'a T>,
16}
17
18impl<T: Bitfield, U: Bitfield, const START: usize, const END: usize> fmt::Debug
19    for NestedRef<'_, T, U, START, END>
20where
21    T::Storage: Bits<U::Storage>,
22    U: fmt::Debug,
23{
24    #[inline]
25    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26        fmt::Debug::fmt(&self.value, f)
27    }
28}
29
30impl<'a, T: Bitfield, U: Bitfield, const START: usize, const END: usize>
31    NestedRef<'a, T, U, START, END>
32where
33    T::Storage: Bits<U::Storage>,
34{
35    #[inline]
36    pub fn new(parent: &'a T) -> Self {
37        NestedRef {
38            value: U::from_storage(parent.storage().bits::<START, END>()),
39            _parent: PhantomData,
40        }
41    }
42}
43
44impl<T: Bitfield, U: Bitfield, const START: usize, const END: usize> Deref
45    for NestedRef<'_, T, U, START, END>
46where
47    T::Storage: Bits<U::Storage>,
48{
49    type Target = U;
50
51    #[inline]
52    fn deref(&self) -> &Self::Target {
53        &self.value
54    }
55}
56
57pub struct NestedRefMut<'a, T: Bitfield, U: Bitfield, const START: usize, const END: usize>
58where
59    T::Storage: Bits<U::Storage> + SetBits<U::Storage>,
60    U::Storage: Clone,
61{
62    value: U,
63    parent: &'a mut T,
64}
65
66impl<'a, T: Bitfield, U: Bitfield, const START: usize, const END: usize>
67    NestedRefMut<'a, T, U, START, END>
68where
69    T::Storage: Bits<U::Storage> + SetBits<U::Storage>,
70    U::Storage: Clone,
71{
72    #[inline]
73    pub fn new(parent: &'a mut T) -> Self {
74        NestedRefMut {
75            value: U::from_storage(parent.storage().bits::<START, END>()),
76            parent,
77        }
78    }
79}
80
81impl<T: Bitfield, U: Bitfield, const START: usize, const END: usize> Deref
82    for NestedRefMut<'_, T, U, START, END>
83where
84    T::Storage: Bits<U::Storage> + SetBits<U::Storage>,
85    U::Storage: Clone,
86{
87    type Target = U;
88
89    #[inline]
90    fn deref(&self) -> &Self::Target {
91        &self.value
92    }
93}
94
95impl<T: Bitfield, U: Bitfield, const START: usize, const END: usize> DerefMut
96    for NestedRefMut<'_, T, U, START, END>
97where
98    T::Storage: Bits<U::Storage> + SetBits<U::Storage>,
99    U::Storage: Clone,
100{
101    #[inline]
102    fn deref_mut(&mut self) -> &mut Self::Target {
103        &mut self.value
104    }
105}
106
107impl<T: Bitfield, U: Bitfield, const START: usize, const END: usize> Drop
108    for NestedRefMut<'_, T, U, START, END>
109where
110    T::Storage: Bits<U::Storage> + SetBits<U::Storage>,
111    U::Storage: Clone,
112{
113    #[inline]
114    fn drop(&mut self) {
115        self.parent
116            .storage_mut()
117            .set_bits::<START, END>(self.value.storage().clone());
118    }
119}