pub struct FixedCompactBytestrings { /* private fields */ }Expand description
An even more compact but limited representation of a list of bytestrings.
Strings are stored contiguously in a vector of bytes, with their starting indices being stored separately.
Limitations include being unable to mutate bytestrings stored in the vector.
§Examples
let mut cmpbytes = FixedCompactBytestrings::with_capacity(20, 3);
cmpbytes.push(b"One");
cmpbytes.push(b"Two");
cmpbytes.push(b"Three");
cmpbytes.remove(1);
assert_eq!(cmpbytes.get(0), Some(b"One".as_slice()));
assert_eq!(cmpbytes.get(1), Some(b"Three".as_slice()));
assert_eq!(cmpbytes.get(2), None);Implementations§
Source§impl FixedCompactBytestrings
impl FixedCompactBytestrings
Sourcepub const fn new() -> Self
pub const fn new() -> Self
Constructs a new, empty FixedCompactBytestrings.
The FixedCompactBytestrings will not allocate until bytestrings are pushed into it.
§Examples
let mut cmpbytes = FixedCompactBytestrings::new();Sourcepub fn with_capacity(data_capacity: usize, capacity_meta: usize) -> Self
pub fn with_capacity(data_capacity: usize, capacity_meta: usize) -> Self
Constructs a new, empty FixedCompactBytestrings with at least the specified capacities in each
vector.
data_capacity: The capacity of the data vector where the bytes of the bytestrings are stored.capacity_meta: The capacity of the meta vector where the starting indices of the bytestrings are stored.
The FixedCompactBytestrings will be able to hold at least data_capacity bytes worth of bytestrings
without reallocating the data vector, and at least capacity_meta of starting indices
without reallocating the meta vector. This method is allowed to allocate for more bytes
than the capacities. If a capacity is 0, the vector will not allocate.
It is important to note that although the data and meta vectors have the minimum capacities specified, they will have a zero length.
If it is important to know the exact allocated capacity of the data vector, always use the
capacity method after construction.
§Examples
let mut cmpbytes = FixedCompactBytestrings::with_capacity(20, 3);
assert_eq!(cmpbytes.len(), 0);
assert!(cmpbytes.capacity() >= 20);
assert!(cmpbytes.capacity_meta() >= 3);Sourcepub fn push<S>(&mut self, bytestring: S)
pub fn push<S>(&mut self, bytestring: S)
Appends a bytestring to the back of the FixedCompactBytestrings.
§Examples
let mut cmpbytes = FixedCompactBytestrings::new();
cmpbytes.push(b"One");
cmpbytes.push(b"Two");
cmpbytes.push(b"Three");
assert_eq!(cmpbytes.get(0), Some(b"One".as_slice()));
assert_eq!(cmpbytes.get(1), Some(b"Two".as_slice()));
assert_eq!(cmpbytes.get(2), Some(b"Three".as_slice()));
assert_eq!(cmpbytes.get(3), None);Sourcepub fn get(&self, index: usize) -> Option<&[u8]>
pub fn get(&self, index: usize) -> Option<&[u8]>
Returns a reference to the bytestring stored in the FixedCompactBytestrings at that position.
§Examples
let mut cmpbytes = FixedCompactBytestrings::new();
cmpbytes.push(b"One");
cmpbytes.push(b"Two");
cmpbytes.push(b"Three");
assert_eq!(cmpbytes.get(0), Some(b"One".as_slice()));
assert_eq!(cmpbytes.get(1), Some(b"Two".as_slice()));
assert_eq!(cmpbytes.get(2), Some(b"Three".as_slice()));
assert_eq!(cmpbytes.get(3), None);Sourcepub unsafe fn get_unchecked(&self, index: usize) -> &[u8]
Available on non-crate feature no_unsafe only.
pub unsafe fn get_unchecked(&self, index: usize) -> &[u8]
no_unsafe only.Returns a reference to the bytestring stored in the FixedCompactBytestrings at that position, without
doing bounds checking.
§Safety
Calling this method with an out-of-bounds index is undefined behavior even if the resulting reference is not used.
§Examples
let mut cmpbytes = FixedCompactBytestrings::new();
cmpbytes.push(b"One");
cmpbytes.push(b"Two");
cmpbytes.push(b"Three");
unsafe {
assert_eq!(cmpbytes.get_unchecked(0), b"One".as_slice());
assert_eq!(cmpbytes.get_unchecked(1), b"Two".as_slice());
assert_eq!(cmpbytes.get_unchecked(2), b"Three".as_slice());
}Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of bytestrings in the FixedCompactBytestrings, also referred to as its ‘length’.
§Examples
let mut cmpbytes = FixedCompactBytestrings::new();
cmpbytes.push(b"One");
cmpbytes.push(b"Two");
cmpbytes.push(b"Three");
assert_eq!(cmpbytes.len(), 3);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the FixedCompactBytestrings contains no bytestrings.
§Examples
let mut cmpbytes = FixedCompactBytestrings::new();
assert!(cmpbytes.is_empty());
cmpbytes.push(b"One");
assert!(!cmpbytes.is_empty());Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the number of bytes the data vector can store without reallocating.
§Examples
let mut cmpbytes = FixedCompactBytestrings::with_capacity(20, 3);
cmpbytes.push(b"One");
assert!(cmpbytes.capacity() >= 20);Sourcepub fn capacity_meta(&self) -> usize
pub fn capacity_meta(&self) -> usize
Returns the number of starting indices can store without reallocating.
§Examples
let mut cmpbytes = FixedCompactBytestrings::with_capacity(20, 3);
cmpbytes.push(b"One");
cmpbytes.push(b"Two");
cmpbytes.push(b"Three");
assert!(cmpbytes.capacity_meta() >= 3);
cmpbytes.push(b"Three");
assert!(cmpbytes.capacity_meta() > 3);Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the FixedCompactBytestrings, removing all bytestrings.
Note that this method has no effect on the allocated capacity of the vectors.
§Examples
let mut cmpbytes = FixedCompactBytestrings::new();
cmpbytes.push(b"One");
cmpbytes.push(b"Two");
cmpbytes.push(b"Three");
cmpbytes.clear();
assert!(cmpbytes.is_empty());Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks the capacity of the data vector, which stores the bytes of the held bytestrings, as much as possible.
It will drop down as close as possible to the length but the allocator may still inform the vector that there is space for a few more elements.
§Examples
let mut cmpbytes = FixedCompactBytestrings::with_capacity(20, 3);
cmpbytes.push(b"One");
cmpbytes.push(b"Two");
cmpbytes.push(b"Three");
assert!(cmpbytes.capacity() >= 20);
cmpbytes.shrink_to_fit();
assert!(cmpbytes.capacity() >= 3);Sourcepub fn shrink_meta_to_fit(&mut self)
pub fn shrink_meta_to_fit(&mut self)
Shrinks the capacity of the info vector, which stores the starting indices of the held bytestrings, as much as possible.
It will drop down as close as possible to the length but the allocator may still inform the vector that there is space for a few more elements.
§Examples
let mut cmpbytes = FixedCompactBytestrings::with_capacity(20, 10);
cmpbytes.push(b"One");
cmpbytes.push(b"Two");
cmpbytes.push(b"Three");
assert!(cmpbytes.capacity_meta() >= 10);
cmpbytes.shrink_to_fit();
assert!(cmpbytes.capacity_meta() >= 3);Sourcepub fn shrink_to(&mut self, min_capacity: usize)
pub fn shrink_to(&mut self, min_capacity: usize)
Shrinks the capacity of the data vector, which stores the bytes of the held bytestrings, with a lower bound.
The capacity will remain at least as large as both the length and the supplied value.
If the current capacity is less than the lower limit, this is a no-op.
§Examples
let mut cmpbytes = FixedCompactBytestrings::with_capacity(20, 4);
cmpbytes.push(b"One");
cmpbytes.push(b"Two");
cmpbytes.push(b"Three");
assert!(cmpbytes.capacity() >= 20);
cmpbytes.shrink_to(4);
assert!(cmpbytes.capacity() >= 4);Sourcepub fn shrink_meta_to(&mut self, min_capacity: usize)
pub fn shrink_meta_to(&mut self, min_capacity: usize)
Shrinks the capacity of the meta vector, which starting indices of the held bytestrings, with a lower bound.
The capacity will remain at least as large as both the length and the supplied value.
If the current capacity is less than the lower limit, this is a no-op.
§Examples
let mut cmpbytes = FixedCompactBytestrings::with_capacity(20, 10);
cmpbytes.push(b"One");
cmpbytes.push(b"Two");
cmpbytes.push(b"Three");
assert!(cmpbytes.capacity_meta() >= 10);
cmpbytes.shrink_meta_to(4);
assert!(cmpbytes.capacity_meta() >= 4);Sourcepub fn remove(&mut self, index: usize)
pub fn remove(&mut self, index: usize)
Removes the bytes of the bytestring and data pointing to the bytestring is stored.
Note: This does not shrink the vectors where the bytes of the bytestring and data to the bytestring
are stored. You may shrink the data vector with shrink_to and shrink_to_fit and the
meta vector with shrink_meta_to and shrink_meta_to_fit.
Note: Because this shifts over the remaining elements in both data and meta vectors, it has a worst-case performance of O(n).
§Examples
let mut cmpbytes = FixedCompactBytestrings::with_capacity(20, 3);
cmpbytes.push(b"One");
cmpbytes.push(b"Two");
cmpbytes.push(b"Three");
cmpbytes.remove(1);
assert_eq!(cmpbytes.get(0), Some(b"One".as_slice()));
assert_eq!(cmpbytes.get(1), Some(b"Three".as_slice()));
assert_eq!(cmpbytes.get(2), None);Sourcepub fn iter(&self) -> Iter<'_>
pub fn iter(&self) -> Iter<'_>
Returns an iterator over the slice.
The iterator yields all items from start to end.
§Examples
let mut cmpbytes = FixedCompactBytestrings::with_capacity(20, 3);
cmpbytes.push(b"One");
cmpbytes.push(b"Two");
cmpbytes.push(b"Three");
let mut iterator = cmpbytes.iter();
assert_eq!(iterator.next(), Some(b"One".as_slice()));
assert_eq!(iterator.next(), Some(b"Two".as_slice()));
assert_eq!(iterator.next(), Some(b"Three".as_slice()));
assert_eq!(iterator.next(), None);Trait Implementations§
Source§impl Clone for FixedCompactBytestrings
impl Clone for FixedCompactBytestrings
Source§impl Debug for FixedCompactBytestrings
impl Debug for FixedCompactBytestrings
Source§impl<S> Extend<S> for FixedCompactBytestrings
impl<S> Extend<S> for FixedCompactBytestrings
Source§fn extend<I: IntoIterator<Item = S>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = S>>(&mut self, iter: I)
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)