You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
TL;DR: It seems like async route handlers must return a Result. The futures example returns a Result<impl rweb::Reply, Infallible>, suggesting this is known and intentional. Why is this, and can it be documented somewhere?
I see in various examples that a route handler can return an existential type impl Reply. One such example is sse_chat.rs. It compiles fine as-is.
error[E0271]: type mismatch resolving `<impl futures::Future as futures::Future>::Output == Result<_, _>`
--> examples/sse_chat.rs:56:1
|
56 | #[get("/")]
| ^^^^^^^^^^^ expected opaque type, found enum `Result`
57 | async fn index() -> impl Reply {
| ---------- the expected opaque type
|
= note: expected opaque type `impl Reply`
found enum `Result<_, _>`
= note: required because of the requirements on the impl of `TryFuture` for `impl futures::Future`
= note: this error originates in the attribute macro `get` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0271]: type mismatch resolving `<impl futures::Future as futures::Future>::Output == Result<_, _>`
--> examples/sse_chat.rs:56:1
|
56 | #[get("/")]
| ^^^^^^^^^^^ expected enum `Result`, found opaque type
57 | async fn index() -> impl Reply {
| ---------- the found opaque type
|
= note: expected enum `Result<_, _>`
found opaque type `impl Reply`
= note: required because of the requirements on the impl of `TryFuture` for `impl futures::Future`
In short, there is now an extra bound applied somewhere that the returned value must be a Result with certain properties.
I've run both versions through cargo-expand, and see the following diff when adding async:
Indeed, and_then requires a Future returning a Result, from a glance at the docs.
I haven't dug deeply into this. However, I can confirm that the "futures" example returns a Result with Infallible error, so I'm inclined to believe that this is fully intentional/understood.
What's the reason behind this? I guess map doesn't support async filters, and_then it the only alternative, and the route macros don't currently have support for introducing the Result wrapper? Is there somewhere this could be documented?
I figure this issue can stand in as documentation in the short term, which is why I'm opening it with context.
The text was updated successfully, but these errors were encountered:
I suppose I also should make the last part an explicit suggestion -- there are a few other improvements that could potentially be made here, with varying amounts of effort:
Explicitly error when the return type of an async handler isn't a Result
Support implicitly wrapping the return type in a Result<_, Infallible> and later unwrap()ing it if needed
TL;DR: It seems like async route handlers must return a
Result
. Thefutures
example returns aResult<impl rweb::Reply, Infallible>
, suggesting this is known and intentional. Why is this, and can it be documented somewhere?I see in various examples that a route handler can return an existential type
impl Reply
. One such example issse_chat.rs
. It compiles fine as-is.If I make one such handler
async
, as follows:I get two errors:
Click to expand
In short, there is now an extra bound applied somewhere that the returned value must be a
Result
with certain properties.I've run both versions through
cargo-expand
, and see the following diff when addingasync
:Indeed,
and_then
requires a Future returning aResult
, from a glance at the docs.I haven't dug deeply into this. However, I can confirm that the "futures" example returns a Result with
Infallible
error, so I'm inclined to believe that this is fully intentional/understood.What's the reason behind this? I guess
map
doesn't support async filters,and_then
it the only alternative, and the route macros don't currently have support for introducing theResult
wrapper? Is there somewhere this could be documented?I figure this issue can stand in as documentation in the short term, which is why I'm opening it with context.
The text was updated successfully, but these errors were encountered: