shallow Clone for #[derive(Copy,Clone)]
Changes #[derive(Copy, Clone)] to use a faster impl of Clone when
both derives are present, and there are no generics in the type.
The faster impl is simply returning *self (which works because the
type is also Copy). See the comments in libsyntax_ext/deriving/clone.rs
for more details.
There are a few types which are Copy but not Clone, in violation
of the definition of Copy. These include large arrays and tuples. The
very existence of these types is arguably a bug, but in order for this
optimization not to change the applicability of #[derive(Copy, Clone)],
the faster Clone impl also injects calls to a new function,
core::clone::assert_receiver_is_clone, to verify that all members are
actually Clone.
This is not a breaking change, because pursuant to RFC 1521, any type
that implements Copy should not do any observable work in its Clone
impl.
2016-02-03 18:40:59 -06:00
|
|
|
// this will get a no-op Clone impl
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
struct A {
|
|
|
|
a: i32,
|
|
|
|
b: i64
|
|
|
|
}
|
|
|
|
|
|
|
|
// this will get a deep Clone impl
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
struct B<T> {
|
|
|
|
a: i32,
|
|
|
|
b: T
|
|
|
|
}
|
|
|
|
|
|
|
|
struct C; // not Copy or Clone
|
|
|
|
#[derive(Clone)] struct D; // Clone but not Copy
|
|
|
|
|
|
|
|
fn is_copy<T: Copy>(_: T) {}
|
|
|
|
fn is_clone<T: Clone>(_: T) {}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
// A can be copied and cloned
|
|
|
|
is_copy(A { a: 1, b: 2 });
|
|
|
|
is_clone(A { a: 1, b: 2 });
|
|
|
|
|
|
|
|
// B<i32> can be copied and cloned
|
|
|
|
is_copy(B { a: 1, b: 2 });
|
|
|
|
is_clone(B { a: 1, b: 2 });
|
|
|
|
|
|
|
|
// B<C> cannot be copied or cloned
|
2019-10-15 20:42:27 -05:00
|
|
|
is_copy(B { a: 1, b: C }); //~ ERROR Copy
|
|
|
|
is_clone(B { a: 1, b: C }); //~ ERROR Clone
|
shallow Clone for #[derive(Copy,Clone)]
Changes #[derive(Copy, Clone)] to use a faster impl of Clone when
both derives are present, and there are no generics in the type.
The faster impl is simply returning *self (which works because the
type is also Copy). See the comments in libsyntax_ext/deriving/clone.rs
for more details.
There are a few types which are Copy but not Clone, in violation
of the definition of Copy. These include large arrays and tuples. The
very existence of these types is arguably a bug, but in order for this
optimization not to change the applicability of #[derive(Copy, Clone)],
the faster Clone impl also injects calls to a new function,
core::clone::assert_receiver_is_clone, to verify that all members are
actually Clone.
This is not a breaking change, because pursuant to RFC 1521, any type
that implements Copy should not do any observable work in its Clone
impl.
2016-02-03 18:40:59 -06:00
|
|
|
|
|
|
|
// B<D> can be cloned but not copied
|
2019-10-15 20:42:27 -05:00
|
|
|
is_copy(B { a: 1, b: D }); //~ ERROR Copy
|
shallow Clone for #[derive(Copy,Clone)]
Changes #[derive(Copy, Clone)] to use a faster impl of Clone when
both derives are present, and there are no generics in the type.
The faster impl is simply returning *self (which works because the
type is also Copy). See the comments in libsyntax_ext/deriving/clone.rs
for more details.
There are a few types which are Copy but not Clone, in violation
of the definition of Copy. These include large arrays and tuples. The
very existence of these types is arguably a bug, but in order for this
optimization not to change the applicability of #[derive(Copy, Clone)],
the faster Clone impl also injects calls to a new function,
core::clone::assert_receiver_is_clone, to verify that all members are
actually Clone.
This is not a breaking change, because pursuant to RFC 1521, any type
that implements Copy should not do any observable work in its Clone
impl.
2016-02-03 18:40:59 -06:00
|
|
|
is_clone(B { a: 1, b: D });
|
|
|
|
}
|