Struct bytes_utils::SegmentedBuf[][src]

pub struct SegmentedBuf<B> { /* fields omitted */ }

A concatenation of multiple buffers into a large one, without copying the bytes over.

Note that this doesn't provide a continuous slice view into them, it is split into the segments of the original smaller buffers.

This variants drop the inner buffers as they are exhausted and new ones can be added. But it internally keeps a VecDeque, therefore needs a heap allocation. If you don't need the extending behaviour, but want to avoid the allocation, the SegmentedSlice can be used instead.

Why

This can be used, for example, if data of unknown length is coming over the network (for example, the bodies in hyper act a bit like this, it returns a stream of Bytes buffers). One might want to accumulate the whole body before acting on it, possibly by parsing it through serde or prost. Options would include:

  • Have a Vec<u8> and extend it with each chunk. This needlessly copy the bytes every time and reallocates if the vector grows too large.
  • Repeatedly use chain, but this changes the type of the whole buffer, therefore needs to be boxed.
  • Use hyper::body::aggregate to create a Buf implementation that concatenates all of them together, but lacks any kind of flexibility (like protecting against loading too much data into memory).

This type allows for concatenating multiple buffers, either all at once, or by incrementally pushing more buffers to the end.

Heterogeneous buffers

This expects all the buffers are of the same type. If different-typed buffers are needed, one needs to use dynamic dispatch, either something like SegmentedBuf<Box<Buf>> or SegmentedBuf<&mut Buf>.

Example

let mut buf = SegmentedBuf::new();
buf.push(Bytes::from("Hello"));
buf.push(Bytes::from(" "));
buf.push(Bytes::from("World"));

assert_eq!(3, buf.segments());
assert_eq!(11, buf.remaining());
assert_eq!(b"Hello", buf.chunk());

let mut out = String::new();
buf.reader().read_to_string(&mut out).expect("Doesn't cause IO errors");
assert_eq!("Hello World", out);

FIFO behaviour

The buffers are dropped once their data are completely consumed. Additionally, it is possible to add more buffers to the end, even while some of the previous buffers were partially or fully consumed. That makes it usable as kind of a queue (that operates on the buffers, not individual bytes).

let mut buf = SegmentedBuf::new();
buf.push(Bytes::from("Hello"));
assert_eq!(1, buf.segments());

let mut out = [0; 3];
buf.copy_to_slice(&mut out);
assert_eq!(&out, b"Hel");
assert_eq!(2, buf.remaining());
assert_eq!(1, buf.segments());

buf.push(Bytes::from("World"));
assert_eq!(7, buf.remaining());
assert_eq!(2, buf.segments());

buf.copy_to_slice(&mut out);
assert_eq!(&out, b"loW");
assert_eq!(4, buf.remaining());
assert_eq!(1, buf.segments());

Optimizations

The copy_to_bytes method tries to avoid copies by delegating into the underlying buffer if possible (if the whole request can be fulfilled using only a single buffer). If that one is optimized (for example, the Bytes returns a shared instance instead of making a copy), the copying is avoided. If the request is across a buffer boundary, a copy is made.

The chunks_vectored will properly output as many slices as possible, not just 1 as the default implementation does.

Implementations

impl<B> SegmentedBuf<B>[src]

pub fn new() -> Self[src]

Creates a new empty instance.

The instance can be pushed or extended later.

Alternatively, one may create it directly from an iterator, a Vec or a VecDeque of buffers.

pub fn into_inner(self) -> VecDeque<B>[src]

Returns the yet unconsumed sequence of buffers.

pub fn segments(&self) -> usize[src]

Returns the number of segments (buffers) this contains.

impl<B: Buf> SegmentedBuf<B>[src]

pub fn push(&mut self, buf: B)[src]

Extends the buffer by another segment.

The newly added segment is added to the end of the buffer (the buffer works as a FIFO).

Trait Implementations

impl<B: Buf> Buf for SegmentedBuf<B>[src]

impl<B: Clone> Clone for SegmentedBuf<B>[src]

impl<B: Debug> Debug for SegmentedBuf<B>[src]

impl<B> Default for SegmentedBuf<B>[src]

impl<B: Buf> Extend<B> for SegmentedBuf<B>[src]

impl<B: Buf> From<Vec<B, Global>> for SegmentedBuf<B>[src]

impl<B: Buf> From<VecDeque<B>> for SegmentedBuf<B>[src]

impl<B: Buf> FromIterator<B> for SegmentedBuf<B>[src]

Auto Trait Implementations

impl<B> RefUnwindSafe for SegmentedBuf<B> where
    B: RefUnwindSafe
[src]

impl<B> Send for SegmentedBuf<B> where
    B: Send
[src]

impl<B> Sync for SegmentedBuf<B> where
    B: Sync
[src]

impl<B> Unpin for SegmentedBuf<B> where
    B: Unpin
[src]

impl<B> UnwindSafe for SegmentedBuf<B> where
    B: UnwindSafe
[src]

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.