Skip to content

Add bindgen::Builder::derive_copy #952

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1493,7 +1493,9 @@ impl CodeGenerator for CompInfo {
needs_default_impl = ctx.options().derive_default;
}

if item.can_derive_copy(ctx) && !item.annotations().disallow_copy() {
if item.can_derive_copy(ctx) && !item.annotations().disallow_copy() &&
ctx.options().derive_copy
{
derives.push("Copy");
if used_template_params.is_some() {
// FIXME: This requires extra logic if you have a big array in a
Expand Down
15 changes: 15 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,10 @@ impl Builder {
output_vector.push("--no-layout-tests".into());
}

if !self.options.derive_copy {
output_vector.push("--no-derive-copy".into());
}

if !self.options.derive_debug {
output_vector.push("--no-derive-debug".into());
}
Expand Down Expand Up @@ -735,6 +739,12 @@ impl Builder {
self
}

/// Set whether `Copy` should be derived by default.
pub fn derive_copy(mut self, doit: bool) -> Self {
self.options.derive_copy = doit;
self
}

/// Set whether `Debug` should be derived by default.
pub fn derive_debug(mut self, doit: bool) -> Self {
self.options.derive_debug = doit;
Expand Down Expand Up @@ -1104,6 +1114,10 @@ pub struct BindgenOptions {
/// True if we should generate layout tests for generated structures.
pub layout_tests: bool,

/// True if we should derive Copy trait implementations for C/C++ structures
/// and types.
pub derive_copy: bool,

/// True if we should derive Debug trait implementations for C/C++ structures
/// and types.
pub derive_debug: bool,
Expand Down Expand Up @@ -1269,6 +1283,7 @@ impl Default for BindgenOptions {
emit_ir: false,
emit_ir_graphviz: None,
layout_tests: true,
derive_copy: true,
derive_debug: true,
impl_debug: false,
derive_default: false,
Expand Down
7 changes: 7 additions & 0 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ where
Arg::with_name("no-layout-tests")
.long("no-layout-tests")
.help("Avoid generating layout tests for any type."),
Arg::with_name("no-derive-copy")
.long("no-derive-copy")
.help("Avoid deriving Copy on any type."),
Arg::with_name("no-derive-debug")
.long("no-derive-debug")
.help("Avoid deriving Debug on any type."),
Expand Down Expand Up @@ -311,6 +314,10 @@ where
builder = builder.layout_tests(false);
}

if matches.is_present("no-derive-copy") {
builder = builder.derive_copy(false);
}

if matches.is_present("no-derive-debug") {
builder = builder.derive_debug(false);
}
Expand Down
28 changes: 28 additions & 0 deletions tests/expectations/tests/do-not-derive-copy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* automatically generated by rust-bindgen */


#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]


#[repr(C)]
#[derive(Debug, Default)]
pub struct WouldBeCopyButWeAreNotDerivingCopy {
pub x: ::std::os::raw::c_int,
}
#[test]
fn bindgen_test_layout_WouldBeCopyButWeAreNotDerivingCopy() {
assert_eq!(::std::mem::size_of::<WouldBeCopyButWeAreNotDerivingCopy>() ,
4usize , concat ! (
"Size of: " , stringify ! ( WouldBeCopyButWeAreNotDerivingCopy
) ));
assert_eq! (::std::mem::align_of::<WouldBeCopyButWeAreNotDerivingCopy>() ,
4usize , concat ! (
"Alignment of " , stringify ! (
WouldBeCopyButWeAreNotDerivingCopy ) ));
assert_eq! (unsafe {
& ( * ( 0 as * const WouldBeCopyButWeAreNotDerivingCopy ) ) .
x as * const _ as usize } , 0usize , concat ! (
"Alignment of field: " , stringify ! (
WouldBeCopyButWeAreNotDerivingCopy ) , "::" , stringify ! ( x
) ));
}
5 changes: 5 additions & 0 deletions tests/headers/do-not-derive-copy.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// bindgen-flags: --no-derive-copy

class WouldBeCopyButWeAreNotDerivingCopy {
int x;
};