Struct bytes_utils::SegmentedBuf [−][src]
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]
fn remaining(&self) -> usize[src]
fn chunk(&self) -> &[u8][src]
fn advance(&mut self, mut cnt: usize)[src]
fn copy_to_bytes(&mut self, len: usize) -> Bytes[src]
fn chunks_vectored<'a>(&'a self, dst: &mut [IoSlice<'a>]) -> usize[src]
pub fn has_remaining(&self) -> bool[src]
pub fn copy_to_slice(&mut self, dst: &mut [u8])[src]
pub fn get_u8(&mut self) -> u8[src]
pub fn get_i8(&mut self) -> i8[src]
pub fn get_u16(&mut self) -> u16[src]
pub fn get_u16_le(&mut self) -> u16[src]
pub fn get_i16(&mut self) -> i16[src]
pub fn get_i16_le(&mut self) -> i16[src]
pub fn get_u32(&mut self) -> u32[src]
pub fn get_u32_le(&mut self) -> u32[src]
pub fn get_i32(&mut self) -> i32[src]
pub fn get_i32_le(&mut self) -> i32[src]
pub fn get_u64(&mut self) -> u64[src]
pub fn get_u64_le(&mut self) -> u64[src]
pub fn get_i64(&mut self) -> i64[src]
pub fn get_i64_le(&mut self) -> i64[src]
pub fn get_u128(&mut self) -> u128[src]
pub fn get_u128_le(&mut self) -> u128[src]
pub fn get_i128(&mut self) -> i128[src]
pub fn get_i128_le(&mut self) -> i128[src]
pub fn get_uint(&mut self, nbytes: usize) -> u64[src]
pub fn get_uint_le(&mut self, nbytes: usize) -> u64[src]
pub fn get_int(&mut self, nbytes: usize) -> i64[src]
pub fn get_int_le(&mut self, nbytes: usize) -> i64[src]
pub fn get_f32(&mut self) -> f32[src]
pub fn get_f32_le(&mut self) -> f32[src]
pub fn get_f64(&mut self) -> f64[src]
pub fn get_f64_le(&mut self) -> f64[src]
pub fn take(self, limit: usize) -> Take<Self>[src]
pub fn chain<U>(self, next: U) -> Chain<Self, U> where
U: Buf, [src]
U: Buf,
pub fn reader(self) -> Reader<Self>[src]
impl<B: Clone> Clone for SegmentedBuf<B>[src]
fn clone(&self) -> SegmentedBuf<B>[src]
pub fn clone_from(&mut self, source: &Self)1.0.0[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]
fn extend<T: IntoIterator<Item = B>>(&mut self, iter: T)[src]
pub fn extend_one(&mut self, item: A)[src]
pub fn extend_reserve(&mut self, additional: usize)[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]
fn from_iter<T: IntoIterator<Item = B>>(iter: T) -> Self[src]
Auto Trait Implementations
impl<B> RefUnwindSafe for SegmentedBuf<B> where
B: RefUnwindSafe, [src]
B: RefUnwindSafe,
impl<B> Send for SegmentedBuf<B> where
B: Send, [src]
B: Send,
impl<B> Sync for SegmentedBuf<B> where
B: Sync, [src]
B: Sync,
impl<B> Unpin for SegmentedBuf<B> where
B: Unpin, [src]
B: Unpin,
impl<B> UnwindSafe for SegmentedBuf<B> where
B: UnwindSafe, [src]
B: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized, [src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized, [src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized, [src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T[src]
impl<T> From<T> for T[src]
impl<T, U> Into<U> for T where
U: From<T>, [src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone, [src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T[src]
pub fn clone_into(&self, target: &mut T)[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>, [src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>, [src]
U: TryFrom<T>,