diff options
author | chrysn <chrysn@fsfe.org> | 2018-08-25 01:00:55 +0200 |
---|---|---|
committer | Emilio Cobos Álvarez <emilio@crisal.io> | 2018-08-28 12:47:44 +0200 |
commit | aa1030ef4d19e527d1372d68f51b608598fe77f4 (patch) | |
tree | cf1b3c253f1bc6014fe83884de6afb36916d22b3 | |
parent | b3fca4df2eda2f2bfe8861ab88b05a2573a7b502 (diff) |
Tests: Add a --use-core --impl-debug case
This demonstrates the [...] abbreviation of large arrays in contrast to
the std variety. (Long (>32) arrays currently don't have a dedicated
test, but are piggy-backed on the derive-debug-bitfield and
derive-debug-function-pointer tests).
Note that the single occurrence of the `std` namespace in the
expectation is valid: No --ctypes-prefix was set, and ::std::os::raw is
the default value that needs to be overridden in most practical
--use-core applications.
-rw-r--r-- | tests/expectations/tests/derive-debug-bitfield-core.rs | 189 | ||||
-rw-r--r-- | tests/headers/derive-debug-bitfield-core.hpp | 7 |
2 files changed, 196 insertions, 0 deletions
diff --git a/tests/expectations/tests/derive-debug-bitfield-core.rs b/tests/expectations/tests/derive-debug-bitfield-core.rs new file mode 100644 index 00000000..11522bcb --- /dev/null +++ b/tests/expectations/tests/derive-debug-bitfield-core.rs @@ -0,0 +1,189 @@ +/* automatically generated by rust-bindgen */ + +#![allow( + dead_code, + non_snake_case, + non_camel_case_types, + non_upper_case_globals +)] + +#[repr(C)] +#[derive(Copy, Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)] +pub struct __BindgenBitfieldUnit<Storage, Align> +where + Storage: AsRef<[u8]> + AsMut<[u8]>, +{ + storage: Storage, + align: [Align; 0], +} + +impl<Storage, Align> __BindgenBitfieldUnit<Storage, Align> +where + Storage: AsRef<[u8]> + AsMut<[u8]>, +{ + #[inline] + pub fn new(storage: Storage) -> Self { + Self { storage, align: [] } + } + + #[inline] + pub fn get_bit(&self, index: usize) -> bool { + debug_assert!(index / 8 < self.storage.as_ref().len()); + + let byte_index = index / 8; + let byte = self.storage.as_ref()[byte_index]; + + let bit_index = if cfg!(target_endian = "big") { + 7 - (index % 8) + } else { + index % 8 + }; + + let mask = 1 << bit_index; + + byte & mask == mask + } + + #[inline] + pub fn set_bit(&mut self, index: usize, val: bool) { + debug_assert!(index / 8 < self.storage.as_ref().len()); + + let byte_index = index / 8; + let byte = &mut self.storage.as_mut()[byte_index]; + + let bit_index = if cfg!(target_endian = "big") { + 7 - (index % 8) + } else { + index % 8 + }; + + let mask = 1 << bit_index; + if val { + *byte |= mask; + } else { + *byte &= !mask; + } + } + + #[inline] + pub fn get(&self, bit_offset: usize, bit_width: u8) -> u64 { + debug_assert!(bit_width <= 64); + debug_assert!(bit_offset / 8 < self.storage.as_ref().len()); + debug_assert!((bit_offset + (bit_width as usize)) / 8 <= self.storage.as_ref().len()); + + let mut val = 0; + + for i in 0..(bit_width as usize) { + if self.get_bit(i + bit_offset) { + let index = if cfg!(target_endian = "big") { + bit_width as usize - 1 - i + } else { + i + }; + val |= 1 << index; + } + } + + val + } + + #[inline] + pub fn set(&mut self, bit_offset: usize, bit_width: u8, val: u64) { + debug_assert!(bit_width <= 64); + debug_assert!(bit_offset / 8 < self.storage.as_ref().len()); + debug_assert!((bit_offset + (bit_width as usize)) / 8 <= self.storage.as_ref().len()); + + for i in 0..(bit_width as usize) { + let mask = 1 << i; + let val_bit_is_set = val & mask == mask; + let index = if cfg!(target_endian = "big") { + bit_width as usize - 1 - i + } else { + i + }; + self.set_bit(index + bit_offset, val_bit_is_set); + } + } +} +#[repr(C)] +#[derive(Copy, Clone)] +pub struct C { + pub _bitfield_1: __BindgenBitfieldUnit<[u8; 1usize], u8>, + pub large_array: [::std::os::raw::c_int; 50usize], +} +#[test] +fn bindgen_test_layout_C() { + assert_eq!( + ::core::mem::size_of::<C>(), + 204usize, + concat!("Size of: ", stringify!(C)) + ); + assert_eq!( + ::core::mem::align_of::<C>(), + 4usize, + concat!("Alignment of ", stringify!(C)) + ); + assert_eq!( + unsafe { &(*(::core::ptr::null::<C>())).large_array as *const _ as usize }, + 4usize, + concat!( + "Offset of field: ", + stringify!(C), + "::", + stringify!(large_array) + ) + ); +} +impl Default for C { + fn default() -> Self { + unsafe { ::core::mem::zeroed() } + } +} +impl ::core::fmt::Debug for C { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!( + f, + "C {{ a : {:?}, b : {:?}, large_array: [...] }}", + self.a(), + self.b() + ) + } +} +impl C { + #[inline] + pub fn a(&self) -> bool { + unsafe { ::core::mem::transmute(self._bitfield_1.get(0usize, 1u8) as u8) } + } + #[inline] + pub fn set_a(&mut self, val: bool) { + unsafe { + let val: u8 = ::core::mem::transmute(val); + self._bitfield_1.set(0usize, 1u8, val as u64) + } + } + #[inline] + pub fn b(&self) -> bool { + unsafe { ::core::mem::transmute(self._bitfield_1.get(1usize, 7u8) as u8) } + } + #[inline] + pub fn set_b(&mut self, val: bool) { + unsafe { + let val: u8 = ::core::mem::transmute(val); + self._bitfield_1.set(1usize, 7u8, val as u64) + } + } + #[inline] + pub fn new_bitfield_1(a: bool, b: bool) -> __BindgenBitfieldUnit<[u8; 1usize], u8> { + let mut __bindgen_bitfield_unit: __BindgenBitfieldUnit<[u8; 1usize], u8> = + Default::default(); + __bindgen_bitfield_unit.set(0usize, 1u8, { + let a: u8 = unsafe { ::core::mem::transmute(a) }; + a as u64 + }); + __bindgen_bitfield_unit.set(1usize, 7u8, { + let b: u8 = unsafe { ::core::mem::transmute(b) }; + b as u64 + }); + __bindgen_bitfield_unit + } +} diff --git a/tests/headers/derive-debug-bitfield-core.hpp b/tests/headers/derive-debug-bitfield-core.hpp new file mode 100644 index 00000000..7a5694ec --- /dev/null +++ b/tests/headers/derive-debug-bitfield-core.hpp @@ -0,0 +1,7 @@ +// bindgen-flags: --impl-debug --use-core + +class C { + bool a: 1; + bool b: 7; + int large_array[50]; +}; |