Skip to content

Commit af4ab06

Browse files
committed
derive(Debug) on unions.
Related rust-lang#282
1 parent 08a32d5 commit af4ab06

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

src/gen.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,22 @@ fn cunion_to_rs(ctx: &mut GenCtx, name: String, derive_debug: bool, layout: Layo
671671
empty_generics()
672672
);
673673
let union_id = rust_type_id(ctx, name.clone());
674-
let union_attrs = vec!(mk_repr_attr(ctx, layout), mk_deriving_copy_attr(ctx, false));
674+
let union_attrs = {
675+
let mut attrs = vec!(mk_repr_attr(ctx, layout), mk_deriving_copy_attr(ctx, false));
676+
if derive_debug {
677+
let can_derive_debug = members.iter()
678+
.all(|member| match member {
679+
&CompMember::Field(ref f) |
680+
&CompMember::CompField(_, ref f) => f.ty.can_derive_debug(),
681+
_ => true
682+
});
683+
if can_derive_debug {
684+
attrs.push(mk_deriving_debug_attr(ctx))
685+
}
686+
}
687+
attrs
688+
};
689+
675690
let union_def = mk_item(ctx, union_id, def, ast::Visibility::Public, union_attrs);
676691

677692
let union_impl = ast::ItemKind::Impl(

tests/test_struct.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ fn with_anon_union() {
132132
}
133133
#[repr(C)]
134134
#[derive(Copy)]
135+
#[derive(Debug)]
135136
pub struct Union_Unnamed1 {
136137
pub _bindgen_data_: [u32; 1usize],
137138
}

tests/test_union.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ fn with_anon_struct() {
55
assert_bind_eq(Default::default(), "headers/union_with_anon_struct.h", "
66
#[repr(C)]
77
#[derive(Copy)]
8+
#[derive(Debug)]
89
pub struct Union_foo {
910
pub _bindgen_data_: [u32; 2usize],
1011
}
@@ -41,6 +42,7 @@ fn with_anon_struct_bitfield() {
4142
assert_bind_eq(Default::default(), "headers/union_with_anon_struct_bitfield.h", "
4243
#[repr(C)]
4344
#[derive(Copy)]
45+
#[derive(Debug)]
4446
pub struct Union_foo {
4547
pub _bindgen_data_: [u32; 1usize],
4648
}
@@ -67,6 +69,7 @@ fn with_anon_union() {
6769
assert_bind_eq(Default::default(), "headers/union_with_anon_union.h", "
6870
#[repr(C)]
6971
#[derive(Copy)]
72+
#[derive(Debug)]
7073
pub struct Union_foo {
7174
pub _bindgen_data_: [u32; 1usize],
7275
}
@@ -84,6 +87,7 @@ fn with_anon_union() {
8487
}
8588
#[repr(C)]
8689
#[derive(Copy)]
90+
#[derive(Debug)]
8791
pub struct Union_Unnamed1 {
8892
pub _bindgen_data_: [u32; 1usize],
8993
}
@@ -111,6 +115,7 @@ fn with_anon_unnamed_struct() {
111115
assert_bind_eq(Default::default(), "headers/union_with_anon_unnamed_struct.h", "
112116
#[repr(C)]
113117
#[derive(Copy)]
118+
#[derive(Debug)]
114119
pub struct Union_pixel {
115120
pub _bindgen_data_: [u32; 1usize],
116121
}
@@ -150,6 +155,7 @@ fn with_anon_unnamed_union() {
150155
assert_bind_eq(Default::default(), "headers/union_with_anon_unnamed_union.h", "
151156
#[repr(C)]
152157
#[derive(Copy)]
158+
#[derive(Debug)]
153159
pub struct Union_foo {
154160
pub _bindgen_data_: [u32; 1usize],
155161
}
@@ -181,6 +187,7 @@ fn with_nesting() {
181187
assert_bind_eq(Default::default(), "headers/union_with_nesting.h", "
182188
#[repr(C)]
183189
#[derive(Copy)]
190+
#[derive(Debug)]
184191
pub struct Union_foo {
185192
pub _bindgen_data_: [u32; 1usize],
186193
}
@@ -214,3 +221,51 @@ fn with_nesting() {
214221
}
215222
");
216223
}
224+
225+
#[test]
226+
fn with_derive_debug() {
227+
assert_bind_eq(Default::default(), "headers/union_with_big_member.h", "
228+
#[repr(C)]
229+
#[derive(Copy)]
230+
pub struct Union_WithBigArray {
231+
pub _bindgen_data_: [u32; 33usize],
232+
}
233+
impl Union_WithBigArray {
234+
pub unsafe fn a(&mut self) -> *mut ::std::os::raw::c_int {
235+
let raw: *mut u8 = ::std::mem::transmute(&self._bindgen_data_);
236+
::std::mem::transmute(raw.offset(0))
237+
}
238+
pub unsafe fn b(&mut self) -> *mut [::std::os::raw::c_int; 33usize] {
239+
let raw: *mut u8 = ::std::mem::transmute(&self._bindgen_data_);
240+
::std::mem::transmute(raw.offset(0))
241+
}
242+
}
243+
impl ::std::clone::Clone for Union_WithBigArray {
244+
fn clone(&self) -> Self { *self }
245+
}
246+
impl ::std::default::Default for Union_WithBigArray {
247+
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
248+
}
249+
#[repr(C)]
250+
#[derive(Copy)]
251+
pub struct Union_WithBigMember {
252+
pub _bindgen_data_: [u32; 33usize],
253+
}
254+
impl Union_WithBigMember {
255+
pub unsafe fn a(&mut self) -> *mut ::std::os::raw::c_int {
256+
let raw: *mut u8 = ::std::mem::transmute(&self._bindgen_data_);
257+
::std::mem::transmute(raw.offset(0))
258+
}
259+
pub unsafe fn b(&mut self) -> *mut Union_WithBigArray {
260+
let raw: *mut u8 = ::std::mem::transmute(&self._bindgen_data_);
261+
::std::mem::transmute(raw.offset(0))
262+
}
263+
}
264+
impl ::std::clone::Clone for Union_WithBigMember {
265+
fn clone(&self) -> Self { *self }
266+
}
267+
impl ::std::default::Default for Union_WithBigMember {
268+
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
269+
}
270+
");
271+
}

0 commit comments

Comments
 (0)