From 8ad6d67b1e86685dae836630d00341f5bc737339 Mon Sep 17 00:00:00 2001 From: Jethro Beekman Date: Thu, 21 Mar 2019 15:45:03 -0700 Subject: [PATCH] Derive Default for function pointers --- src/ir/analysis/derive.rs | 6 +---- tests/expectations/tests/derive-fn-ptr.rs | 23 +++++++------------ .../expectations/tests/func_ptr_in_struct.rs | 14 +++++------ .../tests/issue-1216-variadic-member.rs | 14 +++++------ .../tests/layout_cmdline_token.rs | 7 +----- .../tests/libclang-3.8/call-conv-field.rs | 19 +++++++-------- .../tests/libclang-3.9/call-conv-field.rs | 7 +----- .../tests/libclang-4/call-conv-field.rs | 7 +----- .../tests/libclang-5/call-conv-field.rs | 7 +----- 9 files changed, 35 insertions(+), 69 deletions(-) diff --git a/src/ir/analysis/derive.rs b/src/ir/analysis/derive.rs index 1025150dc9..dbfe35a4a9 100644 --- a/src/ir/analysis/derive.rs +++ b/src/ir/analysis/derive.rs @@ -194,11 +194,7 @@ impl<'ctx> CannotDerive<'ctx> { let inner_type = self.ctx.resolve_type(inner).canonical_type(self.ctx); if let TypeKind::Function(ref sig) = *inner_type.kind() { - if let DeriveTrait::Default = self.derive_trait { - return CanDerive::No - } else { - return self.derive_trait.can_derive_fnptr(sig) - } + return self.derive_trait.can_derive_fnptr(sig) } else { return self.derive_trait.can_derive_pointer(); } diff --git a/tests/expectations/tests/derive-fn-ptr.rs b/tests/expectations/tests/derive-fn-ptr.rs index 4b7105e97d..7f158b3bc9 100644 --- a/tests/expectations/tests/derive-fn-ptr.rs +++ b/tests/expectations/tests/derive-fn-ptr.rs @@ -1,8 +1,11 @@ /* automatically generated by rust-bindgen */ - -#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] - +#![allow( + dead_code, + non_snake_case, + non_camel_case_types, + non_upper_case_globals +)] pub type my_fun_t = ::std::option::Option< unsafe extern "C" fn( @@ -25,7 +28,7 @@ pub type my_fun_t = ::std::option::Option< ), >; #[repr(C)] -#[derive(Copy, Clone)] +#[derive(Default, Copy, Clone)] pub struct Foo { pub callback: my_fun_t, } @@ -52,11 +55,6 @@ fn bindgen_test_layout_Foo() { ) ); } -impl Default for Foo { - fn default() -> Self { - unsafe { ::std::mem::zeroed() } - } -} pub type my_fun2_t = ::std::option::Option< unsafe extern "C" fn( arg1: ::std::os::raw::c_int, @@ -74,7 +72,7 @@ pub type my_fun2_t = ::std::option::Option< ), >; #[repr(C)] -#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] +#[derive(Debug, Default, Copy, Clone, Hash, PartialEq, Eq)] pub struct Bar { pub callback: my_fun2_t, } @@ -101,8 +99,3 @@ fn bindgen_test_layout_Bar() { ) ); } -impl Default for Bar { - fn default() -> Self { - unsafe { ::std::mem::zeroed() } - } -} diff --git a/tests/expectations/tests/func_ptr_in_struct.rs b/tests/expectations/tests/func_ptr_in_struct.rs index d66ce84d1d..f23984e825 100644 --- a/tests/expectations/tests/func_ptr_in_struct.rs +++ b/tests/expectations/tests/func_ptr_in_struct.rs @@ -1,6 +1,11 @@ /* automatically generated by rust-bindgen */ -#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] +#![allow( + dead_code, + non_snake_case, + non_camel_case_types, + non_upper_case_globals +)] #[repr(i32)] #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] @@ -8,7 +13,7 @@ pub enum baz { __bindgen_cannot_repr_c_on_empty_enum = 0, } #[repr(C)] -#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] +#[derive(Debug, Default, Copy, Clone, Hash, PartialEq, Eq)] pub struct Foo { pub bar: ::std::option::Option< unsafe extern "C" fn(x: ::std::os::raw::c_int, y: ::std::os::raw::c_int) -> baz, @@ -32,8 +37,3 @@ fn bindgen_test_layout_Foo() { concat!("Offset of field: ", stringify!(Foo), "::", stringify!(bar)) ); } -impl Default for Foo { - fn default() -> Self { - unsafe { ::std::mem::zeroed() } - } -} diff --git a/tests/expectations/tests/issue-1216-variadic-member.rs b/tests/expectations/tests/issue-1216-variadic-member.rs index 677993c042..e509956090 100644 --- a/tests/expectations/tests/issue-1216-variadic-member.rs +++ b/tests/expectations/tests/issue-1216-variadic-member.rs @@ -1,12 +1,17 @@ /* automatically generated by rust-bindgen */ -#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] +#![allow( + dead_code, + non_snake_case, + non_camel_case_types, + non_upper_case_globals +)] extern "C" { pub fn f(a: ::std::os::raw::c_int, ...); } #[repr(C)] -#[derive(Debug, Copy, Clone)] +#[derive(Debug, Default, Copy, Clone)] pub struct Foo { pub f: ::std::option::Option< unsafe extern "C" fn( @@ -35,8 +40,3 @@ fn bindgen_test_layout_Foo() { concat!("Offset of field: ", stringify!(Foo), "::", stringify!(f)) ); } -impl Default for Foo { - fn default() -> Self { - unsafe { ::std::mem::zeroed() } - } -} diff --git a/tests/expectations/tests/layout_cmdline_token.rs b/tests/expectations/tests/layout_cmdline_token.rs index e8b72843fb..2ea596fc96 100644 --- a/tests/expectations/tests/layout_cmdline_token.rs +++ b/tests/expectations/tests/layout_cmdline_token.rs @@ -72,7 +72,7 @@ pub type cmdline_parse_token_hdr_t = cmdline_token_hdr; /// get_help() fills the dstbuf with the help for the token. It returns /// -1 on error and 0 on success. #[repr(C)] -#[derive(Debug, Copy, Clone)] +#[derive(Debug, Default, Copy, Clone)] pub struct cmdline_token_ops { /// parse(token ptr, buf, res pts, buf len) pub parse: ::std::option::Option< @@ -162,11 +162,6 @@ fn bindgen_test_layout_cmdline_token_ops() { ) ); } -impl Default for cmdline_token_ops { - fn default() -> Self { - unsafe { ::std::mem::zeroed() } - } -} #[repr(u32)] #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] pub enum cmdline_numtype { diff --git a/tests/expectations/tests/libclang-3.8/call-conv-field.rs b/tests/expectations/tests/libclang-3.8/call-conv-field.rs index bad20dea8b..1800b6350b 100644 --- a/tests/expectations/tests/libclang-3.8/call-conv-field.rs +++ b/tests/expectations/tests/libclang-3.8/call-conv-field.rs @@ -1,16 +1,18 @@ /* automatically generated by rust-bindgen */ - -#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] +#![allow( + dead_code, + non_snake_case, + non_camel_case_types, + non_upper_case_globals +)] #![cfg(not(test))] - #[repr(C)] -#[derive(Copy, Clone)] +#[derive(Default, Copy, Clone)] pub struct JNINativeInterface_ { pub GetVersion: ::std::option::Option< - unsafe extern "stdcall" fn(env: *mut ::std::os::raw::c_void) - -> ::std::os::raw::c_int, + unsafe extern "stdcall" fn(env: *mut ::std::os::raw::c_void) -> ::std::os::raw::c_int, >, pub __hack: ::std::os::raw::c_ulonglong, } @@ -47,11 +49,6 @@ fn bindgen_test_layout_JNINativeInterface_() { ) ); } -impl Default for JNINativeInterface_ { - fn default() -> Self { - unsafe { ::std::mem::zeroed() } - } -} extern "C" { #[link_name = "\u{1}_bar@0"] pub fn bar(); diff --git a/tests/expectations/tests/libclang-3.9/call-conv-field.rs b/tests/expectations/tests/libclang-3.9/call-conv-field.rs index 375eb9ad83..6cf8ec5b8e 100644 --- a/tests/expectations/tests/libclang-3.9/call-conv-field.rs +++ b/tests/expectations/tests/libclang-3.9/call-conv-field.rs @@ -6,7 +6,7 @@ #[repr(C)] -#[derive(Copy, Clone)] +#[derive(Default, Copy, Clone)] pub struct JNINativeInterface_ { pub GetVersion: ::std::option::Option< unsafe extern "stdcall" fn(env: *mut ::std::os::raw::c_void) @@ -47,11 +47,6 @@ fn bindgen_test_layout_JNINativeInterface_() { ) ); } -impl Default for JNINativeInterface_ { - fn default() -> Self { - unsafe { ::std::mem::zeroed() } - } -} extern "stdcall" { #[link_name = "\u{1}_bar@0"] pub fn bar(); diff --git a/tests/expectations/tests/libclang-4/call-conv-field.rs b/tests/expectations/tests/libclang-4/call-conv-field.rs index 375eb9ad83..6cf8ec5b8e 100644 --- a/tests/expectations/tests/libclang-4/call-conv-field.rs +++ b/tests/expectations/tests/libclang-4/call-conv-field.rs @@ -6,7 +6,7 @@ #[repr(C)] -#[derive(Copy, Clone)] +#[derive(Default, Copy, Clone)] pub struct JNINativeInterface_ { pub GetVersion: ::std::option::Option< unsafe extern "stdcall" fn(env: *mut ::std::os::raw::c_void) @@ -47,11 +47,6 @@ fn bindgen_test_layout_JNINativeInterface_() { ) ); } -impl Default for JNINativeInterface_ { - fn default() -> Self { - unsafe { ::std::mem::zeroed() } - } -} extern "stdcall" { #[link_name = "\u{1}_bar@0"] pub fn bar(); diff --git a/tests/expectations/tests/libclang-5/call-conv-field.rs b/tests/expectations/tests/libclang-5/call-conv-field.rs index e167e5ebd2..76712fe790 100644 --- a/tests/expectations/tests/libclang-5/call-conv-field.rs +++ b/tests/expectations/tests/libclang-5/call-conv-field.rs @@ -9,7 +9,7 @@ #![cfg(not(test))] #[repr(C)] -#[derive(Copy, Clone)] +#[derive(Default, Copy, Clone)] pub struct JNINativeInterface_ { pub GetVersion: ::std::option::Option< unsafe extern "stdcall" fn(env: *mut ::std::os::raw::c_void) -> ::std::os::raw::c_int, @@ -50,11 +50,6 @@ fn bindgen_test_layout_JNINativeInterface_() { ) ); } -impl Default for JNINativeInterface_ { - fn default() -> Self { - unsafe { ::std::mem::zeroed() } - } -} extern "stdcall" { #[link_name = "\u{1}_bar@0"] pub fn bar();