Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lifetime issue when using "impl FnMut" as argument type #177

Closed
JanBeh opened this issue Oct 12, 2021 · 1 comment · Fixed by #201
Closed

Lifetime issue when using "impl FnMut" as argument type #177

JanBeh opened this issue Oct 12, 2021 · 1 comment · Fixed by #201

Comments

@JanBeh
Copy link

JanBeh commented Oct 12, 2021

Case 1:

async fn foo(&self, mut callback: impl FnMut(&str) + Send)

expands to:

    fn foo<'life0, 'life1, 'async_trait>(
        &'life0 self,
        callback: impl FnMut(&'life1 str) + Send,
    ) -> ::core::pin::Pin<
        Box<dyn ::core::future::Future<Output = ()> + ::core::marker::Send + 'async_trait>,
    >
    where
        'life0: 'async_trait,
        'life1: 'async_trait,
        Self: ::core::marker::Sync + 'async_trait,

which is not functioning as expected.

Case 2:

async fn foo<C: FnMut(&str) + Send>(&self, mut callback: C)

works however.

The Rust reference states for anonymous type parameters:

That is, impl Trait in argument position is syntactic sugar for a generic type parameter like <T: Trait>, except that the type is anonymous and doesn't appear in the GenericParams list.

It can thus be very surprising that Case 2 is alright, but Case 1 fails.

See also this discussion in the Rust users Forum for an idea what should/could be done about it.

Alternatively, this surprising behavior could be mentioned in the documentation.

@dtolnay
Copy link
Owner

dtolnay commented Oct 13, 2021

I would accept a PR to support this as suggested in the forum thread (callback: impl 'async_trait + FnMut(&str) + Send).

kawaemon added a commit to kawaemon/async-trait that referenced this issue May 16, 2022
kawaemon added a commit to kawaemon/async-trait that referenced this issue May 16, 2022
kawaemon added a commit to kawaemon/async-trait that referenced this issue May 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants