Skip to content

Commit a1c6802

Browse files
committed
Use bitfield getter_name in impl_debug.
Also make impl_partialeq test to also cover impl_debug case.
1 parent af967d7 commit a1c6802

File tree

4 files changed

+56
-33
lines changed

4 files changed

+56
-33
lines changed

src/codegen/impl_debug.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ pub fn gen_debug_impl(
2828
&Field::Bitfields(ref bu) => bu.impl_debug(ctx, ()),
2929
});
3030

31-
3231
for (i, (fstring, toks)) in processed_fields.enumerate() {
3332
if i > 0 {
3433
format_string.push_str(", ");
@@ -91,14 +90,15 @@ impl<'a> ImplDebug<'a> for BitfieldUnit {
9190
) -> Option<(String, Vec<quote::Tokens>)> {
9291
let mut format_string = String::new();
9392
let mut tokens = vec![];
94-
for (i, bu) in self.bitfields().iter().enumerate() {
93+
for (i, bitfield) in self.bitfields().iter().enumerate() {
9594
if i > 0 {
9695
format_string.push_str(", ");
9796
}
9897

99-
if let Some(name) = bu.name() {
100-
format_string.push_str(&format!("{} : {{:?}}", name));
101-
let name_ident = ctx.rust_ident_raw(name);
98+
if let Some(bitfield_name) = bitfield.name() {
99+
format_string.push_str(&format!("{} : {{:?}}", bitfield_name));
100+
let getter_name = bitfield.getter_name();
101+
let name_ident = ctx.rust_ident_raw(getter_name);
102102
tokens.push(quote! {
103103
self.#name_ident ()
104104
});

tests/expectations/tests/derive-partialeq-bitfield-same-name.rs renamed to tests/expectations/tests/derive-bitfield-method-same-name.rs

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,37 @@
44
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
55

66

7+
8+
/// Because this struct have array larger than 32 items
9+
/// and --with-derive-partialeq --impl-partialeq --impl-debug is provided,
10+
/// this struct should manually implement `Debug` and `PartialEq`.
711
#[repr(C)]
812
#[derive(Copy)]
913
pub struct Foo {
10-
pub big_array: [::std::os::raw::c_char; 33usize],
11-
pub _bitfield_1: u8,
14+
pub large: [::std::os::raw::c_int; 33usize],
15+
pub _bitfield_1: [u8; 2usize],
16+
pub __bindgen_padding_0: u16,
1217
}
1318
#[test]
1419
fn bindgen_test_layout_Foo() {
1520
assert_eq!(
1621
::std::mem::size_of::<Foo>(),
17-
34usize,
22+
136usize,
1823
concat!("Size of: ", stringify!(Foo))
1924
);
2025
assert_eq!(
2126
::std::mem::align_of::<Foo>(),
22-
1usize,
27+
4usize,
2328
concat!("Alignment of ", stringify!(Foo))
2429
);
2530
assert_eq!(
26-
unsafe { &(*(0 as *const Foo)).big_array as *const _ as usize },
31+
unsafe { &(*(0 as *const Foo)).large as *const _ as usize },
2732
0usize,
2833
concat!(
2934
"Alignment of field: ",
3035
stringify!(Foo),
3136
"::",
32-
stringify!(big_array)
37+
stringify!(large)
3338
)
3439
);
3540
}
@@ -55,37 +60,51 @@ impl Default for Foo {
5560
unsafe { ::std::mem::zeroed() }
5661
}
5762
}
63+
impl ::std::fmt::Debug for Foo {
64+
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
65+
write!(
66+
f,
67+
"Foo {{ large: [{}], type_ : {:?}, }}",
68+
self.large
69+
.iter()
70+
.enumerate()
71+
.map(|(i, v)| format!("{}{:?}", if i > 0 { ", " } else { "" }, v))
72+
.collect::<String>(),
73+
self.type__bindgen_bitfield()
74+
)
75+
}
76+
}
5877
impl ::std::cmp::PartialEq for Foo {
5978
fn eq(&self, other: &Foo) -> bool {
60-
&self.big_array[..] == &other.big_array[..]
79+
&self.large[..] == &other.large[..]
6180
&& self.type__bindgen_bitfield() == other.type__bindgen_bitfield()
6281
}
6382
}
6483
impl Foo {
6584
#[inline]
6685
pub fn type__bindgen_bitfield(&self) -> ::std::os::raw::c_char {
67-
let mut unit_field_val: u8 = unsafe { ::std::mem::uninitialized() };
86+
let mut unit_field_val: u16 = unsafe { ::std::mem::uninitialized() };
6887
unsafe {
6988
::std::ptr::copy_nonoverlapping(
7089
&self._bitfield_1 as *const _ as *const u8,
71-
&mut unit_field_val as *mut u8 as *mut u8,
72-
::std::mem::size_of::<u8>(),
90+
&mut unit_field_val as *mut u16 as *mut u8,
91+
::std::mem::size_of::<u16>(),
7392
)
7493
};
75-
let mask = 7u64 as u8;
94+
let mask = 7u64 as u16;
7695
let val = (unit_field_val & mask) >> 0usize;
7796
unsafe { ::std::mem::transmute(val as u8) }
7897
}
7998
#[inline]
8099
pub fn set_type__bindgen_bitfield(&mut self, val: ::std::os::raw::c_char) {
81-
let mask = 7u64 as u8;
82-
let val = val as u8 as u8;
83-
let mut unit_field_val: u8 = unsafe { ::std::mem::uninitialized() };
100+
let mask = 7u64 as u16;
101+
let val = val as u8 as u16;
102+
let mut unit_field_val: u16 = unsafe { ::std::mem::uninitialized() };
84103
unsafe {
85104
::std::ptr::copy_nonoverlapping(
86105
&self._bitfield_1 as *const _ as *const u8,
87-
&mut unit_field_val as *mut u8 as *mut u8,
88-
::std::mem::size_of::<u8>(),
106+
&mut unit_field_val as *mut u16 as *mut u8,
107+
::std::mem::size_of::<u16>(),
89108
)
90109
};
91110
unit_field_val &= !mask;
@@ -94,13 +113,13 @@ impl Foo {
94113
::std::ptr::copy_nonoverlapping(
95114
&unit_field_val as *const _ as *const u8,
96115
&mut self._bitfield_1 as *mut _ as *mut u8,
97-
::std::mem::size_of::<u8>(),
116+
::std::mem::size_of::<u16>(),
98117
);
99118
}
100119
}
101120
#[inline]
102-
pub fn new_bitfield_1(type__bindgen_bitfield: ::std::os::raw::c_char) -> u8 {
103-
(0 | ((type__bindgen_bitfield as u8 as u8) << 0usize) & (7u64 as u8))
121+
pub fn new_bitfield_1(type__bindgen_bitfield: ::std::os::raw::c_char) -> u16 {
122+
(0 | ((type__bindgen_bitfield as u8 as u16) << 0usize) & (7u64 as u16))
104123
}
105124
#[inline]
106125
pub unsafe fn type_(&mut self) -> ::std::os::raw::c_char {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// bindgen-flags: --with-derive-partialeq --impl-partialeq --impl-debug
2+
3+
/// Because this struct have array larger than 32 items
4+
/// and --with-derive-partialeq --impl-partialeq --impl-debug is provided,
5+
/// this struct should manually implement `Debug` and `PartialEq`.
6+
struct Foo {
7+
int large[33];
8+
char type_ : 3;
9+
unsigned : 8;
10+
char type();
11+
void set_type_(char c);
12+
void set_type(char c);
13+
};

tests/headers/derive-partialeq-bitfield-same-name.hpp

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)