1use core::ops::Deref;
2use core::ops::DerefMut;
3
4#[derive(Debug)]
9pub struct BitMut<'a> {
10 buf: &'a mut u8,
11 index: u8,
12 value: bool,
13}
14
15impl<'a> BitMut<'a> {
16 #[inline(always)]
17 pub fn new(buf: &'a mut u8, index: u8) -> Self {
18 let value = *buf & (1 << index) != 0;
19 Self { buf, index, value }
20 }
21}
22
23impl Deref for BitMut<'_> {
24 type Target = bool;
25
26 #[inline(always)]
27 fn deref(&self) -> &Self::Target {
28 &self.value
29 }
30}
31
32impl DerefMut for BitMut<'_> {
33 #[inline(always)]
34 fn deref_mut(&mut self) -> &mut Self::Target {
35 &mut self.value
36 }
37}
38
39impl Drop for BitMut<'_> {
40 #[inline(always)]
41 fn drop(&mut self) {
42 *self.buf &= !(1 << self.index);
43 *self.buf |= u8::from(self.value) << self.index;
44 }
45}
46
47#[derive(Debug)]
52pub struct WordMut<'a> {
53 buf: &'a mut [u8; 2],
54 value: u16,
55}
56
57impl<'a> WordMut<'a> {
58 #[inline(always)]
59 pub fn new(buf: &'a mut [u8; 2]) -> Self {
60 let value = u16::from_be_bytes(*buf);
61 Self { buf, value }
62 }
63}
64
65impl Deref for WordMut<'_> {
66 type Target = u16;
67
68 #[inline(always)]
69 fn deref(&self) -> &Self::Target {
70 &self.value
71 }
72}
73
74impl DerefMut for WordMut<'_> {
75 #[inline(always)]
76 fn deref_mut(&mut self) -> &mut Self::Target {
77 &mut self.value
78 }
79}
80
81impl Drop for WordMut<'_> {
82 #[inline(always)]
83 fn drop(&mut self) {
84 *self.buf = self.value.to_be_bytes();
85 }
86}
87
88#[derive(Debug)]
93pub struct DWordMut<'a> {
94 buf: &'a mut [u8; 4],
95 value: u32,
96}
97
98impl<'a> DWordMut<'a> {
99 #[inline(always)]
100 pub fn new(buf: &'a mut [u8; 4]) -> Self {
101 let value = u32::from_be_bytes(*buf);
102 Self { buf, value }
103 }
104}
105
106impl Deref for DWordMut<'_> {
107 type Target = u32;
108
109 #[inline(always)]
110 fn deref(&self) -> &Self::Target {
111 &self.value
112 }
113}
114
115impl DerefMut for DWordMut<'_> {
116 #[inline(always)]
117 fn deref_mut(&mut self) -> &mut Self::Target {
118 &mut self.value
119 }
120}
121
122impl Drop for DWordMut<'_> {
123 #[inline(always)]
124 fn drop(&mut self) {
125 *self.buf = self.value.to_be_bytes();
126 }
127}
128
129#[derive(Debug)]
134pub struct LWordMut<'a> {
135 buf: &'a mut [u8; 8],
136 value: u64,
137}
138
139impl<'a> LWordMut<'a> {
140 #[inline(always)]
141 pub fn new(buf: &'a mut [u8; 8]) -> Self {
142 let value = u64::from_be_bytes(*buf);
143 Self { buf, value }
144 }
145}
146
147impl Deref for LWordMut<'_> {
148 type Target = u64;
149
150 #[inline(always)]
151 fn deref(&self) -> &Self::Target {
152 &self.value
153 }
154}
155
156impl DerefMut for LWordMut<'_> {
157 #[inline(always)]
158 fn deref_mut(&mut self) -> &mut Self::Target {
159 &mut self.value
160 }
161}
162
163impl Drop for LWordMut<'_> {
164 #[inline(always)]
165 fn drop(&mut self) {
166 *self.buf = self.value.to_be_bytes();
167 }
168}