Skip to content

Commit 48dc0ba

Browse files
Improve Copy trait doc
1 parent 3d28927 commit 48dc0ba

File tree

1 file changed

+35
-16
lines changed

1 file changed

+35
-16
lines changed

src/libcore/marker.rs

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ pub trait Unsize<T: ?Sized> {
126126
/// }
127127
/// ```
128128
///
129-
/// The `PointList` `struct` cannot implement `Copy`, because `Vec<T>` is not `Copy`. If we
129+
/// The `PointList` `struct` cannot implement `Copy`, because [`Vec<T>`] is not `Copy`. If we
130130
/// attempt to derive a `Copy` implementation, we'll get an error:
131131
///
132132
/// ```text
@@ -136,10 +136,10 @@ pub trait Unsize<T: ?Sized> {
136136
/// ## When can my type _not_ be `Copy`?
137137
///
138138
/// Some types can't be copied safely. For example, copying `&mut T` would create an aliased
139-
/// mutable reference, and copying `String` would result in two attempts to free the same buffer.
139+
/// mutable reference, and copying [`String`] would result in two attempts to free the same buffer.
140140
///
141-
/// Generalizing the latter case, any type implementing `Drop` can't be `Copy`, because it's
142-
/// managing some resource besides its own `size_of::<T>()` bytes.
141+
/// Generalizing the latter case, any type implementing [`Drop`] can't be `Copy`, because it's
142+
/// managing some resource besides its own [`size_of::<T>()`] bytes.
143143
///
144144
/// ## What if I derive `Copy` on a type that can't?
145145
///
@@ -156,8 +156,7 @@ pub trait Unsize<T: ?Sized> {
156156
///
157157
/// ## Derivable
158158
///
159-
/// This trait can be used with `#[derive]` if all of its components implement `Copy` and the type
160-
/// implements `Clone`. The implementation will copy the bytes of each field using `memcpy`.
159+
/// This trait can be used with `#[derive]` if all of its components implement `Copy` and the type.
161160
///
162161
/// ## How can I implement `Copy`?
163162
///
@@ -178,6 +177,11 @@ pub trait Unsize<T: ?Sized> {
178177
///
179178
/// There is a small difference between the two: the `derive` strategy will also place a `Copy`
180179
/// bound on type parameters, which isn't always desired.
180+
///
181+
/// [`Vec<T>`]: ../../std/vec/struct.Vec.html
182+
/// [`String`]: ../../std/string/struct.String.html
183+
/// [`Drop`]: ../../std/ops/trait.Drop.html
184+
/// [`size_of::<T>()`]: ../../std/mem/fn.size_of.html
181185
#[stable(feature = "rust1", since = "1.0.0")]
182186
#[lang = "copy"]
183187
pub trait Copy : Clone {
@@ -190,11 +194,11 @@ pub trait Copy : Clone {
190194
/// thread-safe. In other words, there is no possibility of data races
191195
/// when passing `&T` references between threads.
192196
///
193-
/// As one would expect, primitive types like `u8` and `f64` are all
197+
/// As one would expect, primitive types like [`u8`] and [`f64`] are all
194198
/// `Sync`, and so are simple aggregate types containing them (like
195199
/// tuples, structs and enums). More instances of basic `Sync` types
196200
/// include "immutable" types like `&T` and those with simple
197-
/// inherited mutability, such as `Box<T>`, `Vec<T>` and most other
201+
/// inherited mutability, such as [`Box<T>`], [`Vec<T>`] and most other
198202
/// collection types. (Generic parameters need to be `Sync` for their
199203
/// container to be `Sync`.)
200204
///
@@ -206,27 +210,42 @@ pub trait Copy : Clone {
206210
/// race.
207211
///
208212
/// Types that are not `Sync` are those that have "interior
209-
/// mutability" in a non-thread-safe way, such as `Cell` and `RefCell`
210-
/// in `std::cell`. These types allow for mutation of their contents
213+
/// mutability" in a non-thread-safe way, such as [`Cell`] and [`RefCell`]
214+
/// in [`std::cell`]. These types allow for mutation of their contents
211215
/// even when in an immutable, aliasable slot, e.g. the contents of
212-
/// `&Cell<T>` can be `.set`, and do not ensure data races are
216+
/// [`&Cell<T>`][`Cell`] can be [`.set`], and do not ensure data races are
213217
/// impossible, hence they cannot be `Sync`. A higher level example
214218
/// of a non-`Sync` type is the reference counted pointer
215-
/// `std::rc::Rc`, because any reference `&Rc<T>` can clone a new
219+
/// [`std::rc::Rc`][`Rc`], because any reference [`&Rc<T>`][`Rc`] can clone a new
216220
/// reference, which modifies the reference counts in a non-atomic
217221
/// way.
218222
///
219223
/// For cases when one does need thread-safe interior mutability,
220-
/// types like the atomics in `std::sync` and `Mutex` & `RWLock` in
221-
/// the `sync` crate do ensure that any mutation cannot cause data
224+
/// types like the atomics in [`std::sync`][`sync`] and [`Mutex`] / [`RwLock`] in
225+
/// the [`sync`] crate do ensure that any mutation cannot cause data
222226
/// races. Hence these types are `Sync`.
223227
///
224-
/// Any types with interior mutability must also use the `std::cell::UnsafeCell`
228+
/// Any types with interior mutability must also use the [`std::cell::UnsafeCell`]
225229
/// wrapper around the value(s) which can be mutated when behind a `&`
226230
/// reference; not doing this is undefined behavior (for example,
227-
/// `transmute`-ing from `&T` to `&mut T` is invalid).
231+
/// [`transmute`]-ing from `&T` to `&mut T` is invalid).
228232
///
229233
/// This trait is automatically derived when the compiler determines it's appropriate.
234+
///
235+
/// [`u8`]: ../../std/primitive.u8.html
236+
/// [`f64`]: ../../std/primitive.f64.html
237+
/// [`Vec<T>`]: ../../std/vec/struct.Vec.html
238+
/// [`Box<T>`]: ../../std/boxed/struct.Box.html
239+
/// [`Cell`]: ../../std/cell/struct.Cell.html
240+
/// [`RefCell`]: ../../std/cell/struct.RefCell.html
241+
/// [`std::cell`]: ../../std/cell/index.html
242+
/// [`.set`]: ../../std/cell/struct.Cell.html#method.set
243+
/// [`Rc`]: ../../std/rc/struct.Rc.html
244+
/// [`sync`]: ../../std/sync/index.html
245+
/// [`Mutex`]: ../../std/sync/struct.Mutex.html
246+
/// [`RwLock`]: ../../std/sync/struct.RwLock.html
247+
/// [`std::cell::UnsafeCell`]: ../../std/cell/struct.UnsafeCell.html
248+
/// [`transmute`]: ../../std/mem/fn.transmute.html
230249
#[stable(feature = "rust1", since = "1.0.0")]
231250
#[lang = "sync"]
232251
#[rustc_on_unimplemented = "`{Self}` cannot be shared between threads safely"]

0 commit comments

Comments
 (0)