Skip to content

Commit 6c023b9

Browse files
committed
Rewrite the special traits section
1 parent 9732f63 commit 6c023b9

17 files changed

+146
-51
lines changed

src/SUMMARY.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,7 @@
6868
- [Type coercions](type-coercions.md)
6969
- [Destructors](destructors.md)
7070

71-
- [Special traits](special-traits.md)
72-
- [The Copy trait](the-copy-trait.md)
73-
- [The Sized trait](the-sized-trait.md)
74-
- [The Drop trait](the-drop-trait.md)
75-
- [The Deref trait](the-deref-trait.md)
76-
- [The Send trait](the-send-trait.md)
77-
- [The Sync trait](the-sync-trait.md)
71+
- [Special types and traits](special-types-and-traits.md)
7872

7973
- [Memory model](memory-model.md)
8074
- [Memory allocation and lifetime](memory-allocation-and-lifetime.md)

src/dynamically-sized-types.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ types">DSTs</abbr>. Such types can only be used in certain cases:
2323
Notably: [variables], function parameters, [const] and [static] items must be
2424
`Sized`.
2525

26-
[sized]: the-sized-trait.html
26+
[sized]: special-types-and-traits.html#sized
2727
[Slices]: types.html#array-and-slice-types
2828
[trait objects]: types.html#trait-objects
2929
[Pointer types]: types.html#pointer-types

