pub struct ChunkedBytes { /* private fields */ }
Expand description
A non-contiguous buffer for efficient serialization of data structures.
A ChunkedBytes
container has a staging buffer to coalesce small byte
sequences of source data, and a queue of byte chunks split off the staging
buffer that can be incrementally consumed by an output API such as an object
implementing AsyncWrite
. Once the number of bytes in the staging
buffer reaches a certain configured chunk size, the buffer content is
split off to form a new chunk.
Unlike loosely::ChunkedBytes
, this variant of the ChunkedBytes
container
never produces chunks larger than the configured size. This comes at a cost
of increased processing overhead and sometimes more allocated memory needed
to keep the buffered data, so the applications that don’t benefit from
the strict limit should prefer loosely::ChunkedBytes
.
Refer to the documentation on the methods available for ChunkedBytes
,
including the methods of traits Buf
and BufMut
, for details on working
with this container.
Implementations§
Source§impl ChunkedBytes
impl ChunkedBytes
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new ChunkedBytes
container with the chunk size limit
set to a default value.
Sourcepub fn with_chunk_size_limit(chunk_size: usize) -> Self
pub fn with_chunk_size_limit(chunk_size: usize) -> Self
Creates a new ChunkedBytes
container with the given chunk size limit.
Sourcepub fn with_profile(chunk_size: usize, chunking_capacity: usize) -> Self
pub fn with_profile(chunk_size: usize, chunking_capacity: usize) -> Self
The fully detailed constructor for ChunkedBytes
.
The chunk size limit is given in chunk_size
, and an upper
estimate of the number of chunks this container could be expected to
have at any moment of time should be given in chunking_capacity
.
More chunks can still be held, but this may cause reallocations of
internal data structures.
Sourcepub fn chunk_size_limit(&self) -> usize
pub fn chunk_size_limit(&self) -> usize
Returns the size this ChunkedBytes
container uses as the limit
for splitting off complete chunks.
Note that the size of produced chunks may be smaller than the configured value, due to the allocation strategy used internally by the implementation and also depending on the pattern of usage.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the ChunkedBytes
container has no complete chunks
and the staging buffer is empty.
Sourcepub fn flush(&mut self)
pub fn flush(&mut self)
Splits any bytes that are currently in the staging buffer into a new complete chunk. If the staging buffer is empty, this method does nothing.
Most users should not need to call this method. It is called internally when needed by the methods that advance the writing position.
Sourcepub fn put_bytes(&mut self, src: Bytes)
pub fn put_bytes(&mut self, src: Bytes)
Appends a Bytes
slice to the container without copying the data.
If src
is empty, this method does nothing. Otherwise,
if there are any bytes currently in the staging buffer, they are split
to form a complete chunk. Next, src
is appended as a sequence of
chunks, split if necessary so that all chunks except the last are
sized to the chunk size limit.
§Performance Notes
For a small slice originating from a buffer that is not split
or shared between other Bytes
instances, copying the bytes with
BufMut::put_slice
may be faster than the overhead of
atomic reference counting induced by use of this method.
Sourcepub fn drain_chunks(&mut self) -> DrainChunks<'_> ⓘ
pub fn drain_chunks(&mut self) -> DrainChunks<'_> ⓘ
Returns an iterator that removes complete chunks from the
ChunkedBytes
container and yields the removed chunks as Bytes
slice handles. This does not include bytes in the staging buffer.
The chunks are removed even if the iterator is dropped without being
consumed until the end. It is unspecified how many chunks are removed
if the DrainChunks
value is not dropped, but the borrow it holds
expires (e.g. due to std::mem::forget
).
Sourcepub fn into_chunks(self) -> IntoChunks ⓘ
pub fn into_chunks(self) -> IntoChunks ⓘ
Consumes the ChunkedBytes
container to produce an iterator over
its chunks. If there are bytes in the staging buffer, they are yielded
as the last src.
The memory allocated for IntoChunks
may be slightly more than the
ChunkedBytes
container it consumes. This is an infrequent side effect
of making the internal state efficient in general for iteration.
Trait Implementations§
Source§impl Buf for ChunkedBytes
impl Buf for ChunkedBytes
Source§fn chunk(&self) -> &[u8] ⓘ
fn chunk(&self) -> &[u8] ⓘ
Returns a slice of the bytes in the first extant complete chunk, or the bytes in the staging buffer if there are no unconsumed chunks.
It is more efficient to use chunks_vectored
to gather all the disjoint
slices for vectored output.
Source§fn advance(&mut self, cnt: usize)
fn advance(&mut self, cnt: usize)
Advances the reading position by cnt
, dropping the Bytes
references
to any complete chunks that the position has been advanced past
and then advancing the starting position of the first remaining chunk.
If there are no complete chunks left, the reading position is advanced
in the staging buffer, effectively removing the consumed bytes.
§Panics
This function may panic when cnt > self.remaining()
.
Source§fn chunks_vectored<'a>(&'a self, dst: &mut [IoSlice<'a>]) -> usize
fn chunks_vectored<'a>(&'a self, dst: &mut [IoSlice<'a>]) -> usize
Fills dst
sequentially with the slice views of the chunks, then
the bytes in the staging buffer if any remain and there is
another unfilled entry left in dst
. Returns the number of IoSlice
entries filled.
Source§fn remaining(&self) -> usize
fn remaining(&self) -> usize
Source§fn has_remaining(&self) -> bool
fn has_remaining(&self) -> bool
Source§fn copy_to_bytes(&mut self, len: usize) -> Bytes
fn copy_to_bytes(&mut self, len: usize) -> Bytes
Source§fn copy_to_slice(&mut self, dst: &mut [u8])
fn copy_to_slice(&mut self, dst: &mut [u8])
Source§fn get_u16(&mut self) -> u16
fn get_u16(&mut self) -> u16
self
in big-endian byte order. Read moreSource§fn get_u16_le(&mut self) -> u16
fn get_u16_le(&mut self) -> u16
self
in little-endian byte order. Read moreSource§fn get_i16(&mut self) -> i16
fn get_i16(&mut self) -> i16
self
in big-endian byte order. Read moreSource§fn get_i16_le(&mut self) -> i16
fn get_i16_le(&mut self) -> i16
self
in little-endian byte order. Read moreSource§fn get_u32(&mut self) -> u32
fn get_u32(&mut self) -> u32
self
in the big-endian byte order. Read moreSource§fn get_u32_le(&mut self) -> u32
fn get_u32_le(&mut self) -> u32
self
in the little-endian byte order. Read moreSource§fn get_i32(&mut self) -> i32
fn get_i32(&mut self) -> i32
self
in big-endian byte order. Read moreSource§fn get_i32_le(&mut self) -> i32
fn get_i32_le(&mut self) -> i32
self
in little-endian byte order. Read moreSource§fn get_u64(&mut self) -> u64
fn get_u64(&mut self) -> u64
self
in big-endian byte order. Read moreSource§fn get_u64_le(&mut self) -> u64
fn get_u64_le(&mut self) -> u64
self
in little-endian byte order. Read moreSource§fn get_i64(&mut self) -> i64
fn get_i64(&mut self) -> i64
self
in big-endian byte order. Read moreSource§fn get_i64_le(&mut self) -> i64
fn get_i64_le(&mut self) -> i64
self
in little-endian byte order. Read moreSource§fn get_u128(&mut self) -> u128
fn get_u128(&mut self) -> u128
self
in big-endian byte order. Read moreSource§fn get_u128_le(&mut self) -> u128
fn get_u128_le(&mut self) -> u128
self
in little-endian byte order. Read moreSource§fn get_i128(&mut self) -> i128
fn get_i128(&mut self) -> i128
self
in big-endian byte order. Read moreSource§fn get_i128_le(&mut self) -> i128
fn get_i128_le(&mut self) -> i128
self
in little-endian byte order. Read moreSource§fn get_uint(&mut self, nbytes: usize) -> u64
fn get_uint(&mut self, nbytes: usize) -> u64
self
in big-endian byte order. Read moreSource§fn get_uint_le(&mut self, nbytes: usize) -> u64
fn get_uint_le(&mut self, nbytes: usize) -> u64
self
in little-endian byte order. Read moreSource§fn get_int(&mut self, nbytes: usize) -> i64
fn get_int(&mut self, nbytes: usize) -> i64
self
in big-endian byte order. Read moreSource§fn get_int_le(&mut self, nbytes: usize) -> i64
fn get_int_le(&mut self, nbytes: usize) -> i64
self
in little-endian byte order. Read moreSource§fn get_f32(&mut self) -> f32
fn get_f32(&mut self) -> f32
self
in big-endian byte order. Read moreSource§fn get_f32_le(&mut self) -> f32
fn get_f32_le(&mut self) -> f32
self
in little-endian byte order. Read moreSource§fn get_f64(&mut self) -> f64
fn get_f64(&mut self) -> f64
self
in big-endian byte order. Read moreSource§fn get_f64_le(&mut self) -> f64
fn get_f64_le(&mut self) -> f64
self
in little-endian byte order. Read moreSource§impl BufMut for ChunkedBytes
impl BufMut for ChunkedBytes
Source§fn remaining_mut(&self) -> usize
fn remaining_mut(&self) -> usize
Source§unsafe fn advance_mut(&mut self, cnt: usize)
unsafe fn advance_mut(&mut self, cnt: usize)
Source§fn chunk_mut(&mut self) -> &mut UninitSlice
fn chunk_mut(&mut self) -> &mut UninitSlice
BufMut::remaining_mut()
. Note that this can be shorter than the
whole remainder of the buffer (this allows non-continuous implementation). Read moreSource§fn has_remaining_mut(&self) -> bool
fn has_remaining_mut(&self) -> bool
self
for more bytes. Read moreSource§fn put_u16(&mut self, n: u16)
fn put_u16(&mut self, n: u16)
self
in big-endian byte order. Read moreSource§fn put_u16_le(&mut self, n: u16)
fn put_u16_le(&mut self, n: u16)
self
in little-endian byte order. Read moreSource§fn put_i16(&mut self, n: i16)
fn put_i16(&mut self, n: i16)
self
in big-endian byte order. Read moreSource§fn put_i16_le(&mut self, n: i16)
fn put_i16_le(&mut self, n: i16)
self
in little-endian byte order. Read moreSource§fn put_u32(&mut self, n: u32)
fn put_u32(&mut self, n: u32)
self
in big-endian byte order. Read moreSource§fn put_u32_le(&mut self, n: u32)
fn put_u32_le(&mut self, n: u32)
self
in little-endian byte order. Read moreSource§fn put_i32(&mut self, n: i32)
fn put_i32(&mut self, n: i32)
self
in big-endian byte order. Read moreSource§fn put_i32_le(&mut self, n: i32)
fn put_i32_le(&mut self, n: i32)
self
in little-endian byte order. Read moreSource§fn put_u64(&mut self, n: u64)
fn put_u64(&mut self, n: u64)
self
in the big-endian byte order. Read moreSource§fn put_u64_le(&mut self, n: u64)
fn put_u64_le(&mut self, n: u64)
self
in little-endian byte order. Read moreSource§fn put_i64(&mut self, n: i64)
fn put_i64(&mut self, n: i64)
self
in the big-endian byte order. Read moreSource§fn put_i64_le(&mut self, n: i64)
fn put_i64_le(&mut self, n: i64)
self
in little-endian byte order. Read moreSource§fn put_u128(&mut self, n: u128)
fn put_u128(&mut self, n: u128)
self
in the big-endian byte order. Read moreSource§fn put_u128_le(&mut self, n: u128)
fn put_u128_le(&mut self, n: u128)
self
in little-endian byte order. Read moreSource§fn put_i128(&mut self, n: i128)
fn put_i128(&mut self, n: i128)
self
in the big-endian byte order. Read moreSource§fn put_i128_le(&mut self, n: i128)
fn put_i128_le(&mut self, n: i128)
self
in little-endian byte order. Read moreSource§fn put_uint(&mut self, n: u64, nbytes: usize)
fn put_uint(&mut self, n: u64, nbytes: usize)
self
in big-endian byte order. Read moreSource§fn put_uint_le(&mut self, n: u64, nbytes: usize)
fn put_uint_le(&mut self, n: u64, nbytes: usize)
self
in the little-endian byte order. Read moreSource§fn put_int(&mut self, n: i64, nbytes: usize)
fn put_int(&mut self, n: i64, nbytes: usize)
self
in big-endian byte order. Read moreSource§fn put_int_le(&mut self, n: i64, nbytes: usize)
fn put_int_le(&mut self, n: i64, nbytes: usize)
self
in little-endian byte order. Read moreSource§fn put_f32(&mut self, n: f32)
fn put_f32(&mut self, n: f32)
self
in big-endian byte order. Read moreSource§fn put_f32_le(&mut self, n: f32)
fn put_f32_le(&mut self, n: f32)
self
in little-endian byte order. Read moreSource§fn put_f64(&mut self, n: f64)
fn put_f64(&mut self, n: f64)
self
in big-endian byte order. Read moreSource§fn put_f64_le(&mut self, n: f64)
fn put_f64_le(&mut self, n: f64)
self
in little-endian byte order. Read more