• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #[repr(C)]
2 #[derive(Copy, Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
3 pub struct __BindgenBitfieldUnit<Storage> {
4     storage: Storage,
5 }
6 
7 impl<Storage> __BindgenBitfieldUnit<Storage> {
8     #[inline]
new(storage: Storage) -> Self9     pub const fn new(storage: Storage) -> Self {
10         Self { storage }
11     }
12 }
13 
14 impl<Storage> __BindgenBitfieldUnit<Storage>
15 where
16     Storage: AsRef<[u8]> + AsMut<[u8]>,
17 {
18     #[inline]
get_bit(&self, index: usize) -> bool19     pub fn get_bit(&self, index: usize) -> bool {
20         debug_assert!(index / 8 < self.storage.as_ref().len());
21 
22         let byte_index = index / 8;
23         let byte = self.storage.as_ref()[byte_index];
24 
25         let bit_index = if cfg!(target_endian = "big") {
26             7 - (index % 8)
27         } else {
28             index % 8
29         };
30 
31         let mask = 1 << bit_index;
32 
33         byte & mask == mask
34     }
35 
36     #[inline]
set_bit(&mut self, index: usize, val: bool)37     pub fn set_bit(&mut self, index: usize, val: bool) {
38         debug_assert!(index / 8 < self.storage.as_ref().len());
39 
40         let byte_index = index / 8;
41         let byte = &mut self.storage.as_mut()[byte_index];
42 
43         let bit_index = if cfg!(target_endian = "big") {
44             7 - (index % 8)
45         } else {
46             index % 8
47         };
48 
49         let mask = 1 << bit_index;
50         if val {
51             *byte |= mask;
52         } else {
53             *byte &= !mask;
54         }
55     }
56 
57     #[inline]
get(&self, bit_offset: usize, bit_width: u8) -> u6458     pub fn get(&self, bit_offset: usize, bit_width: u8) -> u64 {
59         debug_assert!(bit_width <= 64);
60         debug_assert!(bit_offset / 8 < self.storage.as_ref().len());
61         debug_assert!(
62             (bit_offset + (bit_width as usize)) / 8 <=
63                 self.storage.as_ref().len()
64         );
65 
66         let mut val = 0;
67 
68         for i in 0..(bit_width as usize) {
69             if self.get_bit(i + bit_offset) {
70                 let index = if cfg!(target_endian = "big") {
71                     bit_width as usize - 1 - i
72                 } else {
73                     i
74                 };
75                 val |= 1 << index;
76             }
77         }
78 
79         val
80     }
81 
82     #[inline]
set(&mut self, bit_offset: usize, bit_width: u8, val: u64)83     pub fn set(&mut self, bit_offset: usize, bit_width: u8, val: u64) {
84         debug_assert!(bit_width <= 64);
85         debug_assert!(bit_offset / 8 < self.storage.as_ref().len());
86         debug_assert!(
87             (bit_offset + (bit_width as usize)) / 8 <=
88                 self.storage.as_ref().len()
89         );
90 
91         for i in 0..(bit_width as usize) {
92             let mask = 1 << i;
93             let val_bit_is_set = val & mask == mask;
94             let index = if cfg!(target_endian = "big") {
95                 bit_width as usize - 1 - i
96             } else {
97                 i
98             };
99             self.set_bit(index + bit_offset, val_bit_is_set);
100         }
101     }
102 }
103