Enum zerovec::varzerovec::VarZeroVec [−][src]
pub enum VarZeroVec<'a, T: ?Sized> {
Owned(VarZeroVecOwned<T>),
Borrowed(VarZeroVecBorrowed<'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 VarULE
, which is already implemented for str
and [T]
where
T
implements ULE
. It is also implemented on VarZeroVecULE<T>
for nesting.
VarZeroVec<T>
behaves much like 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<str> = VarZeroVec::parse_byte_slice(bytes)?;
assert_eq!(zerovec.get(2), Some("文"));
assert_eq!(zerovec, &*strings);
Here’s another example with [T]
:
use zerovec::VarZeroVec;
use zerovec::ZeroVec;
use zerovec::ule::*;
// The little-endian bytes correspond to the list of integers.
let numbers: Vec<Vec<PlainOldULE<4>>> = vec![
vec![12.into(), 25.into(), 38.into()],
vec![39179.into(), 100.into()],
vec![42.into(), 55555.into()],
vec![12345.into(), 54321.into(), 9.into()],
];
let bytes = &[4, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0, 20, 0, 0, 0, 28, 0, 0, 0,
12, 0, 0, 0, 25, 0, 0, 0, 38, 0, 0, 0, 11, 153, 0, 0, 100, 0,
0, 0, 42, 0, 0, 0, 3, 217, 0, 0, 57, 48, 0, 0, 49, 212, 0, 0,
9, 0, 0, 0];
let zerovec: VarZeroVec<[PlainOldULE<4>]> = VarZeroVec::parse_byte_slice(bytes)?;
assert_eq!(zerovec.get(2).and_then(|v| v.get(1)), Some(&55555.into()));
assert_eq!(zerovec, &*numbers);
for (zv, v) in zerovec.iter().zip(numbers.iter()) {
assert_eq!(zv, v);
}
VarZeroVec
s can be nested infinitely, see the docs of VarZeroVecULE
for more information.
Variants
Owned(VarZeroVecOwned<T>)
An allocated VarZeroVec, allowing for mutations.
Examples
use zerovec::VarZeroVec;
let mut vzv = VarZeroVec::<str>::default();
vzv.make_mut().push("foo");
vzv.make_mut().push("bar");
assert!(matches!(vzv, VarZeroVec::Owned(_)));
Tuple Fields of Owned
0: VarZeroVecOwned<T>
Borrowed(VarZeroVecBorrowed<'a, T>)
A borrowed VarZeroVec, requiring no allocations.
If a mutating operation is invoked on VarZeroVec, the Borrowed is converted to Owned.
Examples
use zerovec::VarZeroVec;
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 vzv: VarZeroVec<str> = VarZeroVec::parse_byte_slice(bytes).unwrap();
assert!(matches!(vzv, VarZeroVec::Borrowed(_)));
Tuple Fields of Borrowed
0: VarZeroVecBorrowed<'a, T>
Implementations
Construct a new empty VarZeroVec
Obtain a VarZeroVecBorrowed
borrowing from the internal buffer
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::<str>::get_serializable_bytes(&strings).unwrap();
let mut vec: VarZeroVec<str> = VarZeroVec::parse_byte_slice(&bytes)?;
assert_eq!(vec.len(), 4);
Returns true
if the vector contains no elements.
Examples
let strings: Vec<String> = vec![];
let bytes = VarZeroVec::<str>::get_serializable_bytes(&strings).unwrap();
let mut vec: VarZeroVec<str> = VarZeroVec::parse_byte_slice(&bytes)?;
assert!(vec.is_empty());
Parse a VarZeroVec from a slice of the appropriate format
Slices of the right format can be obtained via VarZeroVec::<str>::get_serializable_bytes()
or VarZeroVec::get_encoded_slice()
Example
let strings = vec!["foo".to_owned(), "bar".to_owned(),
"baz".to_owned(), "quux".to_owned()];
let bytes = VarZeroVec::<str>::get_serializable_bytes(&strings).unwrap();
let mut vec: VarZeroVec<str> = VarZeroVec::parse_byte_slice(&bytes)?;
assert_eq!(&vec[0], "foo");
assert_eq!(&vec[1], "bar");
assert_eq!(&vec[2], "baz");
assert_eq!(&vec[3], "quux");
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::<str>::get_serializable_bytes(&strings).unwrap();
let mut vec: VarZeroVec<str> = VarZeroVec::parse_byte_slice(&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");
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::<str>::get_serializable_bytes(&strings).unwrap();
let mut vec: VarZeroVec<str> = VarZeroVec::parse_byte_slice(&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);
Convert this into a mutable vector of the owned T
type, cloning if necessary.
Example
let strings = vec!["foo".to_owned(), "bar".to_owned(),
"baz".to_owned(), "quux".to_owned()];
let bytes = VarZeroVec::<str>::get_serializable_bytes(&strings).unwrap();
let mut vec: VarZeroVec<str> = VarZeroVec::parse_byte_slice(&bytes)?;
assert_eq!(vec.len(), 4);
let mutvec = vec.make_mut();
mutvec.push("lorem ipsum".into());
mutvec[2] = "dolor sit".into();
assert_eq!(&vec[0], "foo");
assert_eq!(&vec[1], "bar");
assert_eq!(&vec[2], "dolor sit");
assert_eq!(&vec[3], "quux");
assert_eq!(&vec[4], "lorem ipsum");
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::<str>::get_serializable_bytes(&strings).unwrap();
let mut vec: VarZeroVec<str> = VarZeroVec::parse_byte_slice(&bytes)?;
assert_eq!(vec.len(), 4);
// has 'static lifetime
let owned = vec.into_owned();
Obtain this VarZeroVec
as a VarZeroVecULE
Obtain the internal encoded slice
This can be passed back to Self::parse_byte_slice()
For a slice of T
, get a list of bytes that can be passed to
parse_byte_slice
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::<str>::get_serializable_bytes(&strings).unwrap();
let mut borrowed: VarZeroVec<str> = VarZeroVec::parse_byte_slice(&bytes)?;
assert_eq!(borrowed, &*strings);
Return whether the VarZeroVec
is operating on owned or borrowed
data. VarZeroVec::into_owned()
and VarZeroVec::make_mut()
can
be used to force it into an owned type
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::<str>::get_serializable_bytes(&strings).unwrap();
let mut vec: VarZeroVec<str> = VarZeroVec::parse_byte_slice(&bytes)?;
assert_eq!(vec.binary_search("f"), Ok(2));
assert_eq!(vec.binary_search("e"), Err(2));
Trait Implementations
impl<'de, 'a, T> Deserialize<'de> for VarZeroVec<'a, T> where
T: VarULE + ?Sized,
Box<T>: Deserialize<'de>,
'de: 'a,
impl<'de, 'a, T> Deserialize<'de> for VarZeroVec<'a, T> where
T: VarULE + ?Sized,
Box<T>: Deserialize<'de>,
'de: 'a,
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
impl<'a, A, T> From<&'_ [A]> for VarZeroVec<'a, T> where
T: VarULE,
T: ?Sized,
A: EncodeAsVarULE<T>,
impl<'a, A, T> From<&'_ [A]> for VarZeroVec<'a, T> where
T: VarULE,
T: ?Sized,
A: EncodeAsVarULE<T>,
Performs the conversion.
Performs the conversion.
Performs the conversion.
impl<'a, 'b, T> PartialEq<VarZeroVec<'b, T>> for VarZeroVec<'a, T> where
T: VarULE,
T: ?Sized,
T: PartialEq,
impl<'a, 'b, T> PartialEq<VarZeroVec<'b, T>> for VarZeroVec<'a, T> where
T: VarULE,
T: ?Sized,
T: PartialEq,
This impl can be made available by enabling the optional serde
feature of the zerovec
crate
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>
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
fn transform_mut<F>(&'a mut self, f: F) where
F: 'static + for<'b> FnOnce(&'b mut Self::Output),
fn transform_mut<F>(&'a mut self, f: F) where
F: 'static + for<'b> FnOnce(&'b mut Self::Output),
This method must cast self
between &'a mut Self<'static>
and &'a mut Self<'a>
,
and pass it to f
. Read more
impl<'a, T: 'static + VarULE + ?Sized> ZeroCopyFrom<VarZeroVec<'a, T>> for VarZeroVec<'static, T>
impl<'a, T: 'static + VarULE + ?Sized> ZeroCopyFrom<VarZeroVec<'a, T>> for VarZeroVec<'static, T>
Clone the cart C
into a Yokeable
struct, which may retain references into C
.
type NeedleType = T
type NeedleType = T
The type received by Self::binary_search()
type GetType = T
type GetType = T
The type returned by Self::get()
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
Remove the element at index
(panicking if nonexistant)
Replace the element at index
with another one, returning the old element
Create a new, empty vector, with given capacity
Check if this vector is in ascending order according to T
s Ord
impl
Auto Trait Implementations
impl<'a, T: ?Sized> RefUnwindSafe for VarZeroVec<'a, T> where
T: RefUnwindSafe,
impl<'a, T: ?Sized> Send for VarZeroVec<'a, T> where
T: Send + Sync,
impl<'a, T: ?Sized> Sync for VarZeroVec<'a, T> where
T: Sync,
impl<'a, T: ?Sized> Unpin for VarZeroVec<'a, T>
impl<'a, T: ?Sized> UnwindSafe for VarZeroVec<'a, T> where
T: UnwindSafe + RefUnwindSafe,
Blanket Implementations
Mutably borrows from an owned value. Read more