src/expressions.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,14 @@ other expression contexts are rvalue contexts.
6868
When an lvalue is evaluated in an _rvalue context_ or is bound by value in a
6969
pattern, it denotes the value held _in_ that memory location. If value is of a
7070
type that implements `Copy`, then the value will be copied. In the remaining
71-
situations if the type of the value is [`Sized`](the-sized-trait.html) it may
72-
be possible to move the value. Only the following lvalues may be moved out of:
71+
situations if the type of the value is
72+
[`Sized`](special-types-and-traits.html#sized) it may be possible to move the
73+
value. Only the following lvalues may be moved out of:
7374

7475
* [Variables](variables.html) which are not currently borrowed.
7576
* [Temporary values](#temporary-lifetimes).
7677
* [Field]s of an lvalue which can be moved out of and
77-
doesn't implement [`Drop`](the-drop-trait.html).
78+
doesn't implement [`Drop`](special-types-and-traits.html#drop).
7879
* The result of [dereferencing] an expression with type `Box<T>` and that can
7980
also be moved out of.
8081

@@ -216,7 +217,8 @@ also constant expressions:
216217
Recursively defining constants is not allowed.
217218
* [Tuple expressions].
218219
* [Array expressions].
219-
* [Struct] expressions, where the type does not implement [`Drop`](the-drop-trait.html).
220+
* [Struct] expressions, where the type does not implement
221+
[`Drop`](special-types-and-traits.html#drop).
220222
* [Enum variant] expressions, where the enumeration type does not implement `Drop`.
221223
* [Block expressions]&nbsp;(and `unsafe` blocks) which only contain items and
222224
possibly a (constant) tail expression.

src/expressions/array-expr.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ such as a [literal](tokens.html#literals) or a [constant
1414
item](items/constant-items.html). `[a; b]` creates an array containing `b`
1515
copies of the value of `a`. If the expression after the semi-colon has a value
1616
greater than 1 then this requires that the type of `a` is
17-
[`Copy`](the-copy-trait.html).
17+
[`Copy`](special-types-and-traits.html#copy).
1818

1919
```rust
2020
[1, 2, 3, 4];

src/expressions/closure-expr.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ closure's type is `'static`.
3131
The compiler will determine which of the [closure
3232
traits](types.html#closure-types) the closure's type will implement by how it
3333
acts on its captured variables. The closure will also implement
34-
[`Send`](the-send-trait.html) and/or [`Sync`](the-sync-trait.html) if all of
35-
its captured types do. These traits allow functions to accept closures using
36-
generics, even though the exact types can't be named.
34+
[`Send`](special-types-and-traits.html#send) and/or
35+
[`Sync`](special-types-and-traits.html#sync) if all of its captured types do.
36+
These traits allow functions to accept closures using generics, even though the
37+
exact types can't be named.
3738

3839
In this example, we define a function `ten_times` that takes a higher-order
3940
function argument, and we then call it with a closure expression as an argument,

src/expressions/field-expr.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ possible. In cases of ambiguity, we prefer fewer autoderefs to more.
2525

2626
Finally, the fields of a struct or a reference to a struct are treated as
2727
separate entities when borrowing. If the struct does not implement
28-
[`Drop`](the-drop-trait.html) and is stored in a local variable, this also
29-
applies to moving out of each of its fields. This also does not apply if
30-
automatic dereferencing is done though user defined types.
28+
[`Drop`](special-types-and-traits.html#drop) and is stored in a local variable,
29+
this also applies to moving out of each of its fields. This also does not apply
30+
if automatic dereferencing is done though user defined types.
3131

3232
```rust
3333
struct A { f1: String, f2: String, f3: String }

src/items/constant-items.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@ const BITS_N_STRINGS: BitsNStrings<'static> = BitsNStrings {
3737

3838
[constant value]: expressions.html#constant-expressions
3939
[static lifetime elision]: items/static-items.html#static-lifetime-elision
40-
[`Drop`]: the-drop-trait.html
40+
[`Drop`]: special-types-and-traits.html#drop

src/special-traits.md

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/special-types-and-traits.md

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# Special types and traits
2+
3+
Certain types and traits that exist in [the standard library] are known to the
4+
Rust compiler (usually by [lang items]). This chapter documents the special
5+
features of these types and traits.
6+
7+
## `Box<T>`
8+
9+
[`Box<T>`] has a few special features that Rust doesn't currently allow for user
10+
defined types.
11+
12+
* The [dereference operator] for `Box<T>` produces an lvalue which can be moved
13+
from. This means that the `*` operator and the destructor of `Box<T>` are
14+
built in to the language.
15+
* [Methods] can take `Box<Self>` as a receiver.
16+
* For the purpose of the [orphan rules], `Box<T>` is treated as being defined in
17+
the same crate as `T` (rather than in `alloc`).
18+
19+
## `UnsafeCell<T>`
20+
21+
[`std::cell::UnsafeCell<T>`] is used for [interior mutability]. It ensures that
22+
the compiler doesn't perform optimisations that are incorrect for such types.
23+
It also ensures that [`static` items] which have a type with interior
24+
mutability aren't placed in memory marked as read only.
25+
26+
## `PhantomData<T>`
27+
28+
[`std::marker::PhantomData<T>`] is a zero-sized, minimum alignment, type that
29+
is considered to own a `T` for the purposes of [variance] and [drop check].
30+
31+
## Operator Traits
32+
33+
The traits in [`std::ops`] and [`std::cmp`] are used to overload [operators],
34+
[indexing expressions] and [call expressions].
35+
36+
## `Deref` and `DerefMut`
37+
38+
As well as overloading the unary `*` operator, [`Deref`] and [`DerefMut`] are
39+
also used in [method resolution] and [deref coercions].
40+
41+
## `Drop`
42+
43+
The [`Drop`] trait provides a [destructor], to be run whenever a value of this
44+
type is to be destroyed.
45+
46+
## `Copy`
47+
48+
The [`Copy`] trait changes the semantics of a type implementing it. Values
49+
whose type implements `Copy` are copied rather than moved upon assignment.
50+
`Copy` cannot be implemented for types which implement `Drop`, or which have
51+
fields that are not `Copy`. `Copy` is implemented by the compiler for
52+
53+
* [Numeric types]
54+
* `char` and `bool`
55+
* [Tuples] of `Copy` types
56+
* [Arrays] of `Copy` types
57+
* [Shared references]
58+
* [Raw pointers]
59+
* [Function pointers] and [function item types]
60+
61+
## `Clone`
62+
63+
The [`Clone`] trait is implemented by the compiler for the following types:
64+
65+
* Types with a built-in `Copy` implementation (see above)
66+
* [Tuples] of `Clone` types
67+
* [Arrays] of `Clone` types
68+
69+
## `Send`
70+
71+
The [`Send`] trait indicates that a value of this type is safe to send from one
72+
thread to another. This trait is automatically implemented for types that
73+
only contain types which are `Send`. This trait can be added as a bound to any
74+
[trait object].
75+
76+
## `Sync`
77+
78+
The [`Sync`] trait indicates that a value of this type is safe to share between
79+
multiple threads, that is a shared reference to this type is `Send`. This trait
80+
is automatically implemented for types that only contain types which are
81+
`Send`. This trait must be implemented for all types used in [`static` items].
82+
This trait can be added as a bound to any [trait object].
83+
84+
## `Sized`
85+
86+
The [`Sized`] trait indicates that the size of this type is known at
87+
compile-time, that is, it's not a [dynamically sized type]. [Type parameters]
88+
are `Sized` by default. `Sized` is always implemented automatically by the
89+
compiler, not by [implementation items].
90+
91+
[the standard library]: ../std/index.html
92+
[lang items]: attributes.html#language-items
93+
[`Box<T>`]: ../std/boxed/struct.Box.html
94+
[dereference operator]: expressions/operator-expr.html#the-dereference-operator
95+
[Methods]: items.html#associated-functions-and-methods
96+
[`std::cell::UnsafeCell<T>`]: ../std/cell/struct.UnsafeCell.html
97+
[interior mutability]: iterior-mutability.html
98+
[`std::marker::PhantomData<T>`]: ../std/marker/struct.PhantomData.html
99+
[variance]: ../nomicon/subtyping.html
100+
[drop check]: ../nomicon/dropck.html
101+
[operators]: expressions/operator-expr.html
102+
[`std::ops`]: ../std/ops/index.html
103+
[`std::cmp`]: ../std/cmp/index.html
104+
[indexing expressions]: expressions/array-expr.html#array-and-slice-indexing-expressions
105+
[call expressions]: expressoins/call-expr.html
106+
[`Deref`]: ../std/ops/trait.Deref.html
107+
[`DerefMut`]: ../std/ops/trait.DerefMut.html
108+
[method resolution]: expressions/method-call-expr.html
109+
[deref coercions]: type-coercions.html#coercion-types
110+
[`Drop`]: ../std/ops/trait.Drop.html
111+
[destructor]: destructors.html
112+
[`Copy`]: ../std/marker/trait.Copy.html
113+
[Numeric types]: types.html#numeric-types
114+
[Tuples]: types.html#tuple-types
115+
[Arrays]: types.html#array-and-slice-types
116+
[Shared references]: types.html#shared-references-
117+
[Raw pointers]: types.html#raw-pointers-const-and-mut
118+
[Function pointers]: types.html#function-pointer-types
119+
[function item types]: types.html#function-item-types
120+
[`Clone`]: ../std/clone/trait.Clone.html
121+
[`Send`]: ../std/marker/trait.Send.html
122+
[`static` items]: items/static-items.html
123+
[`Sync`]: ../std/marker/trait.Sync.html
124+
[`Sized`]: ../std/marker/trait.Sized.html
125+
[dynamically sized type]: dynamically-sized-types.html
126+
[Type parameters]: types.html#type-parameters
127+
[implementation items]: items.html/implementations.html

src/the-copy-trait.md

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/the-deref-trait.md

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/the-drop-trait.md

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/the-send-trait.md

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/the-sized-trait.md

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/the-sync-trait.md

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/types.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ to read from a union field or to write to a field that doesn't implement
215215
The memory layout of a `union` is undefined by default, but the `#[repr(...)]`
216216
attribute can be used to fix a layout.
217217

218-
[`Copy`]: the-copy-trait.html
218+
[`Copy`]: special-types-and-traits.html#copy
219219

220220
## Recursive types
221221

src/unsafety.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ Rust:
99
- Dereferencing a [raw pointer](types.html#pointer-types).
1010
- Reading or writing a [mutable static variable](items/static-items.html#mutable-statics).
1111
- Reading a field of a [`union`](items/unions.html), or writing to a field of a
12-
union that isn't [`Copy`](the-copy-trait.html).
12+
union that isn't [`Copy`](special-types-and-traits.html#copy).
1313
- Calling an unsafe function (including an intrinsic or foreign function).
1414
- Implementing an unsafe trait.

0 commit comments

Comments
 (0)