diff options
author | Sergey Pepyakin <s.pepyakin@gmail.com> | 2017-10-05 11:02:26 +0300 |
---|---|---|
committer | Sergey Pepyakin <s.pepyakin@gmail.com> | 2017-10-05 18:04:24 +0300 |
commit | a1c6802bee7b687ae64e90acc4dc8d4b03ffe3ab (patch) | |
tree | fe6d10a6c8dc518202021624879d8a37ec417a40 /tests | |
parent | af967d701a6a693d201793a875a6c1b720e489f0 (diff) |
Use bitfield getter_name in impl_debug.
Also make impl_partialeq test to also cover impl_debug case.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/expectations/tests/derive-bitfield-method-same-name.rs (renamed from tests/expectations/tests/derive-partialeq-bitfield-same-name.rs) | 57 | ||||
-rw-r--r-- | tests/headers/derive-bitfield-method-same-name.hpp | 13 | ||||
-rw-r--r-- | tests/headers/derive-partialeq-bitfield-same-name.hpp | 9 |
3 files changed, 51 insertions, 28 deletions
diff --git a/tests/expectations/tests/derive-partialeq-bitfield-same-name.rs b/tests/expectations/tests/derive-bitfield-method-same-name.rs index 9ee2db42..1873d0d6 100644 --- a/tests/expectations/tests/derive-partialeq-bitfield-same-name.rs +++ b/tests/expectations/tests/derive-bitfield-method-same-name.rs @@ -4,32 +4,37 @@ #![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] + +/// Because this struct have array larger than 32 items +/// and --with-derive-partialeq --impl-partialeq --impl-debug is provided, +/// this struct should manually implement `Debug` and `PartialEq`. #[repr(C)] #[derive(Copy)] pub struct Foo { - pub big_array: [::std::os::raw::c_char; 33usize], - pub _bitfield_1: u8, + pub large: [::std::os::raw::c_int; 33usize], + pub _bitfield_1: [u8; 2usize], + pub __bindgen_padding_0: u16, } #[test] fn bindgen_test_layout_Foo() { assert_eq!( ::std::mem::size_of::<Foo>(), - 34usize, + 136usize, concat!("Size of: ", stringify!(Foo)) ); assert_eq!( ::std::mem::align_of::<Foo>(), - 1usize, + 4usize, concat!("Alignment of ", stringify!(Foo)) ); assert_eq!( - unsafe { &(*(0 as *const Foo)).big_array as *const _ as usize }, + unsafe { &(*(0 as *const Foo)).large as *const _ as usize }, 0usize, concat!( "Alignment of field: ", stringify!(Foo), "::", - stringify!(big_array) + stringify!(large) ) ); } @@ -55,37 +60,51 @@ impl Default for Foo { unsafe { ::std::mem::zeroed() } } } +impl ::std::fmt::Debug for Foo { + fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { + write!( + f, + "Foo {{ large: [{}], type_ : {:?}, }}", + self.large + .iter() + .enumerate() + .map(|(i, v)| format!("{}{:?}", if i > 0 { ", " } else { "" }, v)) + .collect::<String>(), + self.type__bindgen_bitfield() + ) + } +} impl ::std::cmp::PartialEq for Foo { fn eq(&self, other: &Foo) -> bool { - &self.big_array[..] == &other.big_array[..] + &self.large[..] == &other.large[..] && self.type__bindgen_bitfield() == other.type__bindgen_bitfield() } } impl Foo { #[inline] pub fn type__bindgen_bitfield(&self) -> ::std::os::raw::c_char { - let mut unit_field_val: u8 = unsafe { ::std::mem::uninitialized() }; + let mut unit_field_val: u16 = unsafe { ::std::mem::uninitialized() }; unsafe { ::std::ptr::copy_nonoverlapping( &self._bitfield_1 as *const _ as *const u8, - &mut unit_field_val as *mut u8 as *mut u8, - ::std::mem::size_of::<u8>(), + &mut unit_field_val as *mut u16 as *mut u8, + ::std::mem::size_of::<u16>(), ) }; - let mask = 7u64 as u8; + let mask = 7u64 as u16; let val = (unit_field_val & mask) >> 0usize; unsafe { ::std::mem::transmute(val as u8) } } #[inline] pub fn set_type__bindgen_bitfield(&mut self, val: ::std::os::raw::c_char) { - let mask = 7u64 as u8; - let val = val as u8 as u8; - let mut unit_field_val: u8 = unsafe { ::std::mem::uninitialized() }; + let mask = 7u64 as u16; + let val = val as u8 as u16; + let mut unit_field_val: u16 = unsafe { ::std::mem::uninitialized() }; unsafe { ::std::ptr::copy_nonoverlapping( &self._bitfield_1 as *const _ as *const u8, - &mut unit_field_val as *mut u8 as *mut u8, - ::std::mem::size_of::<u8>(), + &mut unit_field_val as *mut u16 as *mut u8, + ::std::mem::size_of::<u16>(), ) }; unit_field_val &= !mask; @@ -94,13 +113,13 @@ impl Foo { ::std::ptr::copy_nonoverlapping( &unit_field_val as *const _ as *const u8, &mut self._bitfield_1 as *mut _ as *mut u8, - ::std::mem::size_of::<u8>(), + ::std::mem::size_of::<u16>(), ); } } #[inline] - pub fn new_bitfield_1(type__bindgen_bitfield: ::std::os::raw::c_char) -> u8 { - (0 | ((type__bindgen_bitfield as u8 as u8) << 0usize) & (7u64 as u8)) + pub fn new_bitfield_1(type__bindgen_bitfield: ::std::os::raw::c_char) -> u16 { + (0 | ((type__bindgen_bitfield as u8 as u16) << 0usize) & (7u64 as u16)) } #[inline] pub unsafe fn type_(&mut self) -> ::std::os::raw::c_char { diff --git a/tests/headers/derive-bitfield-method-same-name.hpp b/tests/headers/derive-bitfield-method-same-name.hpp new file mode 100644 index 00000000..4b7b21e9 --- /dev/null +++ b/tests/headers/derive-bitfield-method-same-name.hpp @@ -0,0 +1,13 @@ +// bindgen-flags: --with-derive-partialeq --impl-partialeq --impl-debug + +/// Because this struct have array larger than 32 items +/// and --with-derive-partialeq --impl-partialeq --impl-debug is provided, +/// this struct should manually implement `Debug` and `PartialEq`. +struct Foo { + int large[33]; + char type_ : 3; + unsigned : 8; + char type(); + void set_type_(char c); + void set_type(char c); +}; diff --git a/tests/headers/derive-partialeq-bitfield-same-name.hpp b/tests/headers/derive-partialeq-bitfield-same-name.hpp deleted file mode 100644 index a85016c7..00000000 --- a/tests/headers/derive-partialeq-bitfield-same-name.hpp +++ /dev/null @@ -1,9 +0,0 @@ -// bindgen-flags: --with-derive-hash --with-derive-partialeq --with-derive-eq --impl-partialeq - -struct Foo { - char big_array[33]; - char type_ : 3; - char type(); - void set_type_(char c); - void set_type(char c); -}; |