Skip to content

Suggestion(V2): change Buf::chunk() -> &[u8] to Buf::nth_chunk(usize) -> &[u8] #823

@b01o

Description

@b01o

I wonder if is it possible to change chunk() to return any position of it's underlying slice instead. Currently we always returns the first chunk in a Buf, which makes it hard to visit a element not within the first chunk. To support access on later chunks, I propose that we could alter the Buf::chunk() -> &[u8] to Buf::nth_chunk(usize) -> &[u8].

trait bytes_v1::Buf {
    fn chunk(&self) -> &[u8];
    //...
}

trait bytes_v2::Buf {
    /// This function should never panic. `nth_chunk(0)` (i.e. `chunk()`)
    /// should return an empty slice **if and only if** `remaining()` returns 0.
    /// In other words, `chunk()` returning an empty slice implies that
    /// `remaining()` will return 0 and `remaining()` returning 0 implies that
    /// `chunk()` will return an empty slice.
    ///
    /// If `nth_chunk(n)` returns an empty slice, then `nth_chunk(m)` must also
    /// return an empty slice for all `m >= n`.
    fn nth_chunk(&self, nth: usize) -> &[u8];
    fn chunk(&self) -> &[u8] { self.nth_chunk(0) }
    // ...
}

Motivation:

  • easy to implement peek_* methods access later bytes without advance the buffer first, useful for many codec parsing.
trait bytes_v2::Buf {
    fn peek(&self, &mut [u8]) -> usize;
    fn peek_exact(&self, &mut [u8]) -> Result<(), TryGetError>;
    fn peek_u32(&self) -> Result<u32, TryGetError>;
    // ...
}
  • should be enable to make bytes_v1 depends on bytes_v2 with this. I don't know how exactly the "semver hack" works, but I assume it's doable as we can simply forward all bytes_v1::chunk() to bytes_v2::chunk() ?
  • enable us to optimize data structure more aggressively, since we now have the whole picture what's upfront.

Drawbacks:

  • changing interface means we need to re-implement all chunk() in current data structures, including Chain, Take and possibly more.
  • more complex/error-prone when implementing new data structures, since with bytes_v1 we only consider the first chunk.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions