Is there any way to make something like this work?
fn bar<S, W: FnOnce(&mut S)>(build: W) {}
fn foo<E>() -> impl FnOnce(E) {
|_| {}
}
fn somefunction() {
bar::<bool, _>(foo::<&mut bool>())
}
The compiler error is sort of unclear:
error[E0308]: mismatched types
--> src/lib.rs:8:5
|
3 | fn foo<E>() -> impl FnOnce(E) {
| --------------
| |
| the expected opaque type
| the found opaque type
...
8 | bar::<bool, _>(foo::<&mut bool>())
| ^^^^^^^^^^^^^^ lifetime mismatch
|
= note: expected associated type `<impl FnOnce(&mut bool) as FnOnce<(&mut bool,)>>::Output`
found associated type `<impl FnOnce(&mut bool) as FnOnce<(&mut bool,)>>::Output`
note: the lifetime requirement is introduced here
--> src/lib.rs:1:14
|
1 | fn bar<S, W: FnOnce(&mut S)>(build: W) {}
| ^^^^^^^^^^^^^^
I've tried this, but that doesn't change anything:
fn foo<E>() -> impl FnOnce(E)
where for<'a> E: 'a
{
|_| {}
}
I suspect this might also have to do with variance. Am I correct in thinking that all impl trait lifetimes are invariant?