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

Applying the '?' expression to an async function doesn't seem to work as intended. #23

Open
myyrakle opened this issue Sep 20, 2022 · 3 comments · May be fixed by #24
Open

Applying the '?' expression to an async function doesn't seem to work as intended. #23

myyrakle opened this issue Sep 20, 2022 · 3 comments · May be fixed by #24

Comments

@myyrakle
Copy link

image
I always use ? expressions.
However, after adding async, an inconvenient error occurs even if i use the method introduced in other documents or use your library.

image
This is a problem that occurs because the Pin and Box are additionally covered.
Do you know a neat solution to this?

@dcchut
Copy link
Owner

dcchut commented Sep 20, 2022

? appears to be working fine, however something is tripping up on the Box<dyn Error> in the Result (possibly rustc can't infer the type we actually want somewhere). The following reproduction experiences a similar issue:

use std::error::Error;
use thiserror::Error;

#[derive(Debug, Error)]
pub enum MyError {
    #[error("something went wrong")]
    Cool,
}


pub enum Expression {
    Literal(&'static str),
    Nested(Box<Expression>),
    AnotherOne,
}

#[async_recursion::async_recursion]
pub async fn resolve_expression(expression: &Expression) -> Result<i32, Box<dyn Error>> {
    match expression {
        Expression::Literal(_) => Ok(7),
        Expression::Nested(nested) => {
            let x = resolve_expression(nested).await?;
            Ok(x + 1)
        }
        Expression::AnotherOne => Err(Box::new(MyError::Cool)),
    }
}

I'll have a go at fixing this up.

@dcchut
Copy link
Owner

dcchut commented Sep 20, 2022

@myyrakle I still intend to fix this properly, but a temporary workaround might be to explicitly cast your boxed error to Box<dyn Error>. This change applied to my example above looks like:

#[async_recursion::async_recursion]
pub async fn resolve_expression(expression: &Expression) -> Result<i32, Box<dyn Error>> {
    match expression {
        Expression::Literal(_) => Ok(7),
        Expression::Nested(nested) => {
            let x = resolve_expression(nested).await?;
            Ok(x + 1)
        }
        Expression::AnotherOne => Err(Box::new(MyError::Cool) as Box<dyn Error>), // <-- cast error type here
    }
}

@myyrakle
Copy link
Author

Thanks for your kind reply.
I hope you can solve it.

@dcchut dcchut linked a pull request Sep 21, 2022 that will close this issue
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

Successfully merging a pull request may close this issue.

2 participants