syncstorage-rs icon indicating copy to clipboard operation
syncstorage-rs copied to clipboard

Convert actix-web extractors to async await

Open fzzzy opened this issue 4 years ago • 9 comments

In the FromRequest implementations for BsoBodies and BsoBody, we invoke and_then() on the Future returned by another from_request() invocation. Now that Rust supports async/await, this is no longer idiomatic. Instead, we should wrap the from_request() invocation in a Box::pin(async move { ... }) block as we do in the other extractors. This would allow us to use async/await.

fzzzy avatar Mar 25 '20 14:03 fzzzy

@fzzzy, I'd like to work on this issue.

Emmanuel-Melon avatar Apr 08 '20 22:04 Emmanuel-Melon

@fzzzy Since Rust doesn't support Async in traits, can we utilize one of the following approaches

  • Async Trait
  • I've looked into the docs and we can desugar this by returning a trait object. Ran a few tests and this seems to partially work. Will draft a PR shortly.

Emmanuel-Melon avatar Apr 10 '20 15:04 Emmanuel-Melon

Thanks. I looked at b15cf0d and I don't think we want to use the dyn Future approach. Did you try using Async Trait? That is what we are planning on moving to in the whole codebase eventually.

In the meantime, you can find other places in the codebase where we work around this. We do it by using a non-async function in the impl, but then using an async closure inside that to be able to use async syntax. However, I think this can also be replaced with Async Trait.

fzzzy avatar Apr 15 '20 13:04 fzzzy

Thanks. I looked at b15cf0d and I don't think we want to use the dyn Future approach. Did you try using Async Trait? That is what we are planning on moving to in the whole codebase eventually.

In the meantime, you can find other places in the codebase where we work around this. We do it by using a non-async function in the impl, but then using an async closure inside that to be able to use async syntax. However, I think this can also be replaced with Async Trait.

Yes, I've suggested using async trait in the previous comment and I wanted a confirmation on whether I should introduce new dependencies.

So, can I go back to writing these functions using async trait?

Emmanuel-Melon avatar Apr 15 '20 21:04 Emmanuel-Melon

Yes, please do. Hopefully it works easily. If not, discuss it in the channel.

fzzzy avatar Apr 16 '20 19:04 fzzzy

JIRA

ethowitz avatar Mar 14 '22 17:03 ethowitz

I could be wrong about this, but since the FromRequest trait isn't defined with #[async_trait], I don't think we can apply #[async_trait] to a FromRequest implementation, so I'm not sure if this is possible

ethowitz avatar Mar 14 '22 21:03 ethowitz

AFAIK you're right: it's not possible to use async-trait for FromRequest impls

pjenvey avatar Mar 14 '22 21:03 pjenvey

There are certain places where we store a fut local var and call and_then instead of wrapping everything in an async {} block and using async/await, so I'll repurpose this issue to address those extractors

ethowitz avatar Mar 14 '22 21:03 ethowitz