Skip to content

Commit 818cbdc

Browse files
Merge pull request rust-lang#172 from Havvy/trait-objects
Small changes around trait objects
2 parents 345c49e + 83dfc24 commit 818cbdc

File tree

3 files changed

+22
-17
lines changed

3 files changed

+22
-17
lines changed

src/glossary.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ unsafe Rust can create such a memory without initializing it.
7070
Types that can be referred to by a path directly. Specifically [enums],
7171
[structs], [unions], and [trait objects].
7272

73+
### Object Safe Traits
74+
75+
[Traits] that can be used as [trait objects]. Only traits that follow specifc
76+
[rules][object safety] are object safe.
77+
7378
### Prelude
7479

7580
Prelude, or The Rust Prelude, is a small collection of items - mostly traits - that are
@@ -131,4 +136,6 @@ Generic functions and generic structs can use traits to constrain, or bound, the
131136
[unions]: items/unions.html
132137
[trait objects]: types.html#trait-objects
133138
[implementations]: items/implementations.html
134-
[traits]: items/traits.html
139+
[traits]: items/traits.html
140+
[object safety]: types.html#object-safety
141+
[trait objects]: types.html#trait-objects

src/items/traits.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,6 @@ pointer of trait type. For example, `&T` could be coerced to `&Shape` if `T:
223223
Shape` holds (and similarly for `Box<T>`). This coercion can either be implicit
224224
or [explicit]. Here is an example of an explicit coercion:
225225

226-
[trait object]: types.html#trait-objects
227-
[explicit]: expressions/operator-expr.html#type-cast-expressions
228-
229226
```rust
230227
trait Shape { }
231228
impl Shape for i32 { }
@@ -239,12 +236,10 @@ Values with a trait type can have [methods called] on them, for any method in
239236
the trait, and can be used to instantiate type parameters that are bounded by
240237
the trait.
241238

242-
[methods called]: expressions/method-call-expr.html
243-
244239
## Supertraits
245240

246241
Trait bounds on `Self` are considered "supertraits". These are required to be
247-
acyclic. Supertraits are somewhat different from other constraints in that
242+
acyclic. Supertraits are somewhat different from other constraints in that
248243
they affect what methods are available in the vtable when the trait is used as
249244
a [trait object]. Consider the following example:
250245

@@ -306,3 +301,7 @@ Likewise, supertrait methods may also be called on trait objects.
306301
let mycircle = Box::new(mycircle) as Box<Circle>;
307302
let nonsense = mycircle.radius() * mycircle.area();
308303
```
304+
305+
[trait object]: types.html#trait-objects
306+
[explicit]: expressions/operator-expr.html#type-cast-expressions
307+
[methods called]: expressions/method-call-expr.html

src/types.md

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -405,10 +405,8 @@ x = bo(5,7);
405405

406406
In Rust, trait names also refer to [dynamically sized types] called _trait
407407
objects_. Like all <abbr title="dynamically sized types">DSTs</abbr>, trait
408-
objects are used behind some kind of pointer: `&SomeTrait` or `Box<SomeTrait>`.
409-
Each instance of a pointer to a trait object includes:
410-
411-
[dynamically sized types]: dynamically-sized-types.html
408+
objects are used behind some type of pointer; for example `&SomeTrait` or
409+
`Box<SomeTrait>`. Each instance of a pointer to a trait object includes:
412410

413411
- a pointer to an instance of a type `T` that implements `SomeTrait`
414412
- a _virtual method table_, often just called a _vtable_, which contains, for
@@ -421,9 +419,8 @@ function pointer is loaded from the trait object vtable and invoked indirectly.
421419
The actual implementation for each vtable entry can vary on an object-by-object
422420
basis.
423421

424-
Note that trait object types only exist for _object-safe_ traits ([RFC 255]):
425-
426-
[RFC 255]: https://github.com/rust-lang/rfcs/blob/master/text/0255-object-safety.md
422+
Note that trait object types only exist for
423+
<span id="object-safety">*object-safe*</span> traits ([RFC 255]):
427424

428425
* It must not require `Self: Sized`
429426
* All associated functions must either have a `where Self: Sized` bound or
@@ -467,9 +464,6 @@ need to be expressed as part of the trait object. The assumed lifetime of
467464
references held by a trait object is called its _default object lifetime bound_.
468465
These were defined in [RFC 599] and amended in [RFC 1156].
469466

470-
[RFC 599]: https://github.com/rust-lang/rfcs/blob/master/text/0599-default-object-bound.md
471-
[RFC 1156]: https://github.com/rust-lang/rfcs/blob/master/text/1156-adjust-default-object-bounds.md
472-
473467
For traits that themselves have no lifetime parameters:
474468
* If there is a unique bound from the containing type then that is the default
475469
* If there is more than one bound from the containing type then an explicit bound must
@@ -580,3 +574,8 @@ impl Printable for String {
580574
```
581575

582576
The notation `&self` is a shorthand for `self: &Self`.
577+
578+
[dynamically sized types]: dynamically-sized-types.html
579+
[RFC 599]: https://github.com/rust-lang/rfcs/blob/master/text/0599-default-object-bound.md
580+
[RFC 1156]: https://github.com/rust-lang/rfcs/blob/master/text/1156-adjust-default-object-bounds.md
581+
[RFC 255]: https://github.com/rust-lang/rfcs/blob/master/text/0255-object-safety.md

0 commit comments

Comments
 (0)