pub enum FlexZeroVec<'a> {
    Owned(FlexZeroVecOwned),
    Borrowed(&'a FlexZeroSlice),
}
Expand description

A zero-copy data structure that efficiently stores integer values.

FlexZeroVec automatically increases or decreases its storage capacity based on the largest integer stored in the vector. It therefore results in lower memory usage when smaller numbers are usually stored, but larger values must sometimes also be stored.

The maximum value that can be stored in FlexZeroVec is usize::MAX on the current platform.

FlexZeroVec is the data structure for storing usize in a ZeroMap.

FlexZeroVec derefs to FlexZeroSlice, which contains most of the methods.

Examples

Storing a vec of usizes in a zero-copy way:

use zerovec::vecs::FlexZeroVec;

// Create a FlexZeroVec and add a few numbers to it
let mut zv1 = FlexZeroVec::new();
zv1.to_mut().push(55);
zv1.to_mut().push(33);
zv1.to_mut().push(999);
assert_eq!(zv1.to_vec(), vec![55, 33, 999]);

// Convert it to bytes and back
let bytes = zv1.as_bytes();
let zv2 = FlexZeroVec::parse_byte_slice(bytes)
    .expect("bytes should round-trip");
assert_eq!(zv2.to_vec(), vec![55, 33, 999]);

// Verify the compact storage
assert_eq!(7, bytes.len());
assert!(matches!(zv2, FlexZeroVec::Borrowed(_)));

Storing a map of usize to usize in a zero-copy way:

use zerovec::ZeroMap;

// Append some values to the ZeroMap
let mut zm = ZeroMap::<usize, usize>::new();
assert!(zm.try_append(&29, &92).is_none());
assert!(zm.try_append(&38, &83).is_none());
assert!(zm.try_append(&56, &65).is_none());
assert_eq!(zm.len(), 3);

// Insert another value into the middle
assert!(zm.try_append(&47, &74).is_some());
assert!(zm.insert(&47, &74).is_none());
assert_eq!(zm.len(), 4);

// Verify that the values are correct
assert_eq!(zm.get_copied(&0), None);
assert_eq!(zm.get_copied(&29), Some(92));
assert_eq!(zm.get_copied(&38), Some(83));
assert_eq!(zm.get_copied(&47), Some(74));
assert_eq!(zm.get_copied(&56), Some(65));
assert_eq!(zm.get_copied(&usize::MAX), None);

Variants

Owned(FlexZeroVecOwned)

