error[E0733]: recursion in an async fn requires boxing

Hi,

I use async function in this kind of way :

enum MyEnum {
    A(...),
    B(...),
    C(...)
}

async fn is_valid(&self) {
        match self {
            A(x)    => { x.is_valid().await },
            B(x)    => { x.is_valid().await },
            C(x)    => { x.is_valid().await },
        }
    }

And I get this error :

error[E0733]: recursion in an async fn requires boxing

I search in documentation. It seems to be related with Box and maybe also Pin.
My project use to work just before without async function until I got a problem with reqwest::blocking.
Now I have to learn a big concept to just call the function is_valid...

Is there a way to call a async funtion safely in a function not async ?

Please share more of your actual code and what you’re trying to achieve. Why does is_valid seem to need to be async? What problem did you encounter with reqwest::blocking?

Even if you do need to use async, more information will help us tell you how to use it well.

To just make the recursion work, you would write this:

A(x) => Box::pin(x.is_valid()).await,

but there is likely a better way, which we need more information about the application to discover.

1 Like

Thank you very much. :sob:
I read so much combining Box and Pin and finally it was simple...

Just a curious question :
Do we really need to be async function when we call a async function ?

An async fn produces a future, which must either be awaited or run by an async executor. Since reqwest uses tokio, you would use the Tokio executor, such as via Runtime::block_on() or Handle::block_on().

However, if you use an executor to run a future to completion, then that makes this function a blocking function. The rule you must follow is: you must not call any blocking function from an async function, even if that blocking function is blocking on an async function.

Using reqwest::blocking is equivalent to calling block_on() on reqwest's futures, so whatever problem you had with that (which you have not explained) will still apply.

1 Like

Sorry. I am doing a test to have a job.
They want it to be private for now. But as soon I can, I will share it to help other if possible because I learn a lot of thing.

Thank you very much for your time. I think it is more clear for me now.
Everybody are very nice in this forum.