diff options
author | Yamakaky <yamakaky@yamaworld.fr> | 2016-03-11 15:19:44 +0100 |
---|---|---|
committer | Yamakaky <yamakaky@yamaworld.fr> | 2016-03-11 15:19:44 +0100 |
commit | af4ab06657ae224bd2e7bec8efdbef3aa924f34c (patch) | |
tree | c24dd08c48f1ca60adb5bc3af28259e083c48c1d | |
parent | 08a32d5276afa7c120af3ce83ba3e32d59503e68 (diff) |
derive(Debug) on unions.
Related #282
-rw-r--r-- | src/gen.rs | 17 | ||||
-rw-r--r-- | tests/test_struct.rs | 1 | ||||
-rw-r--r-- | tests/test_union.rs | 55 |
3 files changed, 72 insertions, 1 deletions
@@ -671,7 +671,22 @@ fn cunion_to_rs(ctx: &mut GenCtx, name: String, derive_debug: bool, layout: Layo empty_generics() ); let union_id = rust_type_id(ctx, name.clone()); - let union_attrs = vec!(mk_repr_attr(ctx, layout), mk_deriving_copy_attr(ctx, false)); + let union_attrs = { + let mut attrs = vec!(mk_repr_attr(ctx, layout), mk_deriving_copy_attr(ctx, false)); + if derive_debug { + let can_derive_debug = members.iter() + .all(|member| match member { + &CompMember::Field(ref f) | + &CompMember::CompField(_, ref f) => f.ty.can_derive_debug(), + _ => true + }); + if can_derive_debug { + attrs.push(mk_deriving_debug_attr(ctx)) + } + } + attrs + }; + let union_def = mk_item(ctx, union_id, def, ast::Visibility::Public, union_attrs); let union_impl = ast::ItemKind::Impl( diff --git a/tests/test_struct.rs b/tests/test_struct.rs index 4539be83..2360f208 100644 --- a/tests/test_struct.rs +++ b/tests/test_struct.rs @@ -132,6 +132,7 @@ fn with_anon_union() { } #[repr(C)] #[derive(Copy)] + #[derive(Debug)] pub struct Union_Unnamed1 { pub _bindgen_data_: [u32; 1usize], } diff --git a/tests/test_union.rs b/tests/test_union.rs index 2e1869ac..f35e325a 100644 --- a/tests/test_union.rs +++ b/tests/test_union.rs @@ -5,6 +5,7 @@ fn with_anon_struct() { assert_bind_eq(Default::default(), "headers/union_with_anon_struct.h", " #[repr(C)] #[derive(Copy)] + #[derive(Debug)] pub struct Union_foo { pub _bindgen_data_: [u32; 2usize], } @@ -41,6 +42,7 @@ fn with_anon_struct_bitfield() { assert_bind_eq(Default::default(), "headers/union_with_anon_struct_bitfield.h", " #[repr(C)] #[derive(Copy)] + #[derive(Debug)] pub struct Union_foo { pub _bindgen_data_: [u32; 1usize], } @@ -67,6 +69,7 @@ fn with_anon_union() { assert_bind_eq(Default::default(), "headers/union_with_anon_union.h", " #[repr(C)] #[derive(Copy)] + #[derive(Debug)] pub struct Union_foo { pub _bindgen_data_: [u32; 1usize], } @@ -84,6 +87,7 @@ fn with_anon_union() { } #[repr(C)] #[derive(Copy)] + #[derive(Debug)] pub struct Union_Unnamed1 { pub _bindgen_data_: [u32; 1usize], } @@ -111,6 +115,7 @@ fn with_anon_unnamed_struct() { assert_bind_eq(Default::default(), "headers/union_with_anon_unnamed_struct.h", " #[repr(C)] #[derive(Copy)] + #[derive(Debug)] pub struct Union_pixel { pub _bindgen_data_: [u32; 1usize], } @@ -150,6 +155,7 @@ fn with_anon_unnamed_union() { assert_bind_eq(Default::default(), "headers/union_with_anon_unnamed_union.h", " #[repr(C)] #[derive(Copy)] + #[derive(Debug)] pub struct Union_foo { pub _bindgen_data_: [u32; 1usize], } @@ -181,6 +187,7 @@ fn with_nesting() { assert_bind_eq(Default::default(), "headers/union_with_nesting.h", " #[repr(C)] #[derive(Copy)] + #[derive(Debug)] pub struct Union_foo { pub _bindgen_data_: [u32; 1usize], } @@ -214,3 +221,51 @@ fn with_nesting() { } "); } + +#[test] +fn with_derive_debug() { + assert_bind_eq(Default::default(), "headers/union_with_big_member.h", " + #[repr(C)] + #[derive(Copy)] + pub struct Union_WithBigArray { + pub _bindgen_data_: [u32; 33usize], + } + impl Union_WithBigArray { + pub unsafe fn a(&mut self) -> *mut ::std::os::raw::c_int { + let raw: *mut u8 = ::std::mem::transmute(&self._bindgen_data_); + ::std::mem::transmute(raw.offset(0)) + } + pub unsafe fn b(&mut self) -> *mut [::std::os::raw::c_int; 33usize] { + let raw: *mut u8 = ::std::mem::transmute(&self._bindgen_data_); + ::std::mem::transmute(raw.offset(0)) + } + } + impl ::std::clone::Clone for Union_WithBigArray { + fn clone(&self) -> Self { *self } + } + impl ::std::default::Default for Union_WithBigArray { + fn default() -> Self { unsafe { ::std::mem::zeroed() } } + } + #[repr(C)] + #[derive(Copy)] + pub struct Union_WithBigMember { + pub _bindgen_data_: [u32; 33usize], + } + impl Union_WithBigMember { + pub unsafe fn a(&mut self) -> *mut ::std::os::raw::c_int { + let raw: *mut u8 = ::std::mem::transmute(&self._bindgen_data_); + ::std::mem::transmute(raw.offset(0)) + } + pub unsafe fn b(&mut self) -> *mut Union_WithBigArray { + let raw: *mut u8 = ::std::mem::transmute(&self._bindgen_data_); + ::std::mem::transmute(raw.offset(0)) + } + } + impl ::std::clone::Clone for Union_WithBigMember { + fn clone(&self) -> Self { *self } + } + impl ::std::default::Default for Union_WithBigMember { + fn default() -> Self { unsafe { ::std::mem::zeroed() } } + } + "); +} |