Borrowed(&'a FlexZeroSlice)

Implementations

Creates a new, borrowed, empty FlexZeroVec.

Examples
use zerovec::vecs::FlexZeroVec;

let zv: FlexZeroVec = FlexZeroVec::new();
assert!(zv.is_empty());

Parses a &[u8] buffer into a FlexZeroVec.

The bytes within the byte buffer must remain constant for the life of the FlexZeroVec.

Endianness

The byte buffer must be encoded in little-endian, even if running in a big-endian environment. This ensures a consistent representation of data across platforms.

Max Value

The bytes will fail to parse if the high value is greater than the capacity of usize on this platform. For example, a FlexZeroVec created on a 64-bit platform might fail to deserialize on a 32-bit platform.

Example
use zerovec::vecs::FlexZeroVec;

let bytes: &[u8] = &[2, 0xD3, 0x00, 0x19, 0x01, 0xA5, 0x01, 0xCD, 0x01];
let zv = FlexZeroVec::parse_byte_slice(bytes).expect("valid slice");

assert!(matches!(zv, FlexZeroVec::Borrowed(_)));
assert_eq!(zv.get(2), Some(421));

Converts a borrowed FlexZeroVec to an owned FlexZeroVec. No-op if already owned.

Example
use zerovec::vecs::FlexZeroVec;

let bytes: &[u8] = &[2, 0xD3, 0x00, 0x19, 0x01, 0xA5, 0x01, 0xCD, 0x01];
let zv = FlexZeroVec::parse_byte_slice(bytes).expect("valid bytes");
assert!(matches!(zv, FlexZeroVec::Borrowed(_)));

let owned = zv.into_owned();
assert!(matches!(owned, FlexZeroVec::Owned(_)));

Allows the FlexZeroVec to be mutated by converting it to an owned variant, and producing a mutable FlexZeroVecOwned.

Example
use zerovec::vecs::FlexZeroVec;

let bytes: &[u8] = &[2, 0xD3, 0x00, 0x19, 0x01, 0xA5, 0x01, 0xCD, 0x01];
let mut zv = FlexZeroVec::parse_byte_slice(bytes).expect("valid bytes");
assert!(matches!(zv, FlexZeroVec::Borrowed(_)));

zv.to_mut().push(12);
assert!(matches!(zv, FlexZeroVec::Owned(_)));
assert_eq!(zv.get(4), Some(12));

Remove all elements from this FlexZeroVec and reset it to an empty borrowed state.

Examples
use zerovec::vecs::FlexZeroVec;

let mut zv: FlexZeroVec = [1, 2, 3].iter().copied().collect();
assert!(!zv.is_empty());
zv.clear();
assert!(zv.is_empty());

Methods from Deref<Target = FlexZeroSlice>

Returns this slice as its underlying &[u8] byte buffer representation.

Useful for serialization.

Example
use zerovec::vecs::FlexZeroSlice;

let bytes: &[u8] = &[2, 0xD3, 0x00, 0x19, 0x01, 0xA5, 0x01, 0xCD, 0x80];
let fzv = FlexZeroSlice::parse_byte_slice(bytes).expect("valid bytes");

assert_eq!(bytes, fzv.as_bytes());

Borrows this FlexZeroSlice as a FlexZeroVec::Borrowed.

Returns the number of elements in the FlexZeroSlice.

Returns whether there are zero elements in the FlexZeroSlice.

Gets the element at index, or None if index >= self.len().

Examples
use zerovec::vecs::FlexZeroVec;

let fzv: FlexZeroVec = [22, 33].iter().copied().collect();
assert_eq!(fzv.get(0), Some(22));
assert_eq!(fzv.get(1), Some(33));
assert_eq!(fzv.get(2), None);

Gets the element at index without checking bounds.

Safety

index must be in-range.

Gets the first element of the slice, or None if the slice is empty.

Gets the last element of the slice, or None if the slice is empty.

Gets an iterator over the elements of the slice as usize.

Gets an iterator over pairs of elements.

The second element of the final pair is None.

Examples
use zerovec::vecs::FlexZeroVec;

let nums: &[usize] = &[211, 281, 421, 461];
let fzv: FlexZeroVec = nums.iter().copied().collect();

let mut pairs_it = fzv.iter_pairs();

assert_eq!(pairs_it.next(), Some((211, Some(281))));
assert_eq!(pairs_it.next(), Some((281, Some(421))));
assert_eq!(pairs_it.next(), Some((421, Some(461))));
assert_eq!(pairs_it.next(), Some((461, None)));
assert_eq!(pairs_it.next(), None);

Creates a Vec<usize> from a FlexZeroSlice (or FlexZeroVec).

Examples
use zerovec::vecs::FlexZeroVec;

let nums: &[usize] = &[211, 281, 421, 461];
let fzv: FlexZeroVec = nums.iter().copied().collect();
let vec: Vec<usize> = fzv.to_vec();

assert_eq!(nums, vec.as_slice());

Binary searches a sorted FlexZeroSlice for the given usize value.

Examples
use zerovec::vecs::FlexZeroVec;

let nums: &[usize] = &[211, 281, 421, 461];
let fzv: FlexZeroVec = nums.iter().copied().collect();

assert_eq!(fzv.binary_search(0), Err(0));
assert_eq!(fzv.binary_search(211), Ok(0));
assert_eq!(fzv.binary_search(250), Err(1));
assert_eq!(fzv.binary_search(281), Ok(1));
assert_eq!(fzv.binary_search(300), Err(2));
assert_eq!(fzv.binary_search(421), Ok(2));
assert_eq!(fzv.binary_search(450), Err(3));
assert_eq!(fzv.binary_search(461), Ok(3));
assert_eq!(fzv.binary_search(462), Err(4));

Binary searches a sorted range of a FlexZeroSlice for the given usize value.

The indices in the return value are relative to the start of the range.

Examples
use zerovec::vecs::FlexZeroVec;

// Make a FlexZeroVec with two sorted ranges: 0..3 and 3..5
let nums: &[usize] = &[111, 222, 444, 333, 555];
let fzv: FlexZeroVec = nums.iter().copied().collect();

// Search in the first range:
assert_eq!(fzv.binary_search_in_range(0, 0..3), Some(Err(0)));
assert_eq!(fzv.binary_search_in_range(111, 0..3), Some(Ok(0)));
assert_eq!(fzv.binary_search_in_range(199, 0..3), Some(Err(1)));
assert_eq!(fzv.binary_search_in_range(222, 0..3), Some(Ok(1)));
assert_eq!(fzv.binary_search_in_range(399, 0..3), Some(Err(2)));
assert_eq!(fzv.binary_search_in_range(444, 0..3), Some(Ok(2)));
assert_eq!(fzv.binary_search_in_range(999, 0..3), Some(Err(3)));

// Search in the second range:
assert_eq!(fzv.binary_search_in_range(0, 3..5), Some(Err(0)));
assert_eq!(fzv.binary_search_in_range(333, 3..5), Some(Ok(0)));
assert_eq!(fzv.binary_search_in_range(399, 3..5), Some(Err(1)));
assert_eq!(fzv.binary_search_in_range(555, 3..5), Some(Ok(1)));
assert_eq!(fzv.binary_search_in_range(999, 3..5), Some(Err(2)));

// Out-of-bounds range:
assert_eq!(fzv.binary_search_in_range(0, 4..6), None);

Binary searches a sorted FlexZeroSlice according to a predicate function.

Binary searches a sorted range of a FlexZeroSlice according to a predicate function.

The indices in the return value are relative to the start of the range.

Binary searches a FlexZeroSlice by its indices.

The predicate function is passed in-bounds indices into the FlexZeroSlice.

Binary searches a range of a FlexZeroSlice by its indices.

The predicate function is passed in-bounds indices into the FlexZeroSlice, which are relative to the start of the entire slice.

The indices in the return value are relative to the start of the range.

Trait Implementations

Converts this type into a shared reference of the (usually inferred) input type.

Returns a TokenStream that would evalutate to self. Read more

Formats the value using the given formatter. Read more

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

The resulting type after dereferencing.

Dereferences the value.

This impl can be made available by enabling the optional serde feature of the zerovec crate

Deserialize this value from the given Serde deserializer. Read more

Creates a FlexZeroVec::Owned from an iterator of usize.

The type returned by Self::remove() and Self::replace()

Insert an element at index

Remove the element at index (panicking if nonexistant)

Replace the element at index with another one, returning the old element

Push an element to the end of this vector

Create a new, empty vector, with given capacity

Remove all elements from the vector

Reserve space for addl additional elements

Convert an owned value to a borrowed T

Construct from the borrowed version of the type Read more

Extract the inner borrowed variant if possible. Returns None if the data is owned. Read more

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

This impl can be made available by enabling the optional serde feature of the zerovec crate

Serialize this value into the given Serde serializer. Read more

This impl can be made available by enabling the optional yoke feature of the zerovec crate

This type MUST be Self with the 'static replaced with 'a, i.e. Self<'a>

This method must cast self between &'a Self<'static> and &'a Self<'a>. Read more

This method must cast self between Self<'static> and Self<'a>. Read more

This method can be used to cast away Self<'a>’s lifetime. Read more

This method must cast self between &'a mut Self<'static> and &'a mut Self<'a>, and pass it to f. Read more

Clone the other C into a struct that may retain references into C.

Clone the other C into a struct that may retain references into C.

The type returned by Self::get()

A fully borrowed version of this

Create a new, empty borrowed variant

Search for a key in a sorted vector, returns Ok(index) if found, returns Err(insert_index) if not found, where insert_index is the index where it should be inserted to maintain sort order. Read more

Search for a key within a certain range in a sorted vector. Returns None if the range is out of bounds, and Ok or Err in the same way as zvl_binary_search. Indices are returned relative to the start of the range. Read more

Search for a key in a sorted vector by a predicate, returns Ok(index) if found, returns Err(insert_index) if not found, where insert_index is the index where it should be inserted to maintain sort order. Read more

Search for a key within a certain range in a sorted vector by a predicate. Returns None if the range is out of bounds, and Ok or Err in the same way as zvl_binary_search. Indices are returned relative to the start of the range. Read more

Get element at index

The length of this vector

Construct a borrowed variant by borrowing from &self. Read more

Obtain a reference to T, passed to a closure Read more

Check if this vector is in ascending order according to Ts Ord impl

Check if this vector is empty

Compare this type with a Self::GetType. This must produce the same result as if g were converted to Self Read more

Compare two values of Self::GetType. This must produce the same result as if both a and b were converted to Self Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.