SeqBytes

Struct SeqBytes 

Source
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

Source

pub fn new() -> Self

Create a new SeqBytes

Source

pub fn is_empty(&self) -> bool

Check if the sequence is empty

Source

pub fn len(&self) -> usize

Get the number of slices in the sequence

Source

pub fn reserve(&mut self, extra: usize)

Reserve capacity for more slices

Source

pub fn shrink_to_fit(&mut self)

Shrink container to fit the current data

Source

pub fn num_bytes(&self) -> usize

Get the sum of the lengths of the byte strings

Source

pub fn get(&self, idx: usize) -> Option<&[u8]>

Get the i’th element of the sequence in a checked manner

Source

pub fn get_mut(&mut self, idx: usize) -> Option<&mut [u8]>

Get the i’th element of the sequence in a checked manner

Source

pub fn contains(&self, s: impl AsRef<[u8]>) -> bool

Check if the sequence contains a particular element

Source

pub fn push(&mut self, s: impl AsRef<[u8]>)

Push a &u8 onto the sequence

Source

pub fn last(&self) -> Option<&[u8]>

Get the last &u8 of the sequence

Source

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.

Source

pub fn iter(&self) -> SeqBytesIter<'_>

Iterate over the sequence of &u8

Source

pub fn iter_mut(&mut self) -> SeqBytesIterMut<'_>

Iterate over the sequence of &mut u8

Source

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].

Source

pub fn range_mut( &mut self, range_bounds: impl RangeBounds<usize>, ) -> SeqBytesIterMut<'_>

Iterate over a range of the sequence of &mut [u8]

Source

pub fn truncate(&mut self, new_size: usize)

Truncate to at most the first n slices

Source

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

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn concat(&self) -> &[u8]

Concatenate the &[u8] in the sequence into one &[u8]

Trait Implementations§

Source§

impl Clone for SeqBytes

Source§

fn clone(&self) -> SeqBytes

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SeqBytes

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for SeqBytes

Source§

fn default() -> SeqBytes

Returns the “default value” for a type. Read more
Source§

impl<A: AsRef<[u8]>> Extend<A> for SeqBytes

Source§

fn extend<T>(&mut self, iter: T)
where T: IntoIterator<Item = A>,

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<A: AsRef<[u8]>> FromIterator<A> for SeqBytes

Source§

fn from_iter<T>(iter: T) -> Self
where T: IntoIterator<Item = A>,

Creates a value from an iterator. Read more
Source§

impl Hash for SeqBytes

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Index<usize> for SeqBytes

Source§

type Output = [u8]

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &[u8]

Performs the indexing (container[index]) operation. Read more
Source§

impl IndexMut<usize> for SeqBytes

Source§

fn index_mut(&mut self, index: usize) -> &mut [u8]

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<'a> IntoIterator for &'a SeqBytes

Source§

type Item = &'a [u8]

The type of the elements being iterated over.
Source§

type IntoIter = SeqBytesIter<'a>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl PartialEq for SeqBytes

Source§

fn eq(&self, other: &SeqBytes) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for SeqBytes

Source§

impl StructuralPartialEq for SeqBytes

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.