Skip to content

Commit f25505e

Browse files
author
Nick Hamann
committed
Improve wording for the "Trait objects" section of the reference.
1 parent 622caa2 commit f25505e

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/doc/reference.md

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3387,14 +3387,21 @@ more of the closure traits:
33873387

33883388
### Trait objects
33893389

3390-
Every trait item (see [traits](#traits)) defines a type with the same name as
3391-
the trait. Traits that are object-safe (see [RFC 255][rfc255]) can be
3392-
instantiated as _trait objects_, which permit "late binding" of methods,
3393-
dispatched using _virtual method tables_ ("vtables"). Whereas most calls to
3394-
trait methods are "early bound" (statically resolved) to specific
3395-
implementations at compile time, a call to a method on an trait objects is
3396-
only resolved to a vtable entry at compile time. The actual
3397-
implementation for each vtable entry can vary on an object-by-object basis.
3390+
In Rust, a type like `&SomeTrait` or `Box<SomeTrait>` is called a _trait object_.
3391+
Each instance of a trait object includes:
3392+
3393+
- a pointer to an instance of a type `T` that implements `SomeTrait`
3394+
- a _virtual method table_, often just called a _vtable_, which contains, for
3395+
each method of `SomeTrait` that `T` implements, a pointer to `T`'s
3396+
implementation (i.e. a function pointer).
3397+
3398+
The purpose of trait objects is to permit "late binding" of methods. A call to
3399+
a method on a trait object is only resolved to a vtable entry at compile time.
3400+
The actual implementation for each vtable entry can vary on an object-by-object
3401+
basis.
3402+
3403+
Note that for a trait object to be instantiated, the trait must be
3404+
_object-safe_. Object safety rules are defined in [RFC 255][rfc255].
33983405

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

0 commit comments

Comments
 (0)