Skip to content

Commit cd871e0

Browse files
Support fully-qualified method call syntax
1 parent 5aa3827 commit cd871e0

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

text/3437-implementable-trait-alias.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
# Summary
77

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.
99

1010
# Motivation
1111

@@ -196,7 +196,7 @@ impl Frobber for MyType {
196196

197197
Joe's original code Just Works.
198198

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
200200
`for` and `{` of a trait `impl` block, and the result is sytactically valid—then the trait alias is implementable.
201201

202202
# Reference-level explanation
@@ -287,6 +287,25 @@ impl Frobber for MyType {
287287

288288
Trait aliases are `unsafe` to implement iff the underlying trait is marked `unsafe`.
289289

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+
290309
# Drawbacks
291310

292311
- The sytactic distance between implementable and non-implementable aliases is short, which might confuse users.

0 commit comments

Comments
 (0)