syncstorage-rs
syncstorage-rs copied to clipboard
Convert actix-web extractors to async await
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, I'd like to work on this issue.
@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.
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.
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?
Yes, please do. Hopefully it works easily. If not, discuss it in the channel.
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
AFAIK you're right: it's not possible to use async-trait
for FromRequest
impls
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