Skip to content

Commit

Permalink
Derive Default for function pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
Jethro Beekman committed Mar 21, 2019
1 parent 37dce66 commit 6647d38
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 69 deletions.
6 changes: 1 addition & 5 deletions src/ir/analysis/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,7 @@ impl<'ctx> CannotDeriveInner<'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();
}
Expand Down
23 changes: 8 additions & 15 deletions tests/expectations/tests/derive-fn-ptr.rs
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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,
}
Expand All @@ -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,
Expand All @@ -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,
}
Expand All @@ -101,8 +99,3 @@ fn bindgen_test_layout_Bar() {
)
);
}
impl Default for Bar {
fn default() -> Self {
unsafe { ::std::mem::zeroed() }
}
}
14 changes: 7 additions & 7 deletions tests/expectations/tests/func_ptr_in_struct.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
/* 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)]
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,
Expand All @@ -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() }
}
}
14 changes: 7 additions & 7 deletions tests/expectations/tests/issue-1216-variadic-member.rs
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -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() }
}
}
7 changes: 1 addition & 6 deletions tests/expectations/tests/layout_cmdline_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<
Expand Down Expand Up @@ -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 {
Expand Down
19 changes: 8 additions & 11 deletions tests/expectations/tests/libclang-3.8/call-conv-field.rs
Original file line number Diff line number Diff line change
@@ -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,
}
Expand Down Expand Up @@ -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();
Expand Down
7 changes: 1 addition & 6 deletions tests/expectations/tests/libclang-3.9/call-conv-field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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();
Expand Down
7 changes: 1 addition & 6 deletions tests/expectations/tests/libclang-4/call-conv-field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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();
Expand Down
7 changes: 1 addition & 6 deletions tests/expectations/tests/libclang-5/call-conv-field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 6647d38

Please sign in to comment.