Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use impl IntoResponse less in docs #1049

Merged
merged 1 commit into from May 22, 2022
Merged

Conversation

davidpdrsn
Copy link
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) {
/// (
Copy link

@bsodmike bsodmike May 22, 2022

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
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.

None yet

3 participants