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 may not live long enough #37

Closed
jennydaman opened this issue Mar 4, 2024 · 3 comments
Closed

lifetime may not live long enough #37

jennydaman opened this issue Mar 4, 2024 · 3 comments

Comments

@jennydaman
Copy link

Hello,

I am wondering how to deal with this situation.

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=9c9d1ddc0b3ece4b4bb40280c0fda6ca

use async_recursion::async_recursion;

#[tokio::main]
async fn main() {
    count_down(5, None).await;
}

#[async_recursion]
async fn count_down(num: u32, foo: Option<&str>) {
    if num == 0 {
        return;
    }
    println!("{}, {:?}", num, foo);
    count_down(num - 1, foo);
}

Output:

error: lifetime may not live long enough
 --> src/main.rs:8:1
  |
8 | #[async_recursion]
  | ^^^^^^^^^^^^^^^^^^ returning this value requires that `'1` must outlive `'static`
9 | async fn count_down(num: u32, foo: Option<&str>) {
  |                                           - let's call the lifetime of this reference `'1`
  |
  = note: this error originates in the attribute macro `async_recursion` (in Nightly builds, run with -Z macro-backtrace for more info)
help: to declare that the trait object captures data from argument `foo`, you can add an explicit `'_` lifetime bound
  |
8 | #[async_recursion] + '_
  |                    ++++

The "help" given by rustc is invalid.

@jennydaman
Copy link
Author

If I were to expand the macro and accept the compiler's help, it works:

#[must_use]
fn count_down(
    num: u32,
    foo: Option<&str>,
) -> ::core::pin::Pin<Box<dyn ::core::future::Future<Output = ()> + ::core::marker::Send + '_>> {
    Box::pin(async move {
        if num == 0 {
            return;
        }
        println!("{}, {:?}", num, foo);
        count_down(num - 1, foo);
    })
}

@dcchut
Copy link
Owner

dcchut commented Mar 6, 2024

Looks like a duplicate of #34

@dcchut
Copy link
Owner

dcchut commented Mar 17, 2024

Resolved in #38, released in v1.1.0.

@dcchut dcchut closed this as completed Mar 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants