Skip to content

Commit f48c043

Browse files
committed
Document builtin implementations of Clone and Copy
There are types that implement `Clone` and `Copy` but are not mentioned in the documentation, because the implementations are provided by the compiler. They are types of variants that cannot be fully covered by trait implementations in Rust code, because the language is not expressive enough.
1 parent 27164fa commit f48c043

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

src/libcore/clone.rs

+17-5
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,6 @@
6363
/// This trait can be used with `#[derive]` if all fields are `Clone`. The `derive`d
6464
/// implementation of [`clone`] calls [`clone`] on each field.
6565
///
66-
/// ## Closures
67-
///
68-
/// Closure types automatically implement `Clone` if they capture no value from the environment
69-
/// or if all such captured values implement `Clone` themselves.
70-
///
7166
/// ## How can I implement `Clone`?
7267
///
7368
/// Types that are [`Copy`] should have a trivial implementation of `Clone`. More formally:
@@ -92,6 +87,23 @@
9287
/// fn clone(&self) -> Stats { *self }
9388
/// }
9489
/// ```
90+
///
91+
/// ## Additional implementors
92+
///
93+
/// In addition to the [implementors listed below][impls],
94+
/// the following types also implement `Clone`:
95+
///
96+
/// * Function item types (i.e. the distinct types defined for each function)
97+
/// * Function pointer types (e.g. `fn() -> i32`)
98+
/// * Array types, for all sizes, if the item type also implements `Clone` (e.g. `[i32; 123456]`)
99+
/// * Tuple types, if each component also implements `Clone` (e.g. `()`, `(i32, bool)`)
100+
/// * Closure types, if they capture no value from the environment
101+
/// or if all such captured values implement `Clone` themselves.
102+
/// Note that variables captured by shared reference always implement `Clone`
103+
/// (even if the referent doesn't),
104+
/// while variables captured by mutable reference never implement `Clone`.
105+
///
106+
/// [impls]: #implementors
95107
#[stable(feature = "rust1", since = "1.0.0")]
96108
#[lang = "clone"]
97109
pub trait Clone : Sized {

src/libcore/marker.rs

+16-5
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,6 @@ pub trait Unsize<T: ?Sized> {
166166
/// are allowed to access `x` after the assignment. Under the hood, both a copy and a move
167167
/// can result in bits being copied in memory, although this is sometimes optimized away.
168168
///
169-
/// ## Closures
170-
///
171-
/// Closure types automatically implement `Copy` if they capture no value from the environment
172-
/// or if all such captured values implement `Copy` themselves.
173-
///
174169
/// ## How can I implement `Copy`?
175170
///
176171
/// There are two ways to implement `Copy` on your type. The simplest is to use `derive`:
@@ -265,13 +260,29 @@ pub trait Unsize<T: ?Sized> {
265260
/// non-`Copy` in the future, it could be prudent to omit the `Copy` implementation now, to
266261
/// avoid a breaking API change.
267262
///
263+
/// ## Additional implementors
264+
///
265+
/// In addition to the [implementors listed below][impls],
266+
/// the following types also implement `Copy`:
267+
///
268+
/// * Function item types (i.e. the distinct types defined for each function)
269+
/// * Function pointer types (e.g. `fn() -> i32`)
270+
/// * Array types, for all sizes, if the item type also implements `Copy` (e.g. `[i32; 123456]`)
271+
/// * Tuple types, if each component also implements `Copy` (e.g. `()`, `(i32, bool)`)
272+
/// * Closure types, if they capture no value from the environment
273+
/// or if all such captured values implement `Copy` themselves.
274+
/// Note that variables captured by shared reference always implement `Copy`
275+
/// (even if the referent doesn't),
276+
/// while variables captured by mutable reference never implement `Copy`.
277+
///
268278
/// [`Vec<T>`]: ../../std/vec/struct.Vec.html
269279
/// [`String`]: ../../std/string/struct.String.html
270280
/// [`Drop`]: ../../std/ops/trait.Drop.html
271281
/// [`size_of::<T>`]: ../../std/mem/fn.size_of.html
272282
/// [`Clone`]: ../clone/trait.Clone.html
273283
/// [`String`]: ../../std/string/struct.String.html
274284
/// [`i32`]: ../../std/primitive.i32.html
285+
/// [impls]: #implementors
275286
#[stable(feature = "rust1", since = "1.0.0")]
276287
#[lang = "copy"]
277288
pub trait Copy : Clone {

0 commit comments

Comments
 (0)