#[repr(transparent)]
pub struct VarZeroSlice<T: ?Sized> { /* private fields */ }
Expand description

A zero-copy “slice”, that works for unsized types, i.e. the zero-copy version of [T] where T is not Sized.

This behaves similarly to VarZeroVec<T>, however VarZeroVec<T> is allowed to contain owned data and as such is ideal for deserialization since most human readable serialization formats cannot unconditionally deserialize zero-copy.

This type can be nested within VarZeroVec<T> to allow for multi-level nested Vecs, for example the following code constructs the conceptual zero-copy equivalent of Vec<Vec<Vec<str>>>

use zerovec::VarZeroVec;
use zerovec::ZeroVec;
use zerovec::varzerovec::VarZeroSlice;
use zerovec::ule::*;
let strings_1: Vec<&str> = vec!["foo", "bar", "baz"];
let strings_2: Vec<&str> = vec!["twelve", "seventeen", "forty two"];
let strings_3: Vec<&str> = vec!["我", "喜歡", "烏龍茶"];
let strings_4: Vec<&str> = vec!["w", "ω", "文", "𑄃"];
let strings_12 = vec![strings_1.clone(), strings_2.clone()];
let strings_34 = vec![strings_3.clone(), strings_4.clone()];
let all_strings = vec![strings_12, strings_34];

let vzv_1: VarZeroVec<str> = VarZeroVec::from(&strings_1);
let vzv_2: VarZeroVec<str> = VarZeroVec::from(&strings_2);
let vzv_3: VarZeroVec<str> = VarZeroVec::from(&strings_3);
let vzv_4: VarZeroVec<str> = VarZeroVec::from(&strings_4);
let vzv_12 = VarZeroVec::from(&[vzv_1.as_slice(), vzv_2.as_slice()]);
let vzv_34 = VarZeroVec::from(&[vzv_3.as_slice(), vzv_4.as_slice()]);
let vzv_all = VarZeroVec::from(&[vzv_12.as_slice(), vzv_34.as_slice()]);

let reconstructed: Vec<Vec<Vec<String>>> = vzv_all.iter()
       .map(|v: &VarZeroSlice<VarZeroSlice<str>>| {
            v.iter().map(|x: &VarZeroSlice<_>| x.as_varzerovec().iter().map(|s| s.to_owned()).collect::<Vec<String>>())
             .collect::<Vec<_>>()
        }).collect::<Vec<_>>();
assert_eq!(reconstructed, all_strings);

let bytes = vzv_all.as_bytes();
let vzv_from_bytes: VarZeroVec<VarZeroSlice<VarZeroSlice<str>>> = VarZeroVec::parse_byte_slice(bytes).unwrap();
assert_eq!(vzv_from_bytes, vzv_all);

Implementations

Construct a new empty VarZeroSlice

Get the number of elements in this slice

Example

let strings = vec!["foo", "bar", "baz", "quux"];
let vec = VarZeroVec::<str>::from(&strings);

assert_eq!(vec.len(), 4);

Returns true if the slice contains no elements.

Examples

let strings: Vec<String> = vec![];
let vec = VarZeroVec::<str>::from(&strings);

assert!(vec.is_empty());

Obtain an iterator over this slice’s elements

Example

let strings = vec!["foo", "bar", "baz", "quux"];
let vec = VarZeroVec::<str>::from(&strings);

let mut iter_results: Vec<&str> = vec.iter().collect();
assert_eq!(iter_results[0], "foo");
assert_eq!(iter_results[1], "bar");
assert_eq!(iter_results[2], "baz");
assert_eq!(iter_results[3], "quux");

Get one of this slice’s elements, returning None if the index is out of bounds

Example

let strings = vec!["foo", "bar", "baz", "quux"];
let vec = VarZeroVec::<str>::from(&strings);

let mut iter_results: Vec<&str> = vec.iter().collect();
assert_eq!(vec.get(0), Some("foo"));
assert_eq!(vec.get(1), Some("bar"));
assert_eq!(vec.get(2), Some("baz"));
assert_eq!(vec.get(3), Some("quux"));
assert_eq!(vec.get(4), None);

