Skip to content

Commit d92b716

Browse files
committed
---
yaml --- r: 36810 b: refs/heads/try2 c: 6d4621d h: refs/heads/master v: v3
1 parent 0021d72 commit d92b716

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: eb8fd119c65c67f3b1b8268cc7341c22d39b7b61
55
refs/heads/try: d324a424d8f84b1eb049b12cf34182bda91b0024
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: d062896a8bb224f97c9bac804d40b5406a3ac993
8+
refs/heads/try2: 6d4621d85fe7854d43f9f4208326b6fc9cd04d4d
99
refs/heads/incoming: d9317a174e434d4c99fc1a37fd7dc0d2f5328d37
1010
refs/heads/dist-snap: 22efa39382d41b084fde1719df7ae8ce5697d8c9
1111
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596

branches/try2/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)