fn test_lifetime<F,U>(_:F) where F: for<'l> Fn(&'l str)->U{}
async fn ashow(_:& str){}
fn main(){
test_lifetime(ashow);
}
For this example, the compiler will report some errors:
one type is more general than the other
As said in this answers, the type parameter U
can only be resolved to a single type. In contrast, the returned type of ashow
, saying OpaqueFuture<&'l str>
, denotes an infinite type for every lifetime. So, the type parameter U
cannot be resolved from OpaqueFuture<&'l str>
.
However, I don't know why async_fn_traits
can bypass this issue. I simplified its implementation as the following:
use std::future::Future;
trait MyFuture<T>{
type OutputFuture;
type Output;
}
impl<F,Fut,T> MyFuture<T> for F // #1
where F: Fn(T)->Fut,
Fut:Future
{
type OutputFuture = Fut;
type Output = Fut::Output;
}
fn test_lifetime<F>(_:F) where F: for<'l> MyFuture<&'l str>{}
async fn ashow(_:& str){}
fn main(){
test_lifetime(ashow);
}
First, with the trait bound for<'l> MyFuture<&'l str>
, the type parameter T
in the item at #1
is explicitly specified as &'l str
, and &'l str
is an infinite type for every lifetime 'l
, why does the type parameter T
can be specified as such an infinite type?
Second, resembling the U
in the first example, why can the type parameter Fut
in the item can be resolved to the infinite type OpaqueFuture<&'l str>
but U
cannot?