@@ -8,11 +8,15 @@ things.
8
8
Trait resolution is the process of pairing up an impl with each
9
9
reference to a trait. So, for example, if there is a generic function like:
10
10
11
- fn clone_slice<T:Clone>(x: &[T]) -> Vec<T> { ... }
11
+ ``` rust
12
+ fn clone_slice <T : Clone >(x : & [T ]) -> Vec <T > { /*...*/ }
13
+ ```
12
14
13
15
and then a call to that function:
14
16
15
- let v: Vec<isize> = clone_slice([1, 2, 3])
17
+ ``` rust
18
+ let v : Vec <isize > = clone_slice (& [1 , 2 , 3 ])
19
+ ```
16
20
17
21
it is the job of trait resolution to figure out (in which case)
18
22
whether there exists an impl of ` isize : Clone `
@@ -21,12 +25,14 @@ Note that in some cases, like generic functions, we may not be able to
21
25
find a specific impl, but we can figure out that the caller must
22
26
provide an impl. To see what I mean, consider the body of ` clone_slice ` :
23
27
24
- fn clone_slice<T:Clone>(x: &[T]) -> Vec<T> {
25
- let mut v = Vec::new();
26
- for e in &x {
27
- v.push((*e).clone()); // (*)
28
- }
28
+ ``` rust
29
+ fn clone_slice < T : Clone >( x : & [ T ]) -> Vec < T > {
30
+ let mut v = Vec :: new ();
31
+ for e in & x {
32
+ v . push (( * e ) . clone ()); // (*)
29
33
}
34
+ }
35
+ ```
30
36
31
37
The line marked ` (*) ` is only legal if ` T ` (the type of ` *e ` )
32
38
implements the ` Clone ` trait. Naturally, since we don't know what ` T `
@@ -107,7 +113,7 @@ otherwise the result is considered ambiguous.
107
113
This process is easier if we work through some examples. Consider
108
114
the following trait:
109
115
110
- ```
116
+ ``` rust
111
117
trait Convert <Target > {
112
118
fn convert (& self ) -> Target ;
113
119
}
@@ -119,8 +125,8 @@ wanted to permit conversion between `isize` and `usize`, we might
119
125
implement ` Convert ` like so:
120
126
121
127
``` rust
122
- impl Convert <usize > for isize { ... } // isize -> usize
123
- impl Convert <isize > for usize { ... } // usize -> isize
128
+ impl Convert <usize > for isize { /* ...*/ } // isize -> usize
129
+ impl Convert <isize > for usize { /* ...*/ } // usize -> isize
124
130
```
125
131
126
132
Now imagine there is some code like the following:
@@ -205,12 +211,14 @@ using the definition of *matching* given above.
205
211
206
212
Consider this simple example:
207
213
208
- trait A1 { ... }
209
- trait A2 : A1 { ... }
214
+ ``` rust
215
+ trait A1 { /*...*/ }
216
+ trait A2 : A1 { /*...*/ }
210
217
211
- trait B { ... }
218
+ trait B { /* ...*/ }
212
219
213
- fn foo<X:A2+B> { ... }
220
+ fn foo <X : A2 + B > { /*...*/ }
221
+ ```
214
222
215
223
Clearly we can use methods offered by ` A1 ` , ` A2 ` , or ` B ` within the
216
224
body of ` foo ` . In each case, that will incur an obligation like `X :
@@ -247,10 +255,12 @@ to us, so we must run trait selection to figure everything out.
247
255
248
256
Here is an example:
249
257
250
- trait Foo { ... }
251
- impl<U,T:Bar<U>> Foo for Vec<T> { ... }
258
+ ``` rust
259
+ trait Foo { /*...*/ }
260
+ impl <U ,T : Bar <U >> Foo for Vec <T > { /*...*/ }
252
261
253
- impl Bar<usize> for isize { ... }
262
+ impl Bar <usize > for isize { /*...*/ }
263
+ ```
254
264
255
265
After one shallow round of selection for an obligation like `Vec<isize >
256
266
: Foo`, we would know which impl we want, and we would know that
@@ -343,7 +353,7 @@ Once the basic matching is done, we get to another interesting topic:
343
353
how to deal with impl obligations. I'll work through a simple example
344
354
here. Imagine we have the traits ` Foo ` and ` Bar ` and an associated impl:
345
355
346
- ```
356
+ ``` rust
347
357
trait Foo <X > {
348
358
fn foo (& self , x : X ) { }
349
359
}
@@ -401,7 +411,9 @@ Therefore, we search through impls and where clauses and so forth, and
401
411
we come to the conclusion that the only possible impl is this one,
402
412
with def-id 22:
403
413
404
- impl Foo<isize> for usize { ... } // Impl #22
414
+ ``` rust
415
+ impl Foo <isize > for usize { ... } // Impl #22
416
+ ```
405
417
406
418
We would then record in the cache `usize : Foo<%0> ==>
407
419
ImplCandidate(22)` . Next we would confirm ` ImplCandidate(22)`, which
0 commit comments