Skip to content

Use impl IntoResponse less in docs - #1049

Merged
davidpdrsn merged 1 commit into
mainfrom
less-impl-into-response-in-docs
May 22, 2022
Merged

Use impl IntoResponse less in docs#1049
davidpdrsn merged 1 commit into
mainfrom
less-impl-into-response-in-docs

Conversation

@davidpdrsn

Copy link
Copy Markdown
Member

Previously we used impl IntoResponse quite a lot in the docs to avoid having to write the full return type. However we've seen several cases on Discord and Twitter where people who are new to Rust gets confused about that. People sometimes thinks it allows returning different types along the lines of

async fn handle() -> impl IntoResponse {
    if something() {
        "foo"
    } else {
        StatusCode::BAD_REQUEST
    }
}

Which unfortunately wont work 😕 Writing out the full type should make the issue clear.

So I think we're actually doing people a disservice using impl IntoResponse as much as we do. In general I think our docs should cater to beginners perhaps more than other crates, since web frameworks are likely to be the first big crate a newcomer picks up.

So this changes the docs to spell out the return type where it is appropriate. I also added a section about calling .into_response() to more easily return different types.

@davidpdrsn
davidpdrsn requested a review from jplatte May 22, 2022 09:39
@davidpdrsn
davidpdrsn enabled auto-merge (squash) May 22, 2022 09:39
@davidpdrsn
davidpdrsn merged commit 19fe932 into main May 22, 2022
@davidpdrsn
davidpdrsn deleted the less-impl-into-response-in-docs branch May 22, 2022 11:41
/// // Note that returning `impl IntoResponse` might be easier if the response has many parts to
/// // it. The return type is written out here for clarity.
/// async fn handler() -> (SetHeader<'static>, SetHeader<'static>, &'static str) {
/// (

@bsodmike bsodmike May 22, 2022

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@davidpdrsn doesn't the Impl IntoReponse interface recurse over the tuple essentially calling .into_reponse() on each tuple element? Referring to all_the_tuples!(impl_into_response); in the source of the trait.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does yes.

davidpdrsn added a commit that referenced this pull request Jun 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants