Skip to content

Commit c1bc691

Browse files
Remove implementability restrictions on const and type alias items
1 parent 20d8ba0 commit c1bc691

File tree

1 file changed

+13
-24
lines changed

1 file changed

+13
-24
lines changed

text/3437-implementable-trait-alias.md

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,10 @@ pub trait Iterator = for<'a> LendingIterator<Item<'a> = <Self as Iterator>::Item
355355
}
356356
```
357357

358+
You’ll note that `Iterator`’s body does not explicitly define `Item`. Instead,
359+
it’s defined implicitly by the `for<'a> <Self as LendingIterator>::Item<'a> =
360+
<Self as Iterator>::Item` clause in the alias definition.
361+
358362
## Implementing trait aliases for multiple traits
359363

360364
Trait aliases that combine multiple traits with `+` are also implementable:
@@ -634,7 +638,9 @@ To resolve these conflicts, you can use trait alias bodies, as described below.
634638
## Bodies for trait aliases
635639

636640
Trait aliases can now optionally contain a body, which specifies various *alias
637-
items*. These can be types, constants, or functions.
641+
items*. These can be types, constants, or functions. It must always be possible
642+
to derive the value of these items from the implementations of the aliased
643+
traits; compilation will fail otherwise.
638644

639645
### `type`s and `const` alias items in trait alias bodies
640646

@@ -672,13 +678,9 @@ trait FooBar = Foo + Bar {
672678
```
673679

674680
As aliases, `type` and `const` alias items neither require nor accept bounds or
675-
`where` clauses; these are taken from the thing being aliased.
676-
677-
#### Implementability
681+
`where` clauses; these are taken from the things being aliased.
678682

679-
To be implementable, a `type` or `const` alias item must obey certain
680-
restrictions. It must either be set equal to an item of a primary trait of the
681-
alias:
683+
`type` and `const` alias items may appear in implementations of the alias:
682684

683685
```rust
684686
trait Foo {
@@ -697,9 +699,8 @@ impl Alias for () {
697699
}
698700
```
699701

700-
Or, the trait alias must set an associated type of the primary trait equal to a
701-
generic type, with the alias item as a generic parameter of that type. For
702-
example, here is
702+
Alias items may be defined *implicitly*, through bounds on the trait alias
703+
itself. For example, here is
703704
[`TryFuture`](https://docs.rs/futures-core/latest/futures_core/future/trait.TryFuture.html)
704705
as an implementable trait alias:
705706

@@ -729,18 +730,6 @@ impl TryFuture for AlwaysFails {
729730
}
730731
```
731732

732-
The generic parameter can also be nested:
733-
734-
```rust
735-
trait Foo {
736-
type Assoc;
737-
}
738-
739-
trait Bar = Foo<Assoc = Result<Self::Foo, Vec<Self::Foo>>> {
740-
type Foo;
741-
}
742-
```
743-
744733
#### GATs in type alias bodies
745734

746735
Type alias bodies can also contain GAT alias items. These are also subject to
@@ -784,8 +773,8 @@ trait Alias = Frob {
784773
Modifiers like `const`, `async`, `unsafe`, or `extern "C"` are neither required
785774
nor accepted.
786775

787-
You are allowed to specify generic parameters, in order to reorder them. But you
788-
don't have to:
776+
You are allowed to specify the generic parameters, in order to reorder them. But
777+
you don't have to:
789778

790779
```rust
791780
trait Frob {

0 commit comments

Comments
 (0)