Struct zerovec::VarZeroVec [−][src]
pub struct VarZeroVec<'a, T>(_);
Expand description
A zero-copy vector for variable-width types.
VarZeroVec<T>
is designed as a drop-in replacement for Vec<T>
in situations where it is
desirable to borrow data from an unaligned byte slice, such as zero-copy deserialization, and
where T
’s data is variable-length (e.g. String
)
T
must implement AsVarULE
, which is already implemented for String
. References are obtained
as its AsVarULE::VarULE
type, which in the case of String
is str
.
VarZeroVec<T>
behaves much like std::borrow::Cow
, where it can be constructed from owned data
but can also borrow from some buffer.
How it Works
VarZeroVec<T>
, when used with non-human-readable serializers (like bincode
), will
serialize to a specially formatted list of bytes. The format is:
- 4 bytes for
length
(interpreted as a little-endian u32) 4 * length
bytes ofindices
(interpreted as little-endian u32)- Remaining bytes for actual
data
Each element in the indices
array points to the starting index of its corresponding
data part in the data
list. The ending index can be calculated from the starting index
of the next element (or the length of the slice if dealing with the last element).
Safety
VarZeroVec<T>
is implemented with a fair amount of unsafe code, but is externally
safe to use.
Example
use zerovec::VarZeroVec; // The little-endian bytes correspond to the list of strings. let strings = vec!["w".to_owned(), "ω".to_owned(), "文".to_owned(), "𑄃".to_owned()]; let bytes = &[ 4, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 3, 0, 0, 0, 6, 0, 0, 0, 119, 207, 137, 230, 150, 135, 240, 145, 132, 131, ]; let zerovec: VarZeroVec<String> = VarZeroVec::try_from_bytes(bytes)?; assert_eq!(zerovec.get(2), Some("文")); assert_eq!(zerovec, &*strings);
Implementations
impl<'a, T: AsVarULE> VarZeroVec<'a, T>
[src]
impl<'a, T: AsVarULE> VarZeroVec<'a, T>
[src]pub fn len(&self) -> usize
[src]
pub fn len(&self) -> usize
[src]Get the number of elements in this vector
Example
let strings = vec!["foo".to_owned(), "bar".to_owned(), "baz".to_owned(), "quux".to_owned()]; let bytes = VarZeroVec::get_serializable_bytes(&strings).unwrap(); let mut vec: VarZeroVec<String> = VarZeroVec::try_from_bytes(&bytes)?; assert_eq!(vec.len(), 4);
pub fn is_empty(&self) -> bool
[src]
pub fn is_empty(&self) -> bool
[src]Returns true
if the vector contains no elements.
Examples
let strings: Vec<String> = vec![]; let bytes = VarZeroVec::get_serializable_bytes(&strings).unwrap(); let mut vec: VarZeroVec<String> = VarZeroVec::try_from_bytes(&bytes)?; assert!(vec.is_empty());
pub fn try_from_bytes(
slice: &'a [u8]
) -> Result<Self, VarZeroVecError<<<T as AsVarULE>::VarULE as VarULE>::Error>>
[src]
pub fn try_from_bytes(
slice: &'a [u8]
) -> Result<Self, VarZeroVecError<<<T as AsVarULE>::VarULE as VarULE>::Error>>
[src]Parse a VarZeroVec from a slice of the appropriate format
Slices of the right format can be obtained via VarZeroVec::get_serializable_bytes()
Example
let strings = vec!["foo".to_owned(), "bar".to_owned(), "baz".to_owned(), "quux".to_owned()]; let bytes = VarZeroVec::get_serializable_bytes(&strings).unwrap(); let mut vec: VarZeroVec<String> = VarZeroVec::try_from_bytes(&bytes)?; assert_eq!(&vec[0], "foo"); assert_eq!(&vec[1], "bar"); assert_eq!(&vec[2], "baz"); assert_eq!(&vec[3], "quux");
pub fn iter<'b: 'a>(&'b self) -> impl Iterator<Item = &'b T::VarULE>
[src]
pub fn iter<'b: 'a>(&'b self) -> impl Iterator<Item = &'b T::VarULE>
[src]Obtain an iterator over VarZeroVec’s elements
Example
let strings = vec!["foo".to_owned(), "bar".to_owned(), "baz".to_owned(), "quux".to_owned()]; let bytes = VarZeroVec::get_serializable_bytes(&strings).unwrap(); let mut vec: VarZeroVec<String> = VarZeroVec::try_from_bytes(&bytes)?; 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");
pub fn get(&self, idx: usize) -> Option<&T::VarULE>
[src]
pub fn get(&self, idx: usize) -> Option<&T::VarULE>
[src]Get one of VarZeroVec’s elements, returning None if the index is out of bounds
Example
let strings = vec!["foo".to_owned(), "bar".to_owned(), "baz".to_owned(), "quux".to_owned()]; let bytes = VarZeroVec::get_serializable_bytes(&strings).unwrap(); let mut vec: VarZeroVec<String> = VarZeroVec::try_from_bytes(&bytes)?; 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);
pub fn into_owned(self) -> VarZeroVec<'static, T> where
T: Clone,
[src]
pub fn into_owned(self) -> VarZeroVec<'static, T> where
T: Clone,
[src]Converts a borrowed ZeroVec to an owned ZeroVec. No-op if already owned.
Example
let strings = vec!["foo".to_owned(), "bar".to_owned(), "baz".to_owned(), "quux".to_owned()]; let bytes = VarZeroVec::get_serializable_bytes(&strings).unwrap(); let mut vec: VarZeroVec<String> = VarZeroVec::try_from_bytes(&bytes)?; assert_eq!(vec.len(), 4); // has 'static lifetime let owned = vec.into_owned();
pub fn get_serializable_bytes(elements: &[T]) -> Option<Vec<u8>>
[src]
pub fn get_serializable_bytes(elements: &[T]) -> Option<Vec<u8>>
[src]For a slice of T
, get a list of bytes that can be passed to
try_from_bytes
to recoup the same data.
Returns None
if the slice is too large to be represented in a list of
bytes whose length fits in a u32
.
Example
let strings = vec!["foo".to_owned(), "bar".to_owned(), "baz".to_owned()]; let bytes = VarZeroVec::get_serializable_bytes(&strings).unwrap(); let mut borrowed: VarZeroVec<String> = VarZeroVec::try_from_bytes(&bytes)?; assert_eq!(borrowed, &*strings);
impl<'a, T> VarZeroVec<'a, T> where
T: AsVarULE,
T::VarULE: Ord,
[src]
impl<'a, T> VarZeroVec<'a, T> where
T: AsVarULE,
T::VarULE: Ord,
[src]pub fn binary_search(&self, x: &T::VarULE) -> Result<usize, usize>
[src]
pub fn binary_search(&self, x: &T::VarULE) -> Result<usize, usize>
[src]Binary searches a sorted VarZeroVec<T>
for the given element. For more information, see
the primitive function binary_search
.
Example
let strings = vec!["a".to_owned(), "b".to_owned(), "f".to_owned(), "g".to_owned()]; let bytes = VarZeroVec::get_serializable_bytes(&strings).unwrap(); let mut vec: VarZeroVec<String> = VarZeroVec::try_from_bytes(&bytes)?; assert_eq!(vec.binary_search("f"), Ok(2)); assert_eq!(vec.binary_search("e"), Err(2));
Trait Implementations
impl<'a, T: Clone> Clone for VarZeroVec<'a, T>
[src]
impl<'a, T: Clone> Clone for VarZeroVec<'a, T>
[src]fn clone(&self) -> VarZeroVec<'a, T>
[src]
fn clone(&self) -> VarZeroVec<'a, T>
[src]Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]Performs copy-assignment from source
. Read more
impl<'de, 'a, T> Deserialize<'de> for VarZeroVec<'a, T> where
T: 'de + Deserialize<'de> + AsVarULE,
<<T as AsVarULE>::VarULE as VarULE>::Error: Display,
'de: 'a,
[src]
impl<'de, 'a, T> Deserialize<'de> for VarZeroVec<'a, T> where
T: 'de + Deserialize<'de> + AsVarULE,
<<T as AsVarULE>::VarULE as VarULE>::Error: Display,
'de: 'a,
[src]This impl can be made available by enabling the optional serde
feature of the zerovec
crate
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where
D: Deserializer<'de>,
[src]
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where
D: Deserializer<'de>,
[src]Deserialize this value from the given Serde deserializer. Read more
impl<'a, T> From<Vec<T, Global>> for VarZeroVec<'a, T>
[src]
impl<'a, T> From<Vec<T, Global>> for VarZeroVec<'a, T>
[src]impl<'a, T: AsVarULE> Index<usize> for VarZeroVec<'a, T>
[src]
impl<'a, T: AsVarULE> Index<usize> for VarZeroVec<'a, T>
[src]impl<'a, 'b, T> PartialEq<VarZeroVec<'b, T>> for VarZeroVec<'a, T> where
T: AsVarULE,
T::VarULE: PartialEq,
[src]
impl<'a, 'b, T> PartialEq<VarZeroVec<'b, T>> for VarZeroVec<'a, T> where
T: AsVarULE,
T::VarULE: PartialEq,
[src]impl<T> Serialize for VarZeroVec<'_, T> where
T: Serialize + AsVarULE + Clone,
[src]
impl<T> Serialize for VarZeroVec<'_, T> where
T: Serialize + AsVarULE + Clone,
[src]This impl can be made available by enabling the optional serde
feature of the zerovec
crate
impl<'a, T: 'static + AsVarULE> Yokeable<'a> for VarZeroVec<'static, T>
[src]
impl<'a, T: 'static + AsVarULE> Yokeable<'a> for VarZeroVec<'static, T>
[src]This impl can be made available by enabling the optional yoke
feature of the zerovec
crate
type Output = VarZeroVec<'a, T>
type Output = VarZeroVec<'a, T>
This type MUST be Self
with the 'static
replaced with 'a
, i.e. Self<'a>
fn transform(&'a self) -> &'a VarZeroVec<'a, T>
[src]
fn transform(&'a self) -> &'a VarZeroVec<'a, T>
[src]This method must cast self
between &'a Self<'static>
and &'a Self<'a>
. Read more
unsafe fn make(from: VarZeroVec<'a, T>) -> Self
[src]
unsafe fn make(from: VarZeroVec<'a, T>) -> Self
[src]This method can be used to cast away Self<'a>
’s lifetime. Read more
impl<'a, T> ZeroVecLike<'a, T> for VarZeroVec<'a, T> where
T: AsVarULE + Clone,
T::VarULE: Ord,
[src]
impl<'a, T> ZeroVecLike<'a, T> for VarZeroVec<'a, T> where
T: AsVarULE + Clone,
T::VarULE: Ord,
[src]type NeedleType = T::VarULE
type NeedleType = T::VarULE
The type received by Self::binary_search()
fn binary_search(&self, k: &T::VarULE) -> Result<usize, usize>
[src]
fn binary_search(&self, k: &T::VarULE) -> Result<usize, usize>
[src]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
fn replace(&mut self, index: usize, value: T) -> T
[src]
fn replace(&mut self, index: usize, value: T) -> T
[src]Replace the element at index
with another one, returning the old element
fn with_capacity(cap: usize) -> Self
[src]
fn with_capacity(cap: usize) -> Self
[src]Create a new, empty vector, with given capacity
fn is_ascending(&self) -> bool
[src]
fn is_ascending(&self) -> bool
[src]Check if this vector is in ascending order according to T
s Ord
impl
Auto Trait Implementations
impl<'a, T> RefUnwindSafe for VarZeroVec<'a, T> where
T: RefUnwindSafe,
T: RefUnwindSafe,
impl<'a, T> Send for VarZeroVec<'a, T> where
T: Send + Sync,
T: Send + Sync,
impl<'a, T> Sync for VarZeroVec<'a, T> where
T: Sync,
T: Sync,
impl<'a, T> Unpin for VarZeroVec<'a, T> where
T: Unpin,
T: Unpin,
impl<'a, T> UnwindSafe for VarZeroVec<'a, T> where
T: RefUnwindSafe + UnwindSafe,
T: RefUnwindSafe + UnwindSafe,
Blanket Implementations
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]pub fn borrow_mut(&mut self) -> &mut T
[src]
pub fn borrow_mut(&mut self) -> &mut T
[src]Mutably borrows from an owned value. Read more
impl<T> ToOwned for T where
T: Clone,
[src]
impl<T> ToOwned for T where
T: Clone,
[src]type Owned = T
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn to_owned(&self) -> T
[src]Creates owned data from borrowed data, usually by cloning. Read more
pub fn clone_into(&self, target: &mut T)
[src]
pub fn clone_into(&self, target: &mut T)
[src]🔬 This is a nightly-only experimental API. (toowned_clone_into
)
recently added
Uses borrowed data to replace owned data, usually by cloning. Read more
impl<T> DeserializeOwned for T where
T: for<'de> Deserialize<'de>,
[src]
T: for<'de> Deserialize<'de>,