@@ -1131,7 +1131,7 @@ A _trait_ describes a set of method types.
1131
1131
Traits can include default implementations of methods,
1132
1132
written in terms of some unknown [ ` self ` type] ( #self-types ) ;
1133
1133
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.
1135
1135
1136
1136
Traits are implemented for specific types through separate [ implementations] ( #implementations ) .
1137
1137
@@ -1176,7 +1176,7 @@ fn draw_twice<T: Shape>(surface: Surface, sh: T) {
1176
1176
}
1177
1177
~~~~
1178
1178
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.
1180
1180
Values of this type are created by [ casting] ( #type-cast-expressions ) pointer values
1181
1181
(pointing to a type for which an implementation of the given trait is in scope)
1182
1182
to pointers to the trait name, used as a type.
@@ -1542,7 +1542,7 @@ method_call_expr : expr '.' ident paren_expr_list ;
1542
1542
A _ method call_ consists of an expression followed by a single dot, an identifier, and a parenthesized expression-list.
1543
1543
Method calls are resolved to methods on specific traits,
1544
1544
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 ) .
1546
1546
1547
1547
1548
1548
### Field expressions
@@ -2656,10 +2656,21 @@ let bo: Binop = add;
2656
2656
x = bo(5,7);
2657
2657
~~~~~~~~
2658
2658
2659
- ### Trait types
2659
+ ### Object types
2660
2660
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:
2663
2674
2664
2675
~~~~~~~~
2665
2676
trait Printable {
@@ -2679,8 +2690,8 @@ fn main() {
2679
2690
}
2680
2691
~~~~~~~~
2681
2692
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 ` .
2684
2695
2685
2696
### Type parameters
2686
2697
0 commit comments