trait Input {}
impl<'a> Input for &'a str {}
fn create_fut_bad<'r, 'i, I: Input + 'i>(input: I) -> impl Future<Output = String> + 'r {
async { String::new() }
}
fn bad() -> impl Future<Output = String> {
let s = String::new();
create_fut_bad(s.as_str()) // `s` does not live long enough
}
the function create_fut_bad creates a future which is obviously invariant to the input argument lifetime, but I'm having trouble somehow encoding it into the function signature.
If the input argument is not generic, everything is fine. E.g.:
I tried HRTB, adding lifetime argument to Input trait, taking a reference to I ... nothing seems to help.
Is there a way to encode the lifetime invariance?
Your attempts aren't really about variance as written; you're trying to return some unrelated lifetime, any related lifetime, which is basically going to require returning something with a 'static lifetime.
A returned impl Trait captures all generic type parameters/inputs, so if you want to return a 'static one, you need a 'static bound on them... which won't work with local borrows.
Thanks a lot for looking into this. I can at least stop banging my head against the wall .
My use case is a bit more complicated and type_alias_impl_trait feature does not seem to cut it just yet. (error: non-defining opaque type use in defining scope)
I'll try to create a specific type implementing Future where I can control the lifetime annotations an return that instead.
Again, thank you