Obtain an owned Vec<Box<T>> out of this

Get a reference to the entire encoded backing buffer of this slice

The bytes can be passed back to Self::parse_byte_slice().

To take the bytes as a vector, see VarZeroVec::into_bytes().

Example

let strings = vec!["foo", "bar", "baz"];
let vzv = VarZeroVec::<str>::from(&strings);

assert_eq!(vzv, VarZeroVec::parse_byte_slice(vzv.as_bytes()).unwrap());

Get this VarZeroSlice as a borrowed VarZeroVec

If you wish to repeatedly call methods on this VarZeroSlice, it is more efficient to perform this conversion first

Parse a VarZeroSlice from a slice of the appropriate format

Slices of the right format can be obtained via VarZeroSlice::as_bytes()

Binary searches a sorted VarZeroVec<T> for the given element. For more information, see the standard library function binary_search.

Example

let strings = vec!["a", "b", "f", "g"];
let vec = VarZeroVec::<str>::from(&strings);

assert_eq!(vec.binary_search("f"), Ok(2));
assert_eq!(vec.binary_search("e"), Err(2));

Binary searches a VarZeroVec<T> for the given element within a certain sorted range.

If the range is out of bounds, returns None. Otherwise, returns a Result according to the behavior of the standard library function binary_search.

The index is returned relative to the start of the range.

Example

let strings = vec!["a", "b", "f", "g", "m", "n", "q"];
let vec = VarZeroVec::<str>::from(&strings);

// Same behavior as binary_search when the range covers the whole slice:
assert_eq!(vec.binary_search_in_range("g", 0..7), Some(Ok(3)));
assert_eq!(vec.binary_search_in_range("h", 0..7), Some(Err(4)));

// Will not look outside of the range:
assert_eq!(vec.binary_search_in_range("g", 0..1), Some(Err(1)));
assert_eq!(vec.binary_search_in_range("g", 6..7), Some(Err(0)));

// Will return indices relative to the start of the range:
assert_eq!(vec.binary_search_in_range("g", 1..6), Some(Ok(2)));
assert_eq!(vec.binary_search_in_range("h", 1..6), Some(Err(3)));

// Will return None if the range is out of bounds:
assert_eq!(vec.binary_search_in_range("x", 100..200), None);
assert_eq!(vec.binary_search_in_range("x", 0..200), None);

Trait Implementations

Performs the conversion.

Get element at index, with a longer lifetime

Formats the value using the given formatter. Read more

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

Calls cb with a piecewise list of byte slices that when concatenated produce the memory pattern of the corresponding instance of T. Read more

Return the length, in bytes, of the corresponding VarULE type

Write the corresponding VarULE type to the dst buffer. dst should be the size of Self::encode_var_ule_len() Read more

Calls cb with a piecewise list of byte slices that when concatenated produce the memory pattern of the corresponding instance of T. Read more

Return the length, in bytes, of the corresponding VarULE type

Write the corresponding VarULE type to the dst buffer. dst should be the size of Self::encode_var_ule_len() Read more

Calls cb with a piecewise list of byte slices that when concatenated produce the memory pattern of the corresponding instance of T. Read more

Return the length, in bytes, of the corresponding VarULE type

Write the corresponding VarULE type to the dst buffer. dst should be the size of Self::encode_var_ule_len() Read more

Performs the conversion.

Performs the conversion.

The returned type after indexing.

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

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

This method tests for !=.

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

Validates a byte slice, &[u8]. Read more

Takes a byte slice, &[u8], and return it as &Self with the same lifetime, assuming that this byte slice has previously been run through Self::parse_byte_slice() with success. Read more

Given &Self, returns a &[u8] with the same lifetime. Read more

Parses a byte slice, &[u8], and return it as &Self with the same lifetime. Read more

Allocate on the heap as a Box<T>

The type returned by Self::get()

A fully borrowed version of this

Create a new, empty vector

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

Get element at index

The length of this vector

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

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

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

Construct from the borrowed version of the type Read more

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

Obtain a version of T suitable for serialization Read more

Check if this vector is empty

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