Skip to content

Split apart the Body type #2345

Description

@seanmonstar

This has been suggested before: the current trait hyper::body::HttpBody should be renamed to hyper::Body, and the existing struct hyper::Body should be split up into more descriptive implementations. I'm coming around to that idea, so here's the full proposal.

Proposal

  • Change the trait hyper::body::HttpBody to hyper::Body
  • Split up the hyper::Body type:
    • hyper::body::Empty: an empty body, yielding no data or trailers. Since it never yields data, its Buf type could even be some enum NeverBuf {}.
    • hyper::body::Full: the full body, able to yield 1 data buffer (what hyper::Body::from(buf) is in 0.13).
    • hyper::body::Streaming: the streaming bodies received from a remote over HTTP/1 or 2.
    • (Optional) hyper::body::BoxBody

A client response would then be Response<Streaming> (as would a server Request<Streaming>), since they are streamed from the connection. Hopefully, this should make the intent clearer when you have a Request<Empty> or Response<Full>, instead of just Request<Body>.

Status: Accepted

Progress

Activity

  1. added
    B-rfcBlocked: More comments would be useful in determine next steps.
    A-bodyArea: body streaming.
    B-breaking-changeBlocked: this is an "API breaking change".
    on Nov 26, 2020
  2. added this to the 0.14 milestone on Nov 26, 2020
  3. seanmonstar commented on Nov 26, 2020

    @seanmonstar
    MemberAuthor
  4. davidbarsky commented on Nov 26, 2020

    @davidbarsky
    Contributor

    As a consumer, I like this proposal.

    hyper::body::Empty: an empty body, yielding no data or trailers. Since it never yields data, its Buf type could even be some enum NeverBuf {}.

    I don't want to jinx this, but it's possible that ! might get stabilized with rust-lang/rust#79366. I don't think it'll be stable in time for Hyper 0.14, but there's a chance it might be stable in time for Hyper 0.15 (if planned).

  5. sfackler commented on Nov 26, 2020

    @sfackler
    Contributor

    Does this imply that the Client type can drop its body type parameter? That'd be quite nice for reasons even beyond this issue, like allowing non 'static bodies.

  6. seanmonstar commented on Nov 26, 2020

    @seanmonstar
    MemberAuthor

    I don't see how the client would drop its body type parameter... it still needs to know what kind of bodies you expect to send.

  7. LucioFranco commented on Nov 27, 2020

    @LucioFranco
    Member

    I like this proposal, I'd also consider adding a GenericBody which is an enum of all of the bodies + the boxed one? We may also want to provide utils like EitherBody to compose them. Overall, I am a fan of this, no major nits.

  8. peku33 commented on Nov 29, 2020

    @peku33

    Would be nice to still be able to use all types at once, as @LucioFranco said. I belive most of apps mix those, eg. Full for data, Streaming for SSE etc. If body type goes into server template parameter, it would be a bit harder to return multi-variant version. Boxing is an option, but still requires some allocations. Having enum of three types doesn't sound bad.

  9. davidbarsky commented on Nov 30, 2020

    @davidbarsky
    Contributor

    Something I didn't consider when responding to this initially: what implications, if any, does this proposed change have for h2-style patterns where the initial handshake request has a body of () but the client subsequently expected to write to a stream handle?

  10. seanmonstar commented on Nov 30, 2020

    @seanmonstar
    MemberAuthor

    If you expect different requests to have different body behaviors, you can use a body type that captures that.

  11. seanmonstar commented on Dec 3, 2020

    @seanmonstar
    MemberAuthor

    So, it does seem like this is desirable, but I'm going to punt to the next milestone, since proposing this right when 0.14 was almost ready is kinda late.

  12. modified the milestones: 0.14, on Dec 3, 2020
  13. 6 remaining items

  14. removed
    B-rfcBlocked: More comments would be useful in determine next steps.
    on Jun 8, 2022
  15. seanmonstar commented on Jun 15, 2022

    @seanmonstar
    MemberAuthor

    The http-body-util crate now has many of these variants. We likely won't re-export directly from hyper, since that util crate is less stable. Next steps for hyper 1.0 are to remove the internal variants of hyper::Body, and replace their usage in tests and examples with those from http-body-util.

  16. added
    E-mediumEffort: medium. Some knowledge of how hyper internal works would be useful.
    C-featureCategory: feature. This is adding a new feature.
    and removed
    B-breaking-changeBlocked: this is an "API breaking change".
    on Jun 15, 2022
  17. Xuanwo commented on Jul 26, 2022

    @Xuanwo
  18. seanmonstar commented on Jul 26, 2022

    @seanmonstar
    Author
  19. modified the milestones: 1.0 RC1, 1.0 Final on Sep 15, 2022
  20. moved this from Todo to In Progress in hyper 1.0on Dec 28, 2022
  21. moved this from In Progress to Done in hyper 1.0on May 18, 2023
  22. added a commit that references this issue on Mar 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-bodyArea: body streaming.C-featureCategory: feature. This is adding a new feature.E-mediumEffort: medium. Some knowledge of how hyper internal works would be useful.

    Type

    No type

    Projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions