diff options
| author | Nick Hamann <nick@wabbo.org> | 2015-05-12 21:47:25 -0500 |
|---|---|---|
| committer | Nick Hamann <nick@wabbo.org> | 2015-05-12 23:35:27 -0500 |
| commit | b2f486feafa99124479d1b5e74c9acc6ed7cc233 (patch) | |
| tree | b62ac27c6c18afe0e0bac45df3af51ebeb2500a0 /src | |
| parent | 857a12a01ed8a9249cea8f48b39a0f8bdfd95cbd (diff) | |
| download | rust-b2f486feafa99124479d1b5e74c9acc6ed7cc233.tar.gz rust-b2f486feafa99124479d1b5e74c9acc6ed7cc233.zip | |
Improve wording for the "Trait objects" section of the reference.
Diffstat (limited to 'src')
| -rw-r--r-- | src/doc/reference.md | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/src/doc/reference.md b/src/doc/reference.md index 03b55309836..76446876160 100644 --- a/src/doc/reference.md +++ b/src/doc/reference.md @@ -3510,13 +3510,21 @@ more of the closure traits: ### Trait objects -Every trait item (see [traits](#traits)) defines a type with the same name as -the trait. This type is called the _trait object_ of the trait. Trait objects -permit "late binding" of methods, dispatched using _virtual method tables_ -("vtables"). Whereas most calls to trait methods are "early bound" (statically -resolved) to specific implementations at compile time, a call to a method on an -trait objects is only resolved to a vtable entry at compile time. The actual -implementation for each vtable entry can vary on an object-by-object basis. +In Rust, a type like `&SomeTrait` or `Box<SomeTrait>` is called a _trait object_. +Each instance of a trait object includes: + + - a pointer to an instance of a type `T` that implements `SomeTrait` + - a _virtual method table_, often just called a _vtable_, which contains, for + each method of `SomeTrait` that `T` implements, a pointer to `T`'s + implementation (i.e. a function pointer). + +The purpose of trait objects is to permit "late binding" of methods. A call to +a method on a trait object is only resolved to a vtable entry at compile time. +The actual implementation for each vtable entry can vary on an object-by-object +basis. + +Note that for a trait object to be instantiated, the trait must be +_object-safe_. Object safety rules are defined in [RFC 255][rfc255]. Given a pointer-typed expression `E` of type `&T` or `Box<T>`, where `T` implements trait `R`, casting `E` to the corresponding pointer type `&R` or |
