Skip to content

Commit

Permalink
Auto merge of #1085 - pepyakin:builtin-clone-impls, r=fitzgen
Browse files Browse the repository at this point in the history
Derive `Clone` along with `Copy` on Rust 1.21

Fixes #934

r? @fitzgen or @emilio
  • Loading branch information
bors-servo authored Oct 24, 2017
2 parents 1831867 + 2e7d997 commit c30a805
Show file tree
Hide file tree
Showing 203 changed files with 601 additions and 2,493 deletions.
5 changes: 4 additions & 1 deletion src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1642,7 +1642,10 @@ impl CodeGenerator for CompInfo {
ctx.options().derive_copy
{
derives.push("Copy");
if used_template_params.is_some() {

if ctx.options().rust_features().builtin_clone_impls() ||
used_template_params.is_some()
{
// FIXME: This requires extra logic if you have a big array in a
// templated struct. The reason for this is that the magic:
// fn clone(&self) -> Self { *self }
Expand Down
11 changes: 10 additions & 1 deletion src/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ macro_rules! rust_target_base {
=> Stable_1_0 => 1.0;
/// Rust stable 1.19
=> Stable_1_19 => 1.19;
/// Rust stable 1.21
=> Stable_1_21 => 1.21;
/// Nightly rust
=> Nightly => nightly;
);
Expand All @@ -100,7 +102,7 @@ rust_target_base!(rust_target_def);
rust_target_base!(rust_target_values_def);

/// Latest stable release of Rust
pub const LATEST_STABLE_RUST: RustTarget = RustTarget::Stable_1_19;
pub const LATEST_STABLE_RUST: RustTarget = RustTarget::Stable_1_21;

/// Create RustFeatures struct definition, new(), and a getter for each field
macro_rules! rust_feature_def {
Expand Down Expand Up @@ -142,6 +144,8 @@ rust_feature_def!(
=> const_fn;
/// `thiscall` calling convention ([Tracking issue](https://github.com/rust-lang/rust/issues/42202))
=> thiscall_abi;
/// builtin impls for `Clone` ([PR](https://github.com/rust-lang/rust/pull/43690))
=> builtin_clone_impls;
);

impl From<RustTarget> for RustFeatures {
Expand All @@ -152,6 +156,10 @@ impl From<RustTarget> for RustFeatures {
features.untagged_union = true;
}

if rust_target >= RustTarget::Stable_1_21 {
features.builtin_clone_impls = true;
}

if rust_target >= RustTarget::Nightly {
features.const_fn = true;
features.thiscall_abi = true;
Expand Down Expand Up @@ -183,6 +191,7 @@ mod test {
fn str_to_target() {
test_target("1.0", RustTarget::Stable_1_0);
test_target("1.19", RustTarget::Stable_1_19);
test_target("1.21", RustTarget::Stable_1_21);
test_target("nightly", RustTarget::Nightly);
}
}
49 changes: 7 additions & 42 deletions tests/expectations/tests/16-byte-alignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@


#[repr(C)]
#[derive(Copy)]
#[derive(Copy, Clone)]
pub struct rte_ipv4_tuple {
pub src_addr: u32,
pub dst_addr: u32,
pub __bindgen_anon_1: rte_ipv4_tuple__bindgen_ty_1,
}
#[repr(C)]
#[derive(Copy)]
#[derive(Copy, Clone)]
pub union rte_ipv4_tuple__bindgen_ty_1 {
pub __bindgen_anon_1: rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1,
pub sctp_tag: u32,
_bindgen_union_align: u32,
}
#[repr(C)]
#[derive(Debug, Default, Copy, Hash, PartialEq, Eq)]
#[derive(Debug, Default, Copy, Clone, Hash, PartialEq, Eq)]
pub struct rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1 {
pub dport: u16,
pub sport: u16,
Expand Down Expand Up @@ -67,11 +67,6 @@ fn bindgen_test_layout_rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1() {
)
);
}
impl Clone for rte_ipv4_tuple__bindgen_ty_1__bindgen_ty_1 {
fn clone(&self) -> Self {
*self
}
}
#[test]
fn bindgen_test_layout_rte_ipv4_tuple__bindgen_ty_1() {
assert_eq!(
Expand All @@ -95,11 +90,6 @@ fn bindgen_test_layout_rte_ipv4_tuple__bindgen_ty_1() {
)
);
}
impl Clone for rte_ipv4_tuple__bindgen_ty_1 {
fn clone(&self) -> Self {
*self
}
}
impl Default for rte_ipv4_tuple__bindgen_ty_1 {
fn default() -> Self {
unsafe { ::std::mem::zeroed() }
Expand Down Expand Up @@ -138,32 +128,27 @@ fn bindgen_test_layout_rte_ipv4_tuple() {
)
);
}
impl Clone for rte_ipv4_tuple {
fn clone(&self) -> Self {
*self
}
}
impl Default for rte_ipv4_tuple {
fn default() -> Self {
unsafe { ::std::mem::zeroed() }
}
}
#[repr(C)]
#[derive(Copy)]
#[derive(Copy, Clone)]
pub struct rte_ipv6_tuple {
pub src_addr: [u8; 16usize],
pub dst_addr: [u8; 16usize],
pub __bindgen_anon_1: rte_ipv6_tuple__bindgen_ty_1,
}
#[repr(C)]
#[derive(Copy)]
#[derive(Copy, Clone)]
pub union rte_ipv6_tuple__bindgen_ty_1 {
pub __bindgen_anon_1: rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1,
pub sctp_tag: u32,
_bindgen_union_align: u32,
}
#[repr(C)]
#[derive(Debug, Default, Copy, Hash, PartialEq, Eq)]
#[derive(Debug, Default, Copy, Clone, Hash, PartialEq, Eq)]
pub struct rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1 {
pub dport: u16,
pub sport: u16,
Expand Down Expand Up @@ -211,11 +196,6 @@ fn bindgen_test_layout_rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1() {
)
);
}
impl Clone for rte_ipv6_tuple__bindgen_ty_1__bindgen_ty_1 {
fn clone(&self) -> Self {
*self
}
}
#[test]
fn bindgen_test_layout_rte_ipv6_tuple__bindgen_ty_1() {
assert_eq!(
Expand All @@ -239,11 +219,6 @@ fn bindgen_test_layout_rte_ipv6_tuple__bindgen_ty_1() {
)
);
}
impl Clone for rte_ipv6_tuple__bindgen_ty_1 {
fn clone(&self) -> Self {
*self
}
}
impl Default for rte_ipv6_tuple__bindgen_ty_1 {
fn default() -> Self {
unsafe { ::std::mem::zeroed() }
Expand Down Expand Up @@ -282,18 +257,13 @@ fn bindgen_test_layout_rte_ipv6_tuple() {
)
);
}
impl Clone for rte_ipv6_tuple {
fn clone(&self) -> Self {
*self
}
}
impl Default for rte_ipv6_tuple {
fn default() -> Self {
unsafe { ::std::mem::zeroed() }
}
}
#[repr(C)]
#[derive(Copy)]
#[derive(Copy, Clone)]
pub union rte_thash_tuple {
pub v4: rte_ipv4_tuple,
pub v6: rte_ipv6_tuple,
Expand Down Expand Up @@ -327,11 +297,6 @@ fn bindgen_test_layout_rte_thash_tuple() {
)
);
}
impl Clone for rte_thash_tuple {
fn clone(&self) -> Self {
*self
}
}
impl Default for rte_thash_tuple {
fn default() -> Self {
unsafe { ::std::mem::zeroed() }
Expand Down
42 changes: 6 additions & 36 deletions tests/expectations/tests/accessors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


#[repr(C)]
#[derive(Debug, Default, Copy)]
#[derive(Debug, Default, Copy, Clone)]
pub struct SomeAccessors {
pub mNoAccessor: ::std::os::raw::c_int,
/// <div rustbindgen accessor></div>
Expand Down Expand Up @@ -68,11 +68,6 @@ fn bindgen_test_layout_SomeAccessors() {
)
);
}
impl Clone for SomeAccessors {
fn clone(&self) -> Self {
*self
}
}
impl SomeAccessors {
#[inline]
pub fn get_mBothAccessors(&self) -> &::std::os::raw::c_int {
Expand All @@ -97,7 +92,7 @@ impl SomeAccessors {
}
/// <div rustbindgen accessor></div>
#[repr(C)]
#[derive(Debug, Default, Copy)]
#[derive(Debug, Default, Copy, Clone)]
pub struct AllAccessors {
pub mBothAccessors: ::std::os::raw::c_int,
pub mAlsoBothAccessors: ::std::os::raw::c_int,
Expand Down Expand Up @@ -135,11 +130,6 @@ fn bindgen_test_layout_AllAccessors() {
)
);
}
impl Clone for AllAccessors {
fn clone(&self) -> Self {
*self
}
}
impl AllAccessors {
#[inline]
pub fn get_mBothAccessors(&self) -> &::std::os::raw::c_int {
Expand All @@ -160,7 +150,7 @@ impl AllAccessors {
}
/// <div rustbindgen accessor="unsafe"></div>
#[repr(C)]
#[derive(Debug, Default, Copy)]
#[derive(Debug, Default, Copy, Clone)]
pub struct AllUnsafeAccessors {
pub mBothAccessors: ::std::os::raw::c_int,
pub mAlsoBothAccessors: ::std::os::raw::c_int,
Expand Down Expand Up @@ -198,11 +188,6 @@ fn bindgen_test_layout_AllUnsafeAccessors() {
)
);
}
impl Clone for AllUnsafeAccessors {
fn clone(&self) -> Self {
*self
}
}
impl AllUnsafeAccessors {
#[inline]
pub unsafe fn get_mBothAccessors(&self) -> &::std::os::raw::c_int {
Expand All @@ -223,7 +208,7 @@ impl AllUnsafeAccessors {
}
/// <div rustbindgen accessor></div>
#[repr(C)]
#[derive(Debug, Default, Copy)]
#[derive(Debug, Default, Copy, Clone)]
pub struct ContradictAccessors {
pub mBothAccessors: ::std::os::raw::c_int,
/// <div rustbindgen accessor="false"></div>
Expand Down Expand Up @@ -286,11 +271,6 @@ fn bindgen_test_layout_ContradictAccessors() {
)
);
}
impl Clone for ContradictAccessors {
fn clone(&self) -> Self {
*self
}
}
impl ContradictAccessors {
#[inline]
pub fn get_mBothAccessors(&self) -> &::std::os::raw::c_int {
Expand All @@ -315,7 +295,7 @@ impl ContradictAccessors {
}
/// <div rustbindgen accessor replaces="Replaced"></div>
#[repr(C)]
#[derive(Debug, Default, Copy)]
#[derive(Debug, Default, Copy, Clone)]
pub struct Replaced {
pub mAccessor: ::std::os::raw::c_int,
}
Expand All @@ -342,11 +322,6 @@ fn bindgen_test_layout_Replaced() {
)
);
}
impl Clone for Replaced {
fn clone(&self) -> Self {
*self
}
}
impl Replaced {
#[inline]
pub fn get_mAccessor(&self) -> &::std::os::raw::c_int {
Expand All @@ -359,7 +334,7 @@ impl Replaced {
}
/// <div rustbindgen accessor></div>
#[repr(C)]
#[derive(Debug, Default, Copy)]
#[derive(Debug, Default, Copy, Clone)]
pub struct Wrapper {
pub mReplaced: Replaced,
}
Expand All @@ -386,11 +361,6 @@ fn bindgen_test_layout_Wrapper() {
)
);
}
impl Clone for Wrapper {
fn clone(&self) -> Self {
*self
}
}
impl Wrapper {
#[inline]
pub fn get_mReplaced(&self) -> &Replaced {
Expand Down
14 changes: 2 additions & 12 deletions tests/expectations/tests/annotation_hide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

/// <div rustbindgen opaque></div>
#[repr(C)]
#[derive(Debug, Default, Copy)]
#[derive(Debug, Default, Copy, Clone)]
pub struct D {
pub _bindgen_opaque_blob: u32,
}
Expand All @@ -24,13 +24,8 @@ fn bindgen_test_layout_D() {
concat!("Alignment of ", stringify!(D))
);
}
impl Clone for D {
fn clone(&self) -> Self {
*self
}
}
#[repr(C)]
#[derive(Debug, Default, Copy)]
#[derive(Debug, Default, Copy, Clone)]
pub struct NotAnnotated {
pub f: ::std::os::raw::c_int,
}
Expand All @@ -57,8 +52,3 @@ fn bindgen_test_layout_NotAnnotated() {
)
);
}
impl Clone for NotAnnotated {
fn clone(&self) -> Self {
*self
}
}
7 changes: 1 addition & 6 deletions tests/expectations/tests/anon_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


#[repr(C)]
#[derive(Debug, Default, Copy, PartialEq)]
#[derive(Debug, Default, Copy, Clone, PartialEq)]
pub struct Test {
pub foo: ::std::os::raw::c_int,
pub bar: f32,
Expand Down Expand Up @@ -49,11 +49,6 @@ fn bindgen_test_layout_Test() {
)
);
}
impl Clone for Test {
fn clone(&self) -> Self {
*self
}
}
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum Baz {
Expand Down
7 changes: 1 addition & 6 deletions tests/expectations/tests/anon_enum_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub enum DataType__bindgen_ty_1 {
generic_type = 0,
}
#[repr(C)]
#[derive(Debug, Default, Copy, Hash, PartialEq, Eq)]
#[derive(Debug, Default, Copy, Clone, Hash, PartialEq, Eq)]
pub struct Foo {
pub _address: u8,
}
Expand All @@ -48,8 +48,3 @@ fn bindgen_test_layout_Foo() {
concat!("Alignment of ", stringify!(Foo))
);
}
impl Clone for Foo {
fn clone(&self) -> Self {
*self
}
}
Loading

0 comments on commit c30a805

Please sign in to comment.