Skip to content

Commit a68b9e5

Browse files
committed
Rewrite the special traits section
1 parent d25e350 commit a68b9e5

17 files changed

+178
-56
lines changed

src/SUMMARY.md

+1-7
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,7 @@
6767
- [Type coercions](type-coercions.md)
6868
- [Destructors](destructors.md)
6969

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

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

src/dynamically-sized-types.md

+1-1
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

+8-6
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ evaluated in the order given by their associativity.
4444
| `=` `+=` `-=` `*=` `/=` `%=` <br> `&=` <code>&#124;=</code> `^=` `<<=` `>>=` | right to left |
4545
| `return` `break` closures | |
4646

47-
## Place Expressions and Value Expressions
47+
## Place Expressions and Value Expressions
4848

4949
Expressions are divided into two main categories: place expressions and
5050
value expressions. Likewise within each expression, sub-expressions may occur
@@ -72,15 +72,14 @@ expression context. All other expression contexts are value expression contexts.
7272

7373
When a place expression is evaluated in a value expression context, or is bound
7474
by value in a pattern, it denotes the value held _in_ that memory location. If
75-
the type of that value implements `Copy`, then the value will be copied. In the
76-
remaining situations if that type is [`Sized`](the-sized-trait.html), then it
77-
may be possible to move the value. Only the following place expressions may be
78-
moved out of:
75+
the type of that value implements [`Copy`], then the value will be copied. In
76+
the remaining situations if that type is [`Sized`], then it may be possible to
77+
move the value. Only the following place expressions may be moved out of:
7978

