From bbc8c932fb05dfaed7b5bfe252712bede722b764 Mon Sep 17 00:00:00 2001 From: ian Date: Wed, 26 Dec 2018 13:01:30 +0800 Subject: [PATCH] Fix inconsistent Clone documentation. Use function pointer as the example to demonstrate how to implement Clone for Copy types. refs #57123 --- src/libcore/clone.rs | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/src/libcore/clone.rs b/src/libcore/clone.rs index 225ea3de9cd6..5cf4eedaef98 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 when T is Clone. +/// #[derive(Clone)] +/// struct Reading { +/// 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(fn() -> T); /// -/// impl Clone for Stats { -/// fn clone(&self) -> Stats { *self } +/// impl Copy for Generate {} +/// +/// impl Clone for Generate { +/// fn clone(&self) -> Self { +/// *self +/// } /// } /// ``` ///