Skip to content

Commit 8ad6d67

Browse files
author
Jethro Beekman
committed
Derive Default for function pointers
1 parent fbc5da7 commit 8ad6d67

File tree

9 files changed

+35
-69
lines changed

9 files changed

+35
-69
lines changed

src/ir/analysis/derive.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,7 @@ impl<'ctx> CannotDerive<'ctx> {
194194
let inner_type =
195195
self.ctx.resolve_type(inner).canonical_type(self.ctx);
196196
if let TypeKind::Function(ref sig) = *inner_type.kind() {
197-
if let DeriveTrait::Default = self.derive_trait {
198-
return CanDerive::No
199-
} else {
200-
return self.derive_trait.can_derive_fnptr(sig)
201-
}
197+
return self.derive_trait.can_derive_fnptr(sig)
202198
} else {
203199
return self.derive_trait.can_derive_pointer();
204200
}

tests/expectations/tests/derive-fn-ptr.rs

+8-15
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
/* automatically generated by rust-bindgen */
22

3-
4-
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
5-
3+
#![allow(
4+
dead_code,
5+
non_snake_case,
6+
non_camel_case_types,
7+
non_upper_case_globals
8+
)]
69

710
pub type my_fun_t = ::std::option::Option<
811
unsafe extern "C" fn(
@@ -25,7 +28,7 @@ pub type my_fun_t = ::std::option::Option<
2528
),
2629
>;
2730
#[repr(C)]
28-
#[derive(Copy, Clone)]
31+
#[derive(Default, Copy, Clone)]
2932
pub struct Foo {
3033
pub callback: my_fun_t,
3134
}
@@ -52,11 +55,6 @@ fn bindgen_test_layout_Foo() {
5255
)
5356
);
5457
}
55-
impl Default for Foo {
56-
fn default() -> Self {
57-
unsafe { ::std::mem::zeroed() }
58-
}
59-
}
6058
pub type my_fun2_t = ::std::option::Option<
6159
unsafe extern "C" fn(
6260
arg1: ::std::os::raw::c_int,
@@ -74,7 +72,7 @@ pub type my_fun2_t = ::std::option::Option<
7472
),
7573
>;
7674
#[repr(C)]
77-
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
75+
#[derive(Debug, Default, Copy, Clone, Hash, PartialEq, Eq)]
7876
pub struct Bar {
7977
pub callback: my_fun2_t,
8078
}
@@ -101,8 +99,3 @@ fn bindgen_test_layout_Bar() {
10199
)
102100
);
103101
}
104-
impl Default for Bar {
105-
fn default() -> Self {
106-
unsafe { ::std::mem::zeroed() }
107-
}
108-
}

tests/expectations/tests/func_ptr_in_struct.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
/* automatically generated by rust-bindgen */
22

3-
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
3+
#![allow(
4+
dead_code,
5+
non_snake_case,
6+
non_camel_case_types,
7+
non_upper_case_globals
8+
)]
49

510
#[repr(i32)]
611
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
712
pub enum baz {
813
__bindgen_cannot_repr_c_on_empty_enum = 0,
914
}
1015
#[repr(C)]
11-
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
16+
#[derive(Debug, Default, Copy, Clone, Hash, PartialEq, Eq)]
1217
pub struct Foo {
1318
pub bar: ::std::option::Option<
1419
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() {
3237
concat!("Offset of field: ", stringify!(Foo), "::", stringify!(bar))
3338
);
3439
}
35-
impl Default for Foo {
36-
fn default() -> Self {
37-
unsafe { ::std::mem::zeroed() }
38-
}
39-
}

tests/expectations/tests/issue-1216-variadic-member.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
/* automatically generated by rust-bindgen */
22

3-
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
3+
#![allow(
4+
dead_code,
5+
non_snake_case,
6+
non_camel_case_types,
7+
non_upper_case_globals
8+
)]
49

