Skip to content

Commit 6ca22f8

Browse files
author
Nick Hamann
committed
Improve wording for the "Trait objects" section of the reference.
1 parent 12b2624 commit 6ca22f8

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

src/doc/reference.md

+15-7
Original file line numberDiff line numberDiff line change
@@ -3511,13 +3511,21 @@ more of the closure traits:
35113511

35123512
### Trait objects
35133513

3514-
Every trait item (see [traits](#traits)) defines a type with the same name as
3515-
the trait. This type is called the _trait object_ of the trait. Trait objects
3516-
permit "late binding" of methods, dispatched using _virtual method tables_
3517-
("vtables"). Whereas most calls to trait methods are "early bound" (statically
3518-
resolved) to specific implementations at compile time, a call to a method on an
3519-
trait objects is only resolved to a vtable entry at compile time. The actual
3520-
implementation for each vtable entry can vary on an object-by-object basis.
3514+
In Rust, a type like `&SomeTrait` or `Box<SomeTrait>` is called a _trait object_.
3515+
Each instance of a trait object includes:
3516+
3517+
- a pointer to an instance of a type `T` that implements `SomeTrait`
3518+
- a _virtual method table_, often just called a _vtable_, which contains, for
3519+
each method of `SomeTrait` that `T` implements, a pointer to `T`'s
3520+
implementation (i.e. a function pointer).
3521+
3522+
The purpose of trait objects is to permit "late binding" of methods. A call to
3523+
a method on a trait object is only resolved to a vtable entry at compile time.
3524+
The actual implementation for each vtable entry can vary on an object-by-object
3525+
basis.
3526+
3527+
Note that for a trait object to be instantiated, the trait must be
3528+
_object-safe_. Object safety rules are defined in [RFC 255][rfc255].
35213529

35223530
Given a pointer-typed expression `E` of type `&T` or `Box<T>`, where `T`
35233531
implements trait `R`, casting `E` to the corresponding pointer type `&R` or

0 commit comments

Comments
 (0)