Skip to content

Migrating from Fn -> impl Future to AsyncFn breaks Send #137698

Open
@smessmer

Description

@smessmer

Posted here: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=48d50965069b89953a04b10aff63c9cb

I want to convert some code to the new AsyncFn traits, and expected AsyncFn to be a drop-in replacement for code that previously used impl Fn () -> F with F: Future, but it doesn't seem to be and I 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?

Meta

Rust 1.85
reproducible with rust playground links above

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-async-awaitArea: Async & AwaitC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions