Skip to content

Feature: Implement the Body trait for more native data types to improve ergonomics #3746

Open

Description

@akneni

Currently, the 'String' is the only native data type that implements hyper::body::Body.
If we're passing other data types as a body, we need to add the http_body_util crate and add a little bit of boiler plate when building our request.

use http_body_util::Full;

let some_bytes = vec![0_u8, 1, 2];
let mut req = Request::builder()
    .method("POST")
    .body(Full::<Bytes>::from(some_bytes))
    .unwrap();

Note: While the request builder itself may not require the body to implement the Body trait, both the sender and connection returned by http1::handshake require the request's body to implement it.

With the body trait implemented for Vec<u8> (just one example that probably should implement body) we could leave out the http_body_util crate. Building a request would also become more intuitive and ergonomic.

    let some_bytes = vec![0_u8, 1, 2];
    let mut req = Request::builder()
        .method("POST")
        .body(some_bytes)
        .unwrap();

Activity

  1. added
    C-featureCategory: feature. This is adding a new feature.
    on Aug 27, 2024
  2. zaira-bibi commented on Aug 27, 2024

    @zaira-bibi

    Hi! I would like to try my hand on this issue.

  3. seanmonstar commented on Aug 27, 2024

    @seanmonstar
    Member

    I've been personally torn on this. I appreciate the explicitness of having Full, and I kind of regret that we stabilized with an implementation on String.

    So, part of me resists adding it to any other standard buffer types.

    But I also understand that one exists, so should we just swallow the pill and provide the rest? Maybe. I don't love it. But perhaps I'm just wrong.

  4. akneni commented on Aug 27, 2024

    @akneni
    Author

    I've launched a poll here #3747 . I say we give it a little bit and see what others think.

  5. akneni commented on Sep 5, 2024

    @akneni
    Author

    It seems like 80% of people (granted, only out of 5 responses) want the body trait implemented on additional types. I can get started working on this if that's good with you @seanmonstar

  6. dswij commented on Sep 6, 2024

    @dswij
    Member

    I've been personally torn on this. I appreciate the explicitness of having Full, and I kind of regret that we stabilized with an implementation on String.

    So, part of me resists adding it to any other standard buffer types.

    But I also understand that one exists, so should we just swallow the pill and provide the rest? Maybe. I don't love it. But perhaps I'm just wrong.

    In hindsight, it makes sense to at least have an implementation for bytes if we have one for String.

  7. added
    B-rfcBlocked: More comments would be useful in determine next steps.
    on Sep 24, 2024
  8. zaira-bibi commented on Oct 10, 2024

    @zaira-bibi

    Are there any plans to have an implementation for more native data types? If yes, then I would love to work on this.

  9. akneni commented on Oct 10, 2024

    @akneni
    Author

    I've already implemented this trait for Vec<u8>, but no other native types. However, it has yet to be merged, so I think the maintainers are still hesitant to extend this past the string type.

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

    B-rfcBlocked: More comments would be useful in determine next steps.C-featureCategory: feature. This is adding a new feature.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions