Skip to content

Commit 5fa4230

Browse files
LendingIterator
1 parent 7d101c2 commit 5fa4230

File tree

1 file changed

+38
-25
lines changed

1 file changed

+38
-25
lines changed

text/3437-implementable-trait-alias.md

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,9 @@ With today's `trait_alias`, it wouldn't make much difference for `downstream`.
178178

179179
## Splitting a trait
180180

181-
To exemplify this use-case, we will use [Niko Matsakis’s proposal to split the
182-
`Deref`
181+
### `Deref``Receiver` + `Deref`
182+
183+
[Niko Matsakis wants to split up the `Deref`
183184
trait](https://github.com/rust-lang/rust/pull/135881#issuecomment-2718417230).
184185

185186
`Deref` currently looks like this:
@@ -193,7 +194,23 @@ pub trait Deref {
193194
```
194195

195196
Niko wants to split off the `type Target` part into a separate `Receiver`
196-
supertrait. But there is no backward-compatible way to do this at present.
197+
supertrait. But there is no good backward-compatible way to do this at present.
198+
199+
### `Iterator``LendingIterator` + `Iterator`
200+
201+
Once the necessary language features are stabilized, the library team will
202+
likely want to add a `LendingIterator` trait to the standard library, that looks
203+
like this:
204+
205+
```rust
206+
pub trait LendingIterator {
207+
type Item<'a> where Self: 'a;
208+
fn next(&mut self) -> Option<Self::Item<'_>>;
209+
}
210+
```
211+
212+
Ideally, every `Iterator` should automatically be a `LendingIterator`. But,
213+
again, there is no good way to do this right now.
197214

198215
## Removing a `Sized` bound
199216

@@ -324,6 +341,20 @@ impl QuiteVerboseAlias for () {
324341
}
325342
```
326343

344+
This could be used to add `LendingIterator` as a supertrait of `Iterator`, as
345+
mentioned in the motivation section:
346+
347+
```rust
348+
pub trait LendingIterator {
349+
type Item<'a> where Self: 'a;
350+
fn next(&mut self) -> Option<Self::Item<'_>>;
351+
}
352+
353+
pub trait Iterator = for<'a> LendingIterator<Item<'a> = <Self as Iterator>::Item> {
354+
type Item;
355+
}
356+
```
357+
327358
## Implementing trait aliases for multiple traits
328359

329360
Trait aliases that combine multiple traits with `+` are also implementable:
@@ -975,11 +1006,10 @@ something when it does not.
9751006
Anything that makes `where` clauses more powerful would make this feature more
9761007
powerful as well.
9771008

978-
For example:
979-
980-
- If we could write bounds for the `const`ness of a method, that could allow
981-
emulating some of [const traits](https://github.com/rust-lang/rfcs/pull/3762)—or
982-
even form part of the desugaring for that feature:
1009+
For example, if we could write bounds for the `const`ness of a method, that
1010+
could allow emulating some of [const
1011+
traits](https://github.com/rust-lang/rfcs/pull/3762)—or even form part of the
1012+
desugaring for that feature:
9831013

9841014
```rust
9851015
pub trait PartialEq<Rhs = Self>
@@ -998,20 +1028,3 @@ where
9981028
Self::eq: const,
9991029
Self::ne: const; // 🚲🏠
10001030
```
1001-
1002-
- If we could write a bound requiring that a GAT not use is lifetime, that could
1003-
enable retrofitting `LendingIterator` into `Iterator`:
1004-
1005-
```rust
1006-
pub trait LendingIterator {
1007-
type Item<'a>
1008-
where
1009-
Self: 'a;
1010-
1011-
fn next(&mut self) -> Option<Self::Item<'_>>;
1012-
}
1013-
1014-
pub trait Iterator = Iterator
1015-
where
1016-
for<'a> Self::Item<'a>: doesnt_depend_on<'a>; // 🚲🏠
1017-
```

0 commit comments

Comments
 (0)