AsyncFn Send lifetime issue

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);
}

playground

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);
}

playground

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?

(Your second playground link goes to the wrong place).

I believe it decides that it's not Send if it decides you're holding a &mut _ across an await point in the AsyncFnMut implementation, and there's some lack of normalization or something going on that makes it think that can happen here.

This is mostly a guess about the new functionality. However, here's a playground with the failing code, but I've added a 'static bound on call_callback, just to show that still fails. Then if we go to unstable and limit the future to be a single type, i.e. require that it can't be a lending function, it compiles again.

pub async fn call_callback<'a, F, CallbackFn>(
    arg: &'a i32,
    callback: Callback<'a, CallbackFn>,
)
where
    F: Future,
    CallbackFn: 'static + for<'call> AsyncFnMut<
        (&'a i32,), 
        Output = (), 
        CallRefFuture<'call> = F
    >,

(If you remove the 'static bound, you get the typical "GATs and HRTBs don't play well together" issues.)

You could file an issue. Apparently you don't need lending functions, since your code worked before; in that case you could just not migrate. If you need lending functions for something new, this might be a legitimate error for those cases.

Or it has to assume it can happen as the bounds on the function allow it.

Looks similar to this.

        fn foo<T>(t:T) -> T where for<'a> T: FnMut(&'a i32) { t }
        foo(|_| {});
    
        fn bar<'a, T>(t:T) -> T where T: AsyncFnMut(&'a i32) { t }
        bar(async |_| {});
        bar(|_| { async{} });
        
        fn baz<T>(t:T) -> T where for<'a> T: AsyncFnMut(&'a i32) { t }
        baz(async |_| {});
        baz(|_| { async{} });  // error

Except I'm trying to promote a closure rather than fn. (In early rust non-capturing closures were not allowed to be promoted to fn)