8079
* [Variables] which are not currently borrowed.
8180
* [Temporary values](#temporary-lifetimes).
8281
* [Fields][field] of a place expression which can be moved out of and
83-
doesn't implement [`Drop`](the-drop-trait.html).
82+
doesn't implement [`Drop`].
8483
* The result of [dereferencing] an expression with type [`Box<T>`] and that can
8584
also be moved out of.
8685

@@ -281,6 +280,9 @@ exist in `core::ops` and `core::cmp` with the same names.
281280
[destructors]: destructors.html
282281
[interior mutability]: interior-mutability.html
283282
[`Box<T>`]: ../std/boxed/struct.Box.html
283+
[`Copy`]: special-types-and-traits.html#copy
284+
[`Drop`]: special-types-and-traits.html#drop
285+
[`Sized`]: special-types-and-traits.html#sized
284286
[implicit borrow]: #implicit-borrows
285287
[implicitly mutably borrowed]: #implicit-borrows
286288
[let]: statements.html#let-statements

src/expressions/array-expr.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ such as a [literal](tokens.html#literals) or a [constant
2020
item](items/constant-items.html). `[a; b]` creates an array containing `b`
2121
copies of the value of `a`. If the expression after the semi-colon has a value
2222
greater than 1 then this requires that the type of `a` is
23-
[`Copy`](the-copy-trait.html).
23+
[`Copy`](special-types-and-traits.html#copy).
2424

2525
```rust
2626
[1, 2, 3, 4];

src/expressions/closure-expr.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@ closure's type is `'static`.
3737
The compiler will determine which of the [closure
3838
traits](types.html#closure-types) the closure's type will implement by how it
3939
acts on its captured variables. The closure will also implement
40-
[`Send`](the-send-trait.html) and/or [`Sync`](the-sync-trait.html) if all of
41-
its captured types do. These traits allow functions to accept closures using
42-
generics, even though the exact types can't be named.
40+
[`Send`](special-types-and-traits.html#send) and/or
41+
[`Sync`](special-types-and-traits.html#sync) if all of its captured types do.
42+
These traits allow functions to accept closures using generics, even though the
43+
exact types can't be named.
4344

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

src/expressions/field-expr.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ possible. In cases of ambiguity, we prefer fewer autoderefs to more.
2727

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

3434
```rust
3535
struct A { f1: String, f2: String, f3: String }

src/items/constant-items.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Constant items
22

3-
> **<sup>Syntax</sup>**
4-
> _ConstantItem_ :
5-
> &nbsp;&nbsp; `const` [IDENTIFIER] `:` [_Type_] `=` [_Expression_] `;`
3+
> **<sup>Syntax</sup>**
4+
> _ConstantItem_ :
5+
> &nbsp;&nbsp; `const` [IDENTIFIER] `:` [_Type_] `=` [_Expression_] `;`
66
77
A *constant item* is a named _[constant value]_ which is not associated with a
88
specific memory location in the program. Constants are essentially inlined
@@ -62,7 +62,7 @@ fn create_and_drop_zero_with_destructor() {
6262

6363
[constant value]: expressions.html#constant-expressions
6464
[static lifetime elision]: items/static-items.html#static-lifetime-elision
65-
[`Drop`]: the-drop-trait.html
65+
[`Drop`]: special-types-and-traits.html#drop
6666
[IDENTIFIER]: identifiers.html
6767
[_Type_]: types.html
6868
[_Expression_]: expressions.html

src/special-traits.md

-3
This file was deleted.

src/special-types-and-traits.md

+154
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
# Special types and traits
2+
3+
Certain types and traits that exist in [the standard library] are known to the
4+
Rust compiler. This chapter documents the special features of these types and
5+
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 a place 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+
* A trait may be implemented for `Box<T>` in the same crate as `T`, which the
17+
[orphan rules] prevent for other generic types.
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], [drop check] and
30+
[auto traits](#auto-traits).
31+
32+
## Operator Traits
33+
34+
The traits in [`std::ops`] and [`std::cmp`] are used to overload [operators],
35+
[indexing expressions] and [call expressions].
36+
37+
## `Deref` and `DerefMut`
38+
39+
As well as overloading the unary `*` operator, [`Deref`] and [`DerefMut`] are
40+
also used in [method resolution] and [deref coercions].
41+
42+
## `Drop`
43+
44+
The [`Drop`] trait provides a [destructor], to be run whenever a value of this
45+
type is to be destroyed.
46+
47+
## `Copy`
48+
49+
The [`Copy`] trait changes the semantics of a type implementing it. Values
50+
whose type implements `Copy` are copied rather than moved upon assignment.
51+
`Copy` cannot be implemented for types which implement `Drop`, or which have
52+
fields that are not `Copy`. `Copy` is implemented by the compiler for
53+
54+
* [Numeric types]
55+
* `char` and `bool`
56+
* [Tuples] of `Copy` types
57+
* [Arrays] of `Copy` types
58+
* [Shared references]
59+
* [Raw pointers]
60+
* [Function pointers] and [function item types]
61+
62+
## `Clone`
63+
64+
The [`Clone`] trait is a supertrait of `Copy`, so it also needs compiler
65+
generated implementations. It is implemented by the compiler for the following
66+
types:
67+
68+
* Types with a built-in `Copy` implementation (see above)
69+
* [Tuples] of `Clone` types
70+
* [Arrays] of `Clone` types
71+
72+
## `Send`
73+
74+
The [`Send`] trait indicates that a value of this type is safe to send from one
75+
thread to another.
76+
77+
## `Sync`
78+
79+
The [`Sync`] trait indicates that a value of this type is safe to share between
80+
multiple threads. This trait must be implemented for all types used in
81+
immutable [`static` items].
82+
83+
## Auto traits
84+
85+
The [`Send`], [`Sync`], [`UnwindSafe`] and [`RefUnwindSafe`] traits are _auto
86+
traits_. Auto traits have special properties.
87+
88+
First, auto traits are automatically implemented using the following rules:
89+
90+
* `&T`, `&mut T`, `*const T`, `*mut T`, `[T; n]` and `[T]` implement the trait
91+
if `T` does.
92+
* Function item types and function pointers automatically implement the trait.
93+
* Structs, enums, unions and tuples implement the trait if all of their fields
94+
do.
95+
* Closures implement the trait if the types of all of their captures do. A
96+
closure that captures a `T` by shared reference and a `U` by value implements
97+
any auto traits that both `&T` and `U` do.
98+
99+
Auto traits can also have negative implementations, shown as `impl !AutoTrait
100+
for T` in the standard library documentation, that override the automatic
101+
implementations. For example `*mut T` has a negative implementation of `Send`,
102+
and so `*mut T` and `(*mut T,)` are not `Send`. Finally, auto traits may
103+
be added as a bound to any [trait object]\: `Box<Debug + Send + UnwindSafe>` is
104+
a valid type.
105+
106+
## `Sized`
107+
108+
The [`Sized`] trait indicates that the size of this type is known at
109+
compile-time; that is, it's not a [dynamically sized type]. [Type parameters]
110+
are `Sized` by default. `Sized` is always implemented automatically by the
111+
compiler, not by [implementation items].
112+
113+
[`Box<T>`]: ../std/boxed/struct.Box.html
114+
[`Clone`]: ../std/clone/trait.Clone.html
115+
[`Copy`]: ../std/marker/trait.Copy.html
116+
[`Deref`]: ../std/ops/trait.Deref.html
117+
[`DerefMut`]: ../std/ops/trait.DerefMut.html
118+
[`Drop`]: ../std/ops/trait.Drop.html
119+
[`RefUnwindSafe`]: ../std/panic/trait.RefUnwindSafe.html
120+
[`Send`]: ../std/marker/trait.Send.html
121+
[`Sized`]: ../std/marker/trait.Sized.html
122+
[`std::cell::UnsafeCell<T>`]: ../std/cell/struct.UnsafeCell.html
123+
[`std::cmp`]: ../std/cmp/index.html
124+
[`std::marker::PhantomData<T>`]: ../std/marker/struct.PhantomData.html
125+
[`std::ops`]: ../std/ops/index.html
126+
[`UnwindSafe`]: ../std/panic/trait.UnwindSafe.html
127+
[`Sync`]: ../std/marker/trait.Sync.html
128+
129+
[Arrays]: types.html#array-and-slice-types
130+
[call expressions]: expressoins/call-expr.html
131+
[deref coercions]: type-coercions.html#coercion-types
132+
[dereference operator]: expressions/operator-expr.html#the-dereference-operator
133+
[destructor]: destructors.html
134+
[drop check]: ../nomicon/dropck.html
135+
[dynamically sized type]: dynamically-sized-types.html
136+
[Function pointers]: types.html#function-pointer-types
137+
[function item types]: types.html#function-item-types
138+
[implementation items]: items.html/implementations.html
139+
[indexing expressions]: expressions/array-expr.html#array-and-slice-indexing-expressions
140+
[interior mutability]: iterior-mutability.html
141+
[lang items]: attributes.html#language-items
142+
[Numeric types]: types.html#numeric-types
143+
[Methods]: items.html#associated-functions-and-methods
144+
[method resolution]: expressions/method-call-expr.html
145+
[operators]: expressions/operator-expr.html
146+
[orphan rules]: items/implementations.html#trait-implementation-coherence
147+
[Raw pointers]: types.html#raw-pointers-const-and-mut
148+
[`static` items]: items/static-items.html
149+
[Shared references]: types.html#shared-references-
150+
[the standard library]: ../std/index.html
151+
[trait object]: types.html#trait-objects
152+
[Tuples]: types.html#tuple-types
153+
[Type parameters]: types.html#type-parameters
154+
[variance]: ../nomicon/subtyping.html

src/the-copy-trait.md

-4
This file was deleted.

src/the-deref-trait.md

-7
This file was deleted.

src/the-drop-trait.md

-4
This file was deleted.

src/the-send-trait.md

-4
This file was deleted.

src/the-sized-trait.md

-3
This file was deleted.

src/the-sync-trait.md

-4
This file was deleted.

src/types.md

+1-1
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

+1-1
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)