Skip to content

Commit 6d4621d

Browse files
committed
doc: a little more material on object types, part of #4217.
1 parent d062896 commit 6d4621d

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

doc/rust.md

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,7 +1131,7 @@ A _trait_ describes a set of method types.
11311131
Traits can include default implementations of methods,
11321132
written in terms of some unknown [`self` type](#self-types);
11331133
the `self` type may either be completely unspecified,
1134-
or constrained by some other [trait type](#trait-types).
1134+
or constrained by some other trait.
11351135

11361136
Traits are implemented for specific types through separate [implementations](#implementations).
11371137

@@ -1176,7 +1176,7 @@ fn draw_twice<T: Shape>(surface: Surface, sh: T) {
11761176
}
11771177
~~~~
11781178

1179-
Traits also define a [type](#trait-types) with the same name as the trait.
1179+
Traits also define an [object type](#object-types) with the same name as the trait.
11801180
Values of this type are created by [casting](#type-cast-expressions) pointer values
11811181
(pointing to a type for which an implementation of the given trait is in scope)
11821182
to pointers to the trait name, used as a type.
@@ -1542,7 +1542,7 @@ method_call_expr : expr '.' ident paren_expr_list ;
15421542
A _method call_ consists of an expression followed by a single dot, an identifier, and a parenthesized expression-list.
15431543
Method calls are resolved to methods on specific traits,
15441544
either statically dispatching to a method if the exact `self`-type of the left-hand-side is known,
1545-
or dynamically dispatching if the left-hand-side expression is an indirect [trait type](#trait-types).
1545+
or dynamically dispatching if the left-hand-side expression is an indirect [object type](#object-types).
15461546

15471547

15481548
### Field expressions
@@ -2656,10 +2656,21 @@ let bo: Binop = add;
26562656
x = bo(5,7);
26572657
~~~~~~~~
26582658

2659-
### Trait types
2659+
### Object types
26602660

2661-
Every trait item (see [traits](#traits)) defines a type with the same name
2662-
as the trait. For a trait `T`, cast expressions introduce values of type `T`:
2661+
Every trait item (see [traits](#traits)) defines a type with the same name as the trait.
2662+
This type is called the _object type_ of the trait.
2663+
Object types permit "late binding" of methods, dispatched using _virtual method tables_ ("vtables").
2664+
Whereas most calls to trait methods are "early bound" (statically resolved) to specific implementations at compile time,
2665+
a call to a method on an object type is only resolved to a vtable entry at compile time.
2666+
The actual implementation for each vtable entry can vary on an object-by-object basis.
2667+
2668+
Given a pointer-typed expression `E` of type `&T`, `~T` or `@T`, where `T` implements trait `R`,
2669+
casting `E` to the corresponding pointer type `&R`, `~R` or `@R` results in a value of the _object type_ `R`.
2670+
This result is represented as a pair of pointers:
2671+
the vtable pointer for the `T` implementation of `R`, and the pointer value of `E`.
2672+
2673+
An example of an object type:
26632674

26642675
~~~~~~~~
26652676
trait Printable {
@@ -2679,8 +2690,8 @@ fn main() {
26792690
}
26802691
~~~~~~~~
26812692

2682-
In this example, the trait `Printable` occurs as a type in both the type signature of
2683-
`print`, and the cast expression in `main`.
2693+
In this example, the trait `Printable` occurs as an object type in both the type signature of `print`,
2694+
and the cast expression in `main`.
26842695

26852696
### Type parameters
26862697

0 commit comments

Comments
 (0)