I want to convert some code to the new AsyncFn
traits, but am running into lifetime and/or Send trait issues.
Minimalized toy example, pre-conversion, this code works fine:
use std::marker::PhantomData;
pub async fn call_callback<'a, F, CallbackFn>(
arg: &'a i32,
callback: Callback<'a, F, CallbackFn>,
)
where
F: Future,
CallbackFn: FnMut(&'a i32) -> F,
{
if let Callback::SomeCallback{mut func} = callback {
func(arg).await;
}
}
pub enum Callback<'a, F, CallbackFn>
where
F: Future,
CallbackFn: FnMut(&'a i32) -> F,
{
NoCallback {
_p: PhantomData<&'a i32>,
},
SomeCallback {
func: CallbackFn,
},
}
impl<'a> Callback<'a, std::future::Ready<()>, fn(&'a i32) -> std::future::Ready<()>>
{
pub fn no_callback() -> Self {
Self::NoCallback {
_p: PhantomData,
}
}
}
fn assert_is_send<V: Send>(_v: V) {}
async fn test() {
let task = async {
let callback = Callback::no_callback();
call_callback(&0, callback).await;
};
assert_is_send(task);
}
and here the version converting it to AsyncFn
:
use std::marker::PhantomData;
pub async fn call_callback<'a, CallbackFn>(
arg: &'a i32,
callback: Callback<'a, CallbackFn>,
)
where
CallbackFn: AsyncFnMut(&'a i32),
{
if let Callback::SomeCallback{mut func} = callback {
func(arg).await;
}
}
pub enum Callback<'p, CallbackFn>
where
CallbackFn: AsyncFnMut(&'p i32),
{
NoCallback {
_p: PhantomData<&'p i32>,
},
SomeCallback {
func: CallbackFn,
},
}
impl<'p> Callback<'p, fn(&'p i32) -> std::future::Ready<()>>
{
pub fn no_callback() -> Self {
Self::NoCallback {
_p: PhantomData,
}
}
}
fn assert_is_send<V: Send>(_v: V) {}
async fn test() {
let task = async {
let callback = Callback::no_callback();
call_callback(&0, callback).await;
};
assert_is_send(task);
}
The converted code fails with
Compiling playground v0.0.1 (/playground)
error: implementation of `AsyncFnMut` is not general enough
--> src/lib.rs:44:5
|
44 | assert_is_send(task);
| ^^^^^^^^^^^^^^^^^^^^ implementation of `AsyncFnMut` is not general enough
|
= note: `fn(&'0 i32) -> std::future::Ready<()>` must implement `AsyncFnMut<(&'1 i32,)>`, for any two lifetimes `'0` and `'1`...
= note: ...but it actually implements `AsyncFnMut<(&i32,)>`
error: could not compile `playground` (lib) due to 1 previous error
Interestingly, using a higher ranked type bound will fix it:
impl<'p> Callback<'p, for <'a> fn(&'a i32) -> std::future::Ready<()>>
But I do not understand why the converted version requires that and why the previous version does not require it (also note that in my particular case this solution doesn't work because in the non-minimized example, I actually don't have a lifetime but a type carrying a lifetime there and for<T>
isn't allowed in stable Rust yet).
Another interesting point is that this doesn't actually seem to be a lifetime issue. When I remove the call to assert_is_send
, everything works.
So it seems the lifetimes work just fine, just the generated Future isn't Send
for some reason.
Any idea what's going on and why the compiler behaves in this way here?