Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: Implementable trait aliases #3437

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions text/3437-implementable-trait-alias.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

# Summary

Extend `#![feature(trait_alias)]` to permit `impl` blocks for trait aliases with a single primary trait.
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.

# Motivation

Expand Down Expand Up @@ -196,7 +196,7 @@ impl Frobber for MyType {

Joe's original code Just Works.

The rule of thumb is: if you can copy everything between the `=` and `;` of a trait alias, and paste it between the
The rule of thumb is: if you can copy everything between the `=` and `;` of a trait alias, paste it between the
`for` and `{` of a trait `impl` block, and the result is sytactically valid—then the trait alias is implementable.

# Reference-level explanation
Expand Down Expand Up @@ -287,6 +287,25 @@ impl Frobber for MyType {

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

Implementable trait aliases can also be used with trait-qualified and fully-qualified method call syntax. When used this way,
Jules-Bertholet marked this conversation as resolved.
Show resolved Hide resolved
they are treated equivalently to the underlying primary trait, with the additional restriction that all `where` clauses and associated type bounds
must be satisfied.

```rust
trait IntIter = Iterator<Item = u32> where Self: Clone;

fn foo() {
let iter = [1_u32].into_iter();
IntIter::next(&mut iter); // works
<std::array::IntoIter as IntIter>::next(); // works
//IntIter::clone(&iter); // ERROR: trait `Iterator` has no method named `clone()`
let dyn_iter: &mut dyn Iterator<Item = u32> = &mut iter;
//IntIter::next(dyn_iter); // ERROR: `dyn Iterator<Item = u32>` does not implement `Clone`
let signed_iter = [1_i32].into_iter();
//IntIter::next(&mut signed_iter); // ERROR: Expected `<Self as Iterator>::Item` to be `u32`, it is `i32`
}
```

# Drawbacks

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