diff options
| author | ian <me@iany.me> | 2018-12-26 13:01:30 +0800 |
|---|---|---|
| committer | ian <me@iany.me> | 2018-12-31 21:22:50 +0800 |
| commit | bbc8c932fb05dfaed7b5bfe252712bede722b764 (patch) | |
| tree | 28962fab0be47cf686c5177ac5d771c426a781cc /src/libcore | |
| parent | 14b96659e4e9b2f31431df48f47c219957e2666a (diff) | |
| download | rust-bbc8c932fb05dfaed7b5bfe252712bede722b764.tar.gz rust-bbc8c932fb05dfaed7b5bfe252712bede722b764.zip | |
Fix inconsistent Clone documentation.
Use function pointer as the example to demonstrate how to implement Clone for Copy types. refs #57123
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/clone.rs | 29 |
1 files changed, 20 insertions, 9 deletions
diff --git a/src/libcore/clone.rs b/src/libcore/clone.rs index 225ea3de9cd..5cf4eedaef9 100644 --- a/src/libcore/clone.rs +++ b/src/libcore/clone.rs @@ -63,6 +63,17 @@ /// This trait can be used with `#[derive]` if all fields are `Clone`. The `derive`d /// implementation of [`clone`] calls [`clone`] on each field. /// +/// For a generic struct, `#[derive]` implements `Clone` conditionally by adding bound `Clone` on +/// generic parameters. +/// +/// ``` +/// // `derive` implements Clone for Reading<T> when T is Clone. +/// #[derive(Clone)] +/// struct Reading<T> { +/// frequency: T, +/// } +/// ``` +/// /// ## How can I implement `Clone`? /// /// Types that are [`Copy`] should have a trivial implementation of `Clone`. More formally: @@ -70,21 +81,21 @@ /// Manual implementations should be careful to uphold this invariant; however, unsafe code /// must not rely on it to ensure memory safety. /// -/// An example is an array holding more than 32 elements of a type that is `Clone`; the standard -/// library only implements `Clone` up until arrays of size 32. In this case, the implementation of -/// `Clone` cannot be `derive`d, but can be implemented as: +/// An example is a generic struct holding a function pointer. In this case, the +/// implementation of `Clone` cannot be `derive`d, but can be implemented as: /// /// [`Copy`]: ../../std/marker/trait.Copy.html /// [`clone`]: trait.Clone.html#tymethod.clone /// /// ``` -/// #[derive(Copy)] -/// struct Stats { -/// frequencies: [i32; 100], -/// } +/// struct Generate<T>(fn() -> T); +/// +/// impl<T> Copy for Generate<T> {} /// -/// impl Clone for Stats { -/// fn clone(&self) -> Stats { *self } +/// impl<T> Clone for Generate<T> { +/// fn clone(&self) -> Self { +/// *self +/// } /// } /// ``` /// |