510
extern "C" {
611
pub fn f(a: ::std::os::raw::c_int, ...);
712
}
813
#[repr(C)]
9-
#[derive(Debug, Copy, Clone)]
14+
#[derive(Debug, Default, Copy, Clone)]
1015
pub struct Foo {
1116
pub f: ::std::option::Option<
1217
unsafe extern "C" fn(
@@ -35,8 +40,3 @@ fn bindgen_test_layout_Foo() {
3540
concat!("Offset of field: ", stringify!(Foo), "::", stringify!(f))
3641
);
3742
}
38-
impl Default for Foo {
39-
fn default() -> Self {
40-
unsafe { ::std::mem::zeroed() }
41-
}
42-
}

tests/expectations/tests/layout_cmdline_token.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ pub type cmdline_parse_token_hdr_t = cmdline_token_hdr;
7272
/// get_help() fills the dstbuf with the help for the token. It returns
7373
/// -1 on error and 0 on success.
7474
#[repr(C)]
75-
#[derive(Debug, Copy, Clone)]
75+
#[derive(Debug, Default, Copy, Clone)]
7676
pub struct cmdline_token_ops {
7777
/// parse(token ptr, buf, res pts, buf len)
7878
pub parse: ::std::option::Option<
@@ -162,11 +162,6 @@ fn bindgen_test_layout_cmdline_token_ops() {
162162
)
163163
);
164164
}
165-
impl Default for cmdline_token_ops {
166-
fn default() -> Self {
167-
unsafe { ::std::mem::zeroed() }
168-
}
169-
}
170165
#[repr(u32)]
171166
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
172167
pub enum cmdline_numtype {

tests/expectations/tests/libclang-3.8/call-conv-field.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
/* automatically generated by rust-bindgen */
22

3-
4-
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
3+
#![allow(
4+
dead_code,
5+
non_snake_case,
6+
non_camel_case_types,
7+
non_upper_case_globals
8+
)]
59
#![cfg(not(test))]
610

7-
811
#[repr(C)]
9-
#[derive(Copy, Clone)]
12+
#[derive(Default, Copy, Clone)]
1013
pub struct JNINativeInterface_ {
1114
pub GetVersion: ::std::option::Option<
12-
unsafe extern "stdcall" fn(env: *mut ::std::os::raw::c_void)
13-
-> ::std::os::raw::c_int,
15+
unsafe extern "stdcall" fn(env: *mut ::std::os::raw::c_void) -> ::std::os::raw::c_int,
1416
>,
1517
pub __hack: ::std::os::raw::c_ulonglong,
1618
}
@@ -47,11 +49,6 @@ fn bindgen_test_layout_JNINativeInterface_() {
4749
)
4850
);
4951
}
50-
impl Default for JNINativeInterface_ {
51-
fn default() -> Self {
52-
unsafe { ::std::mem::zeroed() }
53-
}
54-
}
5552
extern "C" {
5653
#[link_name = "\u{1}_bar@0"]
5754
pub fn bar();

tests/expectations/tests/libclang-3.9/call-conv-field.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77

88
#[repr(C)]
9-
#[derive(Copy, Clone)]
9+
#[derive(Default, Copy, Clone)]
1010
pub struct JNINativeInterface_ {
1111
pub GetVersion: ::std::option::Option<
1212
unsafe extern "stdcall" fn(env: *mut ::std::os::raw::c_void)
@@ -47,11 +47,6 @@ fn bindgen_test_layout_JNINativeInterface_() {
4747
)
4848
);
4949
}
50-
impl Default for JNINativeInterface_ {
51-
fn default() -> Self {
52-
unsafe { ::std::mem::zeroed() }
53-
}
54-
}
5550
extern "stdcall" {
5651
#[link_name = "\u{1}_bar@0"]
5752
pub fn bar();

tests/expectations/tests/libclang-4/call-conv-field.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77

88
#[repr(C)]
9-
#[derive(Copy, Clone)]
9+
#[derive(Default, Copy, Clone)]
1010
pub struct JNINativeInterface_ {
1111
pub GetVersion: ::std::option::Option<
1212
unsafe extern "stdcall" fn(env: *mut ::std::os::raw::c_void)
@@ -47,11 +47,6 @@ fn bindgen_test_layout_JNINativeInterface_() {
4747
)
4848
);
4949
}
50-
impl Default for JNINativeInterface_ {
51-
fn default() -> Self {
52-
unsafe { ::std::mem::zeroed() }
53-
}
54-
}
5550
extern "stdcall" {
5651
#[link_name = "\u{1}_bar@0"]
5752
pub fn bar();

tests/expectations/tests/libclang-5/call-conv-field.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#![cfg(not(test))]
1010

1111
#[repr(C)]
12-
#[derive(Copy, Clone)]
12+
#[derive(Default, Copy, Clone)]
1313
pub struct JNINativeInterface_ {
1414
pub GetVersion: ::std::option::Option<
1515
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_() {
5050
)
5151
);
5252
}
53-
impl Default for JNINativeInterface_ {
54-
fn default() -> Self {
55-
unsafe { ::std::mem::zeroed() }
56-
}
57-
}
5853
extern "stdcall" {
5954
#[link_name = "\u{1}_bar@0"]
6055
pub fn bar();

0 commit comments

Comments
 (0)