Skip to content

Commit b2b7a06

Browse files
committed
Auto merge of #57125 - doitian:inconsistent-clone-doc, r=bluss
Fix inconsistent Clone documentation. Now, arrays of any size Clone if the element type is Clone. So remove the the document that uses this as an example. refs #57123
2 parents cae1647 + bbc8c93 commit b2b7a06

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

src/libcore/clone.rs

+20-9
Original file line numberDiff line numberDiff line change
@@ -53,28 +53,39 @@
5353
/// This trait can be used with `#[derive]` if all fields are `Clone`. The `derive`d
5454
/// implementation of [`clone`] calls [`clone`] on each field.
5555
///
56+
/// For a generic struct, `#[derive]` implements `Clone` conditionally by adding bound `Clone` on
57+
/// generic parameters.
58+
///
59+
/// ```
60+
/// // `derive` implements Clone for Reading<T> when T is Clone.
61+
/// #[derive(Clone)]
62+
/// struct Reading<T> {
63+
/// frequency: T,
64+
/// }
65+
/// ```
66+
///
5667
/// ## How can I implement `Clone`?
5768
///
5869
/// Types that are [`Copy`] should have a trivial implementation of `Clone`. More formally:
5970
/// if `T: Copy`, `x: T`, and `y: &T`, then `let x = y.clone();` is equivalent to `let x = *y;`.
6071
/// Manual implementations should be careful to uphold this invariant; however, unsafe code
6172
/// must not rely on it to ensure memory safety.
6273
///
63-
/// An example is an array holding more than 32 elements of a type that is `Clone`; the standard
64-
/// library only implements `Clone` up until arrays of size 32. In this case, the implementation of
65-
/// `Clone` cannot be `derive`d, but can be implemented as:
74+
/// An example is a generic struct holding a function pointer. In this case, the
75+
/// implementation of `Clone` cannot be `derive`d, but can be implemented as:
6676
///
6777
/// [`Copy`]: ../../std/marker/trait.Copy.html
6878
/// [`clone`]: trait.Clone.html#tymethod.clone
6979
///
7080
/// ```
71-
/// #[derive(Copy)]
72-
/// struct Stats {
73-
/// frequencies: [i32; 100],
74-
/// }
81+
/// struct Generate<T>(fn() -> T);
82+
///
83+
/// impl<T> Copy for Generate<T> {}
7584
///
76-
/// impl Clone for Stats {
77-
/// fn clone(&self) -> Stats { *self }
85+
/// impl<T> Clone for Generate<T> {
86+
/// fn clone(&self) -> Self {
87+
/// *self
88+
/// }
7889
/// }
7990
/// ```
8091
///

0 commit comments

Comments
 (0)