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.
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 aBuf, 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 theBuf::chunk() -> &[u8]toBuf::nth_chunk(usize) -> &[u8].Motivation:
bytes_v1depends onbytes_v2with this. I don't know how exactly the "semver hack" works, but I assume it's doable as we can simply forward allbytes_v1::chunk()tobytes_v2::chunk()?Drawbacks:
chunk()in current data structures, includingChain,Takeand possibly more.bytes_v1we only consider the first chunk.