Struct ZeroSlice
struct ZeroSlice<T: AsULE>(_)
A zero-copy "slice", i.e. the zero-copy version of [T].
This behaves
similarly to [ZeroVec<T>], however [ZeroVec<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>>. See the VarZeroVec docs for an example.
Examples
Const-construct a ZeroSlice of u16:
use AsULE;
use ZeroSlice;
const DATA: & =
from_ule_slice;
assert_eq!;
Implementations
impl ZeroSlice<bool>
const fn try_from_bytes(bytes: &[u8]) -> Result<&Self, UleError>This function can be used for constructing ZeroVecs in a const context, avoiding parsing checks.
This cannot be generic over T because of current limitations in
const, but if this method is needed in a non-const context, check out [ZeroSlice::parse_bytes()] instead.See [
ZeroSlice::cast()] for an example.
impl ZeroSlice<u128>
const fn try_from_bytes(bytes: &[u8]) -> Result<&Self, UleError>This function can be used for constructing ZeroVecs in a const context, avoiding parsing checks.
This cannot be generic over T because of current limitations in
const, but if this method is needed in a non-const context, check out [ZeroSlice::parse_bytes()] instead.See [
ZeroSlice::cast()] for an example.
impl ZeroSlice<u16>
const fn try_from_bytes(bytes: &[u8]) -> Result<&Self, UleError>This function can be used for constructing ZeroVecs in a const context, avoiding parsing checks.
This cannot be generic over T because of current limitations in
const, but if this method is needed in a non-const context, check out [ZeroSlice::parse_bytes()] instead.See [
ZeroSlice::cast()] for an example.
impl ZeroSlice<u32>
const fn try_from_bytes(bytes: &[u8]) -> Result<&Self, UleError>This function can be used for constructing ZeroVecs in a const context, avoiding parsing checks.
This cannot be generic over T because of current limitations in
const, but if this method is needed in a non-const context, check out [ZeroSlice::parse_bytes()] instead.See [
ZeroSlice::cast()] for an example.
impl ZeroSlice<u64>
const fn try_from_bytes(bytes: &[u8]) -> Result<&Self, UleError>This function can be used for constructing ZeroVecs in a const context, avoiding parsing checks.
This cannot be generic over T because of current limitations in
const, but if this method is needed in a non-const context, check out [ZeroSlice::parse_bytes()] instead.See [
ZeroSlice::cast()] for an example.
impl ZeroSlice<u8>
const fn try_from_bytes(bytes: &[u8]) -> Result<&Self, UleError>This function can be used for constructing ZeroVecs in a const context, avoiding parsing checks.
This cannot be generic over T because of current limitations in
const, but if this method is needed in a non-const context, check out [ZeroSlice::parse_bytes()] instead.See [
ZeroSlice::cast()] for an example.
impl<T> ZeroSlice<T>
fn get(self: &Self, index: usize) -> Option<T>Gets the element at the specified index. Returns
Noneif out of range.Example
use ZeroVec; let bytes: & = &; let zerovec: = parse_bytes.expect; assert_eq!; assert_eq!;fn get_as_array<N: usize>(self: &Self) -> Option<[T; N]>Gets the entire slice as an array of length
N. ReturnsNoneif the slice does not have exactlyNelements.Example
use ZeroVec; let bytes: & = &; let zerovec: = parse_bytes.expect; let array: = zerovec.get_as_array.expect; assert_eq!;fn get_subslice(self: &Self, range: Range<usize>) -> Option<&ZeroSlice<T>>Gets a subslice of elements within a certain range. Returns
Noneif the range is out of bounds of thisZeroSlice.Example
use ZeroVec; let bytes: & = &; let zerovec: = parse_bytes.expect; assert_eq!; assert_eq!;fn get_ule_ref(self: &Self, index: usize) -> Option<&<T as >::ULE>Get a borrowed reference to the underlying ULE type at a specified index.
Prefer [
Self::get()] over this method where possible since working directly withULEtypes is less ergonomicconst fn cast<P>(self: &Self) -> &ZeroSlice<P> where P: AsULE<ULE = <T as >::ULE>Casts a
ZeroSlice<T>to a compatibleZeroSlice<P>.TandPare compatible if they have the sameULErepresentation.If the
ULEs ofTandPare different, use [Self::try_as_converted()].Examples
use ZeroSlice; const BYTES: & = &; const ZS_U16: & = ; let zs_i16: & = ZS_U16.cast; assert_eq!; assert_eq!;fn try_as_converted<P: AsULE>(self: &Self) -> Result<&ZeroSlice<P>, UleError>Converts a
&ZeroSlice<T>into a&ZeroSlice<P>.The resulting slice will have the same length as the original slice if and only if
T::ULEandP::ULEare the same size.If
TandPhave the exact sameULE, use [Self::cast()].Examples
use ZeroSlice; const BYTES: & = &; const ZS_U32: & = ; let zs_u8_4: & = ZS_U32.try_as_converted.expect; assert_eq!; assert_eq!;fn first(self: &Self) -> Option<T>Gets the first element. Returns
Noneif empty.Example
use ZeroVec; let bytes: & = &; let zerovec: = parse_bytes.expect; assert_eq!;fn last(self: &Self) -> Option<T>Gets the last element. Returns
Noneif empty.Example
use ZeroVec; let bytes: & = &; let zerovec: = parse_bytes.expect; assert_eq!;fn iter<'a>(self: &'a Self) -> ZeroSliceIter<'a, T>Gets an iterator over the elements.
Example
use ZeroVec; let bytes: & = &; let zerovec: = parse_bytes.expect; let mut it = zerovec.iter; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!;fn split_first(self: &Self) -> Option<(T, &ZeroSlice<T>)>Returns a tuple with the first element and a subslice of the remaining elements.
Example
use AsULE; use ZeroSlice; const DATA: & = from_ule_slice; const EXPECTED_VALUE: = ; assert_eq!;
impl<T> ZeroSlice<T>
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
ZeroVec<T>based on a given predicate. For more information, see the primitive functionbinary_search_by.Example
use ZeroVec; let bytes: & = &; let zerovec: = parse_bytes.expect; assert_eq!; assert_eq!;
impl<T> ZeroSlice<T>
const fn new_empty() -> &'static SelfReturns an empty slice.
const fn as_zerovec(self: &Self) -> ZeroVec<'_, T>Get this
ZeroSliceas a borrowedZeroVecZeroSlicedoes not have most of the methods thatZeroVecdoes, so it is recommended to convert it to aZeroVecbefore doing anything.fn parse_bytes(bytes: &[u8]) -> Result<&Self, UleError>Attempt to construct a
&ZeroSlice<T>from a byte slice, returning an error if it's not a valid byte sequenceunsafe const fn from_bytes_unchecked(bytes: &[u8]) -> &SelfUses a
&[u8]buffer as aZeroVec<T>without any verification.Safety
bytesneed to be an output from [ZeroSlice::as_bytes()].const fn from_ule_slice(slice: &[<T as >::ULE]) -> &SelfConstruct a
&ZeroSlice<T>from a slice of ULEs.This function can be used for constructing ZeroVecs in a const context, avoiding parsing checks.
See
ZeroSlicefor an example.fn from_boxed_slice(slice: Box<[<T as >::ULE]>) -> Box<Self>Construct a
Box<ZeroSlice<T>>from a boxed slice of ULEsfn as_bytes(self: &Self) -> &[u8]Returns this slice as its underlying
&[u8]byte buffer representation.Useful for serialization.
Example
use ZeroVec; // The little-endian bytes correspond to the numbers on the following line. let bytes: & = &; let nums: & = &; let zerovec = alloc_from_slice; assert_eq!;const fn as_ule_slice(self: &Self) -> &[<T as >::ULE]Dereferences this slice as
&[T::ULE].const fn len(self: &Self) -> usizeReturns the number of elements in this slice.
Example
use AsULE; use ZeroVec; let bytes: & = &; let zerovec: = parse_bytes.expect; assert_eq!; assert_eq!;const fn is_empty(self: &Self) -> boolReturns whether this slice is empty.
Example
use ZeroVec; let bytes: & = &; let zerovec: = parse_bytes.expect; assert!; let emptyvec: = parse_bytes.expect; assert!;
impl<T> ZeroSlice<T>
fn binary_search(self: &Self, x: &T) -> Result<usize, usize>Binary searches a sorted
ZeroVec<T>for the given element. For more information, see the primitive functionbinary_search.Example
use ZeroVec; let bytes: & = &; let zerovec: = parse_bytes.expect; assert_eq!; assert_eq!;
impl<'a, T> PartialEq for ZeroSlice<T>
fn eq(self: &Self, other: &ZeroVec<'a, T>) -> bool
impl<'a, T> ZeroMapKV for ZeroSlice<T>
impl<T> Any for ZeroSlice<T>
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for ZeroSlice<T>
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for ZeroSlice<T>
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> Debug for ZeroSlice<T>
fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result
impl<T> EncodeAsVarULE for ZeroSlice<T>
fn encode_var_ule_as_slices<R>(self: &Self, cb: impl FnOnce(&[&[u8]]) -> R) -> R
impl<T> Eq for ZeroSlice<T>
impl<T> Freeze for ZeroSlice<T>
impl<T> PartialEq for ZeroSlice<T>
fn eq(self: &Self, other: &[T]) -> bool
impl<T> PartialEq for ZeroSlice<T>
fn eq(self: &Self, other: &ZeroSlice<T>) -> bool
impl<T> RefUnwindSafe for ZeroSlice<T>
impl<T> Send for ZeroSlice<T>
impl<T> Sized for ZeroSlice<T>
impl<T> Sync for ZeroSlice<T>
impl<T> Unpin for ZeroSlice<T>
impl<T> UnsafeUnpin for ZeroSlice<T>
impl<T> UnwindSafe for ZeroSlice<T>
impl<T> ZeroVecLike for ZeroSlice<T>
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 as >::ULE>fn zvl_len(self: &Self) -> usizefn zvl_as_borrowed(self: &Self) -> &ZeroSlice<T>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: AsULE + 'static> VarULE for ZeroSlice<T>
fn validate_bytes(bytes: &[u8]) -> Result<(), UleError>unsafe fn from_bytes_unchecked(bytes: &[u8]) -> &Self
impl<T: AsULE + Ord> Ord for ZeroSlice<T>
fn cmp(self: &Self, other: &Self) -> Ordering
impl<T: AsULE + PartialOrd> PartialOrd for ZeroSlice<T>
fn partial_cmp(self: &Self, other: &Self) -> Option<Ordering>