BoxFuture Lazy Static Function Pointers

Hello. I am trying to make a static hashamp with asynchronous function pointers in order to await and call later. I am running into an issue where rust-analyzer is giving this code block all red error signs, and I am not sure what the issue is. Here are the two errors I am getting:
unexpected token in input
expected Expr
Here is my code:

pub type StepFuture = BoxFuture<'static, Result<(), HttpResponse<BoxBody>>>;

lazy_static! {
    static ref WORKDAY_STEPS: HashMap<String, fn() -> StepFuture> = {
        HashMap::from([
            (
                "Create Account/Sign In".to_string(),
                (|| {
                    Box::pin(async {
                        println!("Create Account");
                        Ok(())
                    })
                }) as fn() -> StepFuture,
            ),
            (
                "My Information".to_string(),
                (|| {
                    Box::pin(async {
                        println!("My Info");
                        Ok(())
                    })
                }) as fn() -> StepFuture,
            ),
            (
                "My Experience".to_string(),
                (|| {
                    Box::pin(async {
                        println!("My Exp");
                        Ok(())
                    })
                }) as fn() -> StepFuture,
            ),
            (
                "Application Questions".to_string(),
                (|| {
                    Box::pin(async {
                        println!("Application");
                        Ok(())
                    })
                }) as fn() -> StepFuture,
            ),
            (
                "Voluntary Disclosures".to_string(),
                (|| {
                    Box::pin(async {
                        println!("Disclosure");
                        Ok(())
                    })
                }) as fn() -> StepFuture,
            ),
            (
                "Self Identify".to_string(),
                (|| {
                    Box::pin(async {
                        println!("Identity");
                        Ok(())
                    })
                }) as fn() -> StepFuture,
            ),
            (
                "Take Assessment".to_string(),
                (|| {
                    Box::pin(async {
                        println!("Assessment");
                        Ok(())
                    })
                }) as fn() -> StepFuture,
            ),
            (
                "Review".to_string(),
                (|| {
                    Box::pin(async {
                        println!("Review");
                        Ok(())
                    })
                }) as fn() -> StepFuture,
            ),
        ])
    };
}

If anyone can offer any help or insight, thank you so much.

Please always post the full errors from running cargo build or cargo check in the terminal, if you don't know how to get the full errors from your IDE/editor. Rust errors contain lots of useful information: hints, related variables and types, etc.

Ok. I had some other functions so I commented them out and re-ran it. I didn't give me any issues so I restarted vscode and there was no more errors from vscode. Sorry for the confusion.

Hey, I ran into another issue with the awaiting the future. Here is the updated code:

pub type StepFuture = BoxFuture<'static, Result<(), HttpResponse<BoxBody>>>;

pub async fn create_new_account() -> StepFuture {
    println!("New Account");
    let resp = call_api::<AddWorkdayBody, AddWorkdayResp>(
        "http://localhost:8080/add_password",
        AddWorkdayBody {
            company_name: "Test232".to_string(),
        },
    )
    .await;
    let account_info = match resp {
        Ok(resp) => resp,
        Err(err) => return Box::pin(async { Err(HttpResponse::BadRequest().body(err)) }),
    };
    println!("{:?}", account_info);

    Box::pin(async { Ok(()) })
}

lazy_static! {
    static ref WORKDAY_STEPS: HashMap<String, fn() -> StepFuture> = {
        HashMap::from([
            (
                "Create Account/Sign In".to_string(),
                create_new_account as fn() -> StepFuture,
            ),
            (
                "My Information".to_string(),
                (|| {
                    Box::pin(async {
                        println!("My Info");
                        Ok(())
                    })
                }) as fn() -> StepFuture,
            ),
            (
                "My Experience".to_string(),
                (|| {
                    Box::pin(async {
                        println!("My Exp");
                        Ok(())
                    })
                }) as fn() -> StepFuture,
            ),
            (
                "Application Questions".to_string(),
                (|| {
                    Box::pin(async {
                        println!("Application");
                        Ok(())
                    })
                }) as fn() -> StepFuture,
            ),
            (
                "Voluntary Disclosures".to_string(),
                (|| {
                    Box::pin(async {
                        println!("Disclosure");
                        Ok(())
                    })
                }) as fn() -> StepFuture,
            ),
            (
                "Self Identify".to_string(),
                (|| {
                    Box::pin(async {
                        println!("Identity");
                        Ok(())
                    })
                }) as fn() -> StepFuture,
            ),
            (
                "Take Assessment".to_string(),
                (|| {
                    Box::pin(async {
                        println!("Assessment");
                        Ok(())
                    })
                }) as fn() -> StepFuture,
            ),
            (
                "Review".to_string(),
                (|| {
                    Box::pin(async {
                        println!("Review");
                        Ok(())
                    })
                }) as fn() -> StepFuture,
            ),
        ])
    };
}

I am getting an issue with an invalid cast for this line:
create_new_account as fn() -> StepFuture, but I am not sure how it is invalid.
Here is the error message from cargo run:

error[E0605]: non-primitive cast: `fn() -> impl futures::Future<Output = Pin<Box<(dyn futures::Future<Output = Result<(), HttpResponse>> + std::marker::Send + 'static)>>> {create_new_account}` as `fn() -> Pin<Box<(dyn futures::Future<Output = Result<(), HttpResponse>> + std::marker::Send + 'static)>>`
  --> src/scraper.rs:50:17
   |
50 |                 create_new_account as fn() -> StepFuture,

What would you recommend would be the best way to fix it?

You have create_new_account defined as

pub async fn create_new_account() -> StepFuture {

That means it returns a future containing a future. That’s not the return type you intended. You need to write it as a non-async fn containing an async block instead — the same way your closures are.

pub fn create_new_account() -> StepFuture {
    Box::pin(async {
        println!("New Account");
        let resp = call_api::<AddWorkdayBody, AddWorkdayResp>(
            "http://localhost:8080/add_password",
            AddWorkdayBody {
                company_name: "Test232".to_string(),
            },
        )
        .await;
        let account_info = match resp {
            Ok(resp) => resp,
            Err(err) => return Err(HttpResponse::BadRequest().body(err)),
        };
        println!("{:?}", account_info);

        Ok(()) 
    })
}
1 Like

Thank you for the help. I need to do the await call for the call_api() function call, which is only allowed in async functions. Would changing the return type to BoxFuture<'static, StepFuture> work?

Edit: Sorry skipped over a line in your answer by accident. Thank you for the help.