Let me clarify what I'm asking:
fn t1<'a, T>(data: Vec<T>) -> impl Iterator<Item = T> + 'a {
data.into_iterator()
}
It should implicitly be T: 'a
because we specified an associated type binding.
And if we don't specify an associated type binding, we are required to explicitly do a lifetime constrain the captured T
in the hidden type
:
fn t2<'a, T>(data: Vec<T>) -> impl Iterator + 'a where T: 'a {
data.into_iterator()
}
Wouldn't this default make sense?
Let's just also clarify these things.
T: 'lifetime
tells us two things.
T
cannot outlive'lifetime
.- All references inside
T
must live at least as long as'lifetime
impl Trait + 'lifetime
tells us only one thing.
impl Trait
cannot outlive'lifetime
, i.e. theopaque type
cannot outlive'lifetime
.
impl Trait + use<'lifetime>
means the hidden type is allowed to use the 'lifetime
. However, it doesn't imply that the hidden type must capture the lifetime unless it's actually needed. It also means that we only capture 'lifetime
, so if we have another lifetime that the hidden type captures, it will not allow it.
Am I wrong?
Old post
My understanding about this change might not be fully informed, so feel free to correct me if I'm getting something wrong.
The new change will by default capture all in-scope type params + lifetimes.
Is there a reason why RPITs with associated type bindings (e.g. Item = T
in impl Iterator<Item = T>
), and a lifetime bound (e.g. + 'lifetime
)--don't automatically also constrain associated type bindings with said lifetime?
For concrete (w/o generics) type with lifetime bounds (e.g. ConcreteType<T>: 'lifetime
), all of the parameters of that type get the same lifetime implicitly. We can even use covariance to implicity do lifetime bounds (e.g PhantomData<T>: 'lifetime
).
I understand that the difference is because RPITs are opaque types, so they have different semantics than concrete types. But that only makes sense if we don't specify any associated type bindings.
If we have specified them, wouldn't it make more sense that they also get implicit lifetime bounds?
Let me give an example of what I mean.
-> impl Iterator<Item = T> + 'a
-> impl Iterator + 'a
For me, when I see #1 I interpret it as if T has an implicit lifetime bound because of the explicit associated type binding (i.e Item = T), but that is not the case currently.
#2 would then work like it does now, it is an opaque type, so any outlive bound would just apply to the opaque type, nothing propagates down like it would do for concrete types.
This would work with the new Ăąse<>
syntax.