pub struct SeqBytes { /* private fields */ }Expand description
A sequence of &[u8], stored contiguously
This can be used as a drop-in replacement for Vec<Vec<u8>> in some cases,
with better memory locality and fewer memory allocations.
When using SeqBytes instead of Vec<Vec<u8>>, the individual byte strings
cannot be resized, but when this isn’t needed there isn’t much downside otherwise.
The container also supports “emplace”-style APIs like in_place_writer, which allow you to
write the next element directly into the contiguous buffer with minimal overhead.
Implementations§
Source§impl SeqBytes
impl SeqBytes
Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrink container to fit the current data
Sourcepub fn get(&self, idx: usize) -> Option<&[u8]>
pub fn get(&self, idx: usize) -> Option<&[u8]>
Get the i’th element of the sequence in a checked manner
Sourcepub fn get_mut(&mut self, idx: usize) -> Option<&mut [u8]>
pub fn get_mut(&mut self, idx: usize) -> Option<&mut [u8]>
Get the i’th element of the sequence in a checked manner
Sourcepub fn contains(&self, s: impl AsRef<[u8]>) -> bool
pub fn contains(&self, s: impl AsRef<[u8]>) -> bool
Check if the sequence contains a particular element
Sourcepub fn pop(&mut self)
pub fn pop(&mut self)
Pop the last element of the container Note that we can’t return it because of lifetimes, so call [last] before popping.
Sourcepub fn iter(&self) -> SeqBytesIter<'_> ⓘ
pub fn iter(&self) -> SeqBytesIter<'_> ⓘ
Iterate over the sequence of &u8
Sourcepub fn iter_mut(&mut self) -> SeqBytesIterMut<'_> ⓘ
pub fn iter_mut(&mut self) -> SeqBytesIterMut<'_> ⓘ
Iterate over the sequence of &mut u8
Sourcepub fn chunks(&self, chunk_size: usize) -> SeqBytesChunksIter<'_> ⓘ
pub fn chunks(&self, chunk_size: usize) -> SeqBytesChunksIter<'_> ⓘ
Iterate over the sequence of &u8, in chunks of given size. Returns an iterator which yields one iterator for each chunk. The last chunk may be smaller.
Sourcepub fn range(&self, range_bounds: impl RangeBounds<usize>) -> SeqBytesIter<'_> ⓘ
pub fn range(&self, range_bounds: impl RangeBounds<usize>) -> SeqBytesIter<'_> ⓘ
Iterate over a range of the sequence of &[u8]
This resembles std::collections::BTreeMap::range, and is needed becuase like BTreeMap,
we can’t implement Deref or SliceIndex<Range> and produce a slice of our contents.
See also [as_vec].
Sourcepub fn range_mut(
&mut self,
range_bounds: impl RangeBounds<usize>,
) -> SeqBytesIterMut<'_> ⓘ
pub fn range_mut( &mut self, range_bounds: impl RangeBounds<usize>, ) -> SeqBytesIterMut<'_> ⓘ
Iterate over a range of the sequence of &mut [u8]
Sourcepub fn resize(&mut self, new_size: usize)
pub fn resize(&mut self, new_size: usize)
Resize to contain only the first n &[u8], or pad up to n slices, with empty slices added
Sourcepub fn retain(&mut self, pred: impl FnMut(&[u8]) -> bool)
pub fn retain(&mut self, pred: impl FnMut(&[u8]) -> bool)
Retain only those slices satisfying a predicate. The slices are always visited in order, similar to std::vec::Vec::retain.
Sourcepub fn retain_mut(&mut self, pred: impl FnMut(&mut [u8]) -> bool)
pub fn retain_mut(&mut self, pred: impl FnMut(&mut [u8]) -> bool)
Retain only those slices satisfying a predicate. The slices are always visited in order, similar to std::vec::Vec::retain_mut.
Sourcepub fn in_place_writer(&mut self) -> impl Write
pub fn in_place_writer(&mut self) -> impl Write
Get an impl std::io::Write which can be used to write the next slice
directly into the buffer without copying.
Sourcepub fn in_place_writer_no_std(&mut self) -> impl FnMut(&[u8])
pub fn in_place_writer_no_std(&mut self) -> impl FnMut(&[u8])
Version of in_place_writer that doesn’t require std::io::Write trait
Any bytes passed to the result of this function get concatenated to produce the newest byte string in the sequence. The new item is final when the writer is dropped.
Sourcepub fn as_vec(&self) -> Vec<&[u8]>
pub fn as_vec(&self) -> Vec<&[u8]>
Express as a Vec<&u8>. The main reason that this may be useful is that there are
useful methods on slice types &[&[u8]], for example, [core::slice::binary_search],
but SeqBytes itself doesn’t implement Deref the way that Vec does and can
only produce such a slice by allocating.
Note: The trade-offs are that we would need more memory and in_place_writer would have
to be more complicated and slower if we wanted our internal representation of the offsets
to be a Vec<&[u8]>, which would allow such a Deref implementation.
The direction we’ve taken is to add useful functions from Vec and slice
as needed directly to this type instead, if they can’t be obtained in a simpler way.
Trait Implementations§
impl Eq for SeqBytes
Source§impl<A: AsRef<[u8]>> Extend<A> for SeqBytes
impl<A: AsRef<[u8]>> Extend<A> for SeqBytes
Source§fn extend<T>(&mut self, iter: T)where
T: IntoIterator<Item = A>,
fn extend<T>(&mut self, iter: T)where
T: IntoIterator<Item = A>,
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)