Struct VarZeroSlice
struct VarZeroSlice<T: ?Sized, F = Index16> { ... }
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 used inside VarZeroVec<T> and ZeroMap:
This essentially allows for the construction of zero-copy types isomorphic to Vec<Vec<T>> by instead
using VarZeroVec<ZeroSlice<T>>.
The F type parameter is a VarZeroVecFormat (see its docs for more details), which can be used to select the
precise format of the backing buffer with various size and performance tradeoffs. It defaults to Index16.
This type can be nested within itself to allow for multi-level nested Vecs.
Examples
Nested Slices
The following code constructs the conceptual zero-copy equivalent of Vec<Vec<Vec<str>>>
use ;
let strings_1: = vec!;
let strings_2: = vec!;
let strings_3: = vec!;
let strings_4: = vec!;
let strings_12 = vec!;
let strings_34 = vec!;
let all_strings = vec!;
let vzv_1: = from;
let vzv_2: = from;
let vzv_3: = from;
let vzv_4: = from;
let vzv_12 = from;
let vzv_34 = from;
let vzv_all = from;
let reconstructed: = vzv_all
.iter
.map
.;
assert_eq!;
let bytes = vzv_all.as_bytes;
let vzv_from_bytes: =
parse_bytes.unwrap;
assert_eq!;
Iterate over Windows
Although VarZeroSlice does not itself have a .windows iterator like
[core::slice::Windows], this behavior can be easily modeled using an iterator:
use VarZeroVec;
let vzv = from;
# let mut pairs: = Vecnew;
let mut it = vzv.iter.peekable;
while let =
# assert_eq!;
Implementations
impl<T, F> VarZeroSlice<T, F>
fn binary_search(self: &Self, x: &T) -> Result<usize, usize>Binary searches a sorted
VarZeroVec<T>for the given element. For more information, see the standard library functionbinary_search.Example
# use VarZeroVec; let strings = vec!; let vec = from; assert_eq!; assert_eq!;fn binary_search_in_range(self: &Self, x: &T, range: Range<usize>) -> Option<Result<usize, usize>>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 aResultaccording to the behavior of the standard library functionbinary_search.The index is returned relative to the start of the range.
Example
# use VarZeroVec; let strings = vec!; let vec = from; // Same behavior as binary_search when the range covers the whole slice: assert_eq!; assert_eq!; // Will not look outside of the range: assert_eq!; assert_eq!; // Will return indices relative to the start of the range: assert_eq!; assert_eq!; // Will return `None` if the range is out of bounds: assert_eq!; assert_eq!;
impl<T, F> VarZeroSlice<T, F>
fn binary_search_by<impl FnMut(&T) -> Ordering: FnMut(&T) -> Ordering>(self: &Self, predicate: impl FnMut(&T) -> Ordering) -> Result<usize, usize>Binary searches a sorted
VarZeroVec<T>for the given predicate. For more information, see the standard library functionbinary_search_by.Example
# use VarZeroVec; let strings = vec!; let vec = from; assert_eq!; assert_eq!;fn binary_search_in_range_by<impl FnMut(&T) -> Ordering: FnMut(&T) -> Ordering>(self: &Self, predicate: impl FnMut(&T) -> Ordering, range: Range<usize>) -> Option<Result<usize, usize>>Binary searches a
VarZeroVec<T>for the given predicate within a certain sorted range.If the range is out of bounds, returns
None. Otherwise, returns aResultaccording to the behavior of the standard library functionbinary_search.The index is returned relative to the start of the range.
Example
# use VarZeroVec; let strings = vec!; let vec = from; // Same behavior as binary_search when the range covers the whole slice: assert_eq!; assert_eq!; // Will not look outside of the range: assert_eq!; assert_eq!; // Will return indices relative to the start of the range: assert_eq!; assert_eq!; // Will return `None` if the range is out of bounds: assert_eq!; assert_eq!;
impl<T: VarULE + ?Sized, F: VarZeroVecFormat> VarZeroSlice<T, F>
const fn new_empty() -> &'static SelfConstruct a new empty VarZeroSlice
unsafe const fn from_bytes_unchecked(bytes: &[u8]) -> &SelfUses a
&[u8]buffer as aVarZeroSlice<T>without any verification.Safety
bytesneed to be an output from [VarZeroSlice::as_bytes()].fn len(self: &Self) -> usizeGet the number of elements in this slice
Example
# use VarZeroVec; let strings = vec!; let vec = from; assert_eq!;fn is_empty(self: &Self) -> boolReturns
trueif the slice contains no elements.Examples
# use VarZeroVec; let strings: = vec!; let vec = from; assert!;fn iter<'b>(self: &'b Self) -> VarZeroSliceIter<'b, T, F>Obtain an iterator over this slice's elements
Example
# use VarZeroVec; let strings = vec!; let vec = from; let mut iter_results: = vec.iter.collect; assert_eq!; assert_eq!; assert_eq!; assert_eq!;fn get(self: &Self, idx: usize) -> Option<&T>Get one of this slice's elements, returning
Noneif the index is out of boundsExample
# use VarZeroVec; let strings = vec!; let vec = from; let mut iter_results: = vec.iter.collect; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!;unsafe fn get_unchecked(self: &Self, idx: usize) -> &TGet one of this slice's elements
Safety
indexmust be in rangeExample
# use VarZeroVec; let strings = vec!; let vec = from; let mut iter_results: = vec.iter.collect; unsafefn to_vec(self: &Self) -> Vec<Box<T>>Obtain an owned
Vec<Box<T>>out of thisconst fn as_bytes(self: &Self) -> &[u8]Get a reference to the entire encoded backing buffer of this slice
The bytes can be passed back to [
Self::parse_bytes()].To take the bytes as a vector, see [
VarZeroVec::into_bytes()].Example
# use VarZeroVec; let strings = vec!; let vzv = from; assert_eq!;const fn as_varzerovec<'a>(self: &'a Self) -> VarZeroVec<'a, T, F>Get this
VarZeroSliceas a borrowedVarZeroVecIf you wish to repeatedly call methods on this
VarZeroSlice, it is more efficient to perform this conversion firstfn parse_bytes<'a>(slice: &'a [u8]) -> Result<&'a Self, UleError>Parse a VarZeroSlice from a slice of the appropriate format
Slices of the right format can be obtained via [
VarZeroSlice::as_bytes()]
impl<T> Any for VarZeroSlice<T, F>
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for VarZeroSlice<T, F>
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for VarZeroSlice<T, F>
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> EncodeAsVarULE for VarZeroSlice<T, F>
fn encode_var_ule_as_slices<R>(self: &Self, cb: impl FnOnce(&[&[u8]]) -> R) -> R
impl<T, F = Index16> Sized for VarZeroSlice<T, F>
impl<T, F> Eq for VarZeroSlice<T, F>
impl<T, F> Freeze for VarZeroSlice<T, F>
impl<T, F> PartialEq for VarZeroSlice<T, F>
fn eq(self: &Self, other: &VarZeroSlice<T, F>) -> bool
impl<T, F> RefUnwindSafe for VarZeroSlice<T, F>
impl<T, F> Send for VarZeroSlice<T, F>
impl<T, F> Sync for VarZeroSlice<T, F>
impl<T, F> Unpin for VarZeroSlice<T, F>
impl<T, F> UnsafeUnpin for VarZeroSlice<T, F>
impl<T, F> UnwindSafe for VarZeroSlice<T, F>
impl<T, F> ZeroVecLike for VarZeroSlice<T, F>
fn zvl_new_borrowed() -> &'static <Self as >::SliceVariantfn zvl_binary_search(self: &Self, k: &T) -> Result<usize, usize> where T: Ordfn zvl_binary_search_in_range(self: &Self, k: &T, range: Range<usize>) -> Option<Result<usize, usize>> where T: Ordfn zvl_binary_search_by<impl FnMut(&T) -> Ordering: FnMut(&T) -> Ordering>(self: &Self, predicate: impl FnMut(&T) -> Ordering) -> Result<usize, usize>fn zvl_binary_search_in_range_by<impl FnMut(&T) -> Ordering: FnMut(&T) -> Ordering>(self: &Self, predicate: impl FnMut(&T) -> Ordering, range: Range<usize>) -> Option<Result<usize, usize>>fn zvl_get(self: &Self, index: usize) -> Option<&T>fn zvl_len(self: &Self) -> usizefn zvl_as_borrowed(self: &Self) -> &VarZeroSlice<T, F>fn zvl_get_as_t<R, impl FnOnce(&T) -> R: FnOnce(&T) -> R>(g: &<Self as >::GetType, f: impl FnOnce(&T) -> R) -> R
impl<T, F: VarZeroVecFormat> Debug for VarZeroSlice<T, F>
fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result
impl<T: ?Sized, F: VarZeroVecFormat> AsRef for VarZeroSlice<T, F>
fn as_ref(self: &Self) -> &VarZeroSlice<T, F>
impl<T: VarULE + ?Sized + 'static, F: VarZeroVecFormat> VarULE for VarZeroSlice<T, F>
fn validate_bytes(bytes: &[u8]) -> Result<(), UleError>unsafe fn from_bytes_unchecked(bytes: &[u8]) -> &Selffn as_bytes(self: &Self) -> &[u8]
impl<T: VarULE + ?Sized + Ord, F: VarZeroVecFormat> Ord for VarZeroSlice<T, F>
fn cmp(self: &Self, other: &Self) -> Ordering
impl<T: VarULE + ?Sized + PartialOrd, F: VarZeroVecFormat> PartialOrd for VarZeroSlice<T, F>
fn partial_cmp(self: &Self, other: &Self) -> Option<Ordering>
impl<T: VarULE + ?Sized, F: VarZeroVecFormat> Index for VarZeroSlice<T, F>
fn index(self: &Self, index: usize) -> &<Self as >::Output