Skip to content

Remove Chunk in favor of using Bytes directly #1931

Closed
@abonander

Description

@abonander

Compared to Bytes, Chunk is pointless and kinda frustrating:

  • It's a trivial wrapper around Bytes but Bytes is exposed via the From/Into impl anyways so it's not like it's keeping it out of the public API
  • Chunk adds 0 new functionality over Bytes, all of its methods forwards to Bytes anyway
  • There's no way to split a Chunk without round-tripping through Bytes
  • Any third-party crate that wants to build on Stream<Item = Bytes> needs special adapters to work with hyper::Body

That last one is a real problem for me because I'm finally updating multipart-async and trying to maximize interop by generifying the API as much as possible. Because hyper::Body doesn't implement AsyncRead I've instead standardized around a Stream<Item = impl BodyChunk> API where BodyChunk covers two operations: splitting and dereffing to &[u8]. My final constraint is that I want to be able to return the same impl BodyChunk type, mostly to make it easy to drop multipart-async into existing request handlers.

Without implementing BodyChunk specifically for hyper::Chunk while still having an applicable impl for it, I would need an impl that's generic over T: From<Bytes>, Bytes: From<T>, T: AsRef<[u8]>. My hangup on this is that I now cannot implement BodyChunk for &[u8] directly, which complicates testing as I have to convert all bytestring literals to Bytes. (I also want to avoid a generic impl based on Into<Bytes> as that operation may perform implicit copies and I'm trying to be near-zero-copy.)

I realize Chunk exists because it used to be a custom implementation of Bytes but at this point it can be changed to just be a type alias and basically nothing will break (except usages of Chunk::into_bytes(), ironically).

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-bodyArea: body streaming.B-rfcBlocked: More comments would be useful in determine next steps.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions