|
5 | 5 |
|
6 | 6 | # Summary
|
7 | 7 |
|
8 |
| -Extend `#![feature(trait_alias)]` to permit `impl` blocks for trait aliases with a single primary trait. |
| 8 | +Extend `#![feature(trait_alias)]` to permit `impl` blocks for trait aliases with a single primary trait. Also support fully-qualified method call syntax with such aliases. |
9 | 9 |
|
10 | 10 | # Motivation
|
11 | 11 |
|
@@ -196,7 +196,7 @@ impl Frobber for MyType {
|
196 | 196 |
|
197 | 197 | Joe's original code Just Works.
|
198 | 198 |
|
199 |
| -The rule of thumb is: if you can copy everything between the `=` and `;` of a trait alias, and paste it between the |
| 199 | +The rule of thumb is: if you can copy everything between the `=` and `;` of a trait alias, paste it between the |
200 | 200 | `for` and `{` of a trait `impl` block, and the result is sytactically valid—then the trait alias is implementable.
|
201 | 201 |
|
202 | 202 | # Reference-level explanation
|
@@ -287,6 +287,25 @@ impl Frobber for MyType {
|
287 | 287 |
|
288 | 288 | Trait aliases are `unsafe` to implement iff the underlying trait is marked `unsafe`.
|
289 | 289 |
|
| 290 | +Implementable trait aliases can also be used with trait-qualified and fully-qualified method call syntax. When used this way, |
| 291 | +they are treated equivalently to the underlying primary trait, with the additional restriction that all `where` clauses and associated type bounds |
| 292 | +must be satisfied. |
| 293 | + |
| 294 | +```rust |
| 295 | +trait IntIter = Iterator<Item = u32> where Self: Clone; |
| 296 | + |
| 297 | +fn foo() { |
| 298 | + let iter = [1_u32].into_iter(); |
| 299 | + IntIter::next(&mut iter); // works |
| 300 | + <std::array::IntoIter as IntIter>::next(); // works |
| 301 | + //IntIter::clone(&iter); // ERROR: trait `Iterator` has no method named `clone()` |
| 302 | + let dyn_iter: &mut dyn Iterator<Item = u32> = &mut iter; |
| 303 | + //IntIter::next(dyn_iter); // ERROR: `dyn Iterator<Item = u32>` does not implement `Clone` |
| 304 | + let signed_iter = [1_i32].into_iter(); |
| 305 | + //IntIter::next(&mut signed_iter); // ERROR: Expected `<Self as Iterator>::Item` to be `u32`, it is `i32` |
| 306 | +} |
| 307 | +``` |
| 308 | + |
290 | 309 | # Drawbacks
|
291 | 310 |
|
292 | 311 | - The sytactic distance between implementable and non-implementable aliases is short, which might confuse users.
|
|
0 commit comments