Skip to content

Commit 1e49750

Browse files
author
bors-servo
authored
Auto merge of rust-lang#952 - mchlrhw:master, r=fitzgen
Add `bindgen::Builder::derive_copy` Fixes rust-lang#948
2 parents b209d42 + 537a49e commit 1e49750

File tree

5 files changed

+58
-1
lines changed

5 files changed

+58
-1
lines changed

src/codegen/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1493,7 +1493,9 @@ impl CodeGenerator for CompInfo {
14931493
needs_default_impl = ctx.options().derive_default;
14941494
}
14951495

1496-
if item.can_derive_copy(ctx) && !item.annotations().disallow_copy() {
1496+
if item.can_derive_copy(ctx) && !item.annotations().disallow_copy() &&
1497+
ctx.options().derive_copy
1498+
{
14971499
derives.push("Copy");
14981500
if used_template_params.is_some() {
14991501
// FIXME: This requires extra logic if you have a big array in a

src/lib.rs

+15
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,10 @@ impl Builder {
253253
output_vector.push("--no-layout-tests".into());
254254
}
255255

256+
if !self.options.derive_copy {
257+
output_vector.push("--no-derive-copy".into());
258+
}
259+
256260
if !self.options.derive_debug {
257261
output_vector.push("--no-derive-debug".into());
258262
}
@@ -735,6 +739,12 @@ impl Builder {
735739
self
736740
}
737741

742+
/// Set whether `Copy` should be derived by default.
743+
pub fn derive_copy(mut self, doit: bool) -> Self {
744+
self.options.derive_copy = doit;
745+
self
746+
}
747+
738748
/// Set whether `Debug` should be derived by default.
739749
pub fn derive_debug(mut self, doit: bool) -> Self {
740750
self.options.derive_debug = doit;
@@ -1104,6 +1114,10 @@ pub struct BindgenOptions {
11041114
/// True if we should generate layout tests for generated structures.
11051115
pub layout_tests: bool,
11061116

1117+
/// True if we should derive Copy trait implementations for C/C++ structures
1118+
/// and types.
1119+
pub derive_copy: bool,
1120+
11071121
/// True if we should derive Debug trait implementations for C/C++ structures
11081122
/// and types.
11091123
pub derive_debug: bool,
@@ -1269,6 +1283,7 @@ impl Default for BindgenOptions {
12691283
emit_ir: false,
12701284
emit_ir_graphviz: None,
12711285
layout_tests: true,
1286+
derive_copy: true,
12721287
derive_debug: true,
12731288
impl_debug: false,
12741289
derive_default: false,

src/options.rs

+7
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ where
6161
Arg::with_name("no-layout-tests")
6262
.long("no-layout-tests")
6363
.help("Avoid generating layout tests for any type."),
64+
Arg::with_name("no-derive-copy")
65+
.long("no-derive-copy")
66+
.help("Avoid deriving Copy on any type."),
6467
Arg::with_name("no-derive-debug")
6568
.long("no-derive-debug")
6669
.help("Avoid deriving Debug on any type."),
@@ -311,6 +314,10 @@ where
311314
builder = builder.layout_tests(false);
312315
}
313316

317+
if matches.is_present("no-derive-copy") {
318+
builder = builder.derive_copy(false);
319+
}
320+
314321
if matches.is_present("no-derive-debug") {
315322
builder = builder.derive_debug(false);
316323
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
4+
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
5+
6+
7+
#[repr(C)]
8+
#[derive(Debug, Default)]
9+
pub struct WouldBeCopyButWeAreNotDerivingCopy {
10+
pub x: ::std::os::raw::c_int,
11+
}
12+
#[test]
13+
fn bindgen_test_layout_WouldBeCopyButWeAreNotDerivingCopy() {
14+
assert_eq!(::std::mem::size_of::<WouldBeCopyButWeAreNotDerivingCopy>() ,
15+
4usize , concat ! (
16+
"Size of: " , stringify ! ( WouldBeCopyButWeAreNotDerivingCopy
17+
) ));
18+
assert_eq! (::std::mem::align_of::<WouldBeCopyButWeAreNotDerivingCopy>() ,
19+
4usize , concat ! (
20+
"Alignment of " , stringify ! (
21+
WouldBeCopyButWeAreNotDerivingCopy ) ));
22+
assert_eq! (unsafe {
23+
& ( * ( 0 as * const WouldBeCopyButWeAreNotDerivingCopy ) ) .
24+
x as * const _ as usize } , 0usize , concat ! (
25+
"Alignment of field: " , stringify ! (
26+
WouldBeCopyButWeAreNotDerivingCopy ) , "::" , stringify ! ( x
27+
) ));
28+
}

tests/headers/do-not-derive-copy.hpp

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// bindgen-flags: --no-derive-copy
2+
3+
class WouldBeCopyButWeAreNotDerivingCopy {
4+
int x;
5+
};

0 commit comments

Comments
 (0)