#[repr(transparent)]pub struct ZeroSlice<T: AsULE>(_);
Expand description
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>
: 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.
Implementations
Construct a &ZeroSlice<T>
from a slice of ULEs
Construct a Box<ZeroSlice<T>>
from a boxed slice of ULEs
Returns this slice as its underlying &[u8]
byte buffer representation.
Useful for serialization.
Example
use zerovec::ZeroVec;
// The little-endian bytes correspond to the numbers on the following line.
let bytes: &[u8] = &[0xD3, 0x00, 0x19, 0x01, 0xA5, 0x01, 0xCD, 0x01];
let nums: &[u16] = &[211, 281, 421, 461];
let zerovec = ZeroVec::alloc_from_slice(nums);
assert_eq!(bytes, zerovec.as_bytes());
Dereferences this slice as &[T::ULE]
.
Returns the number of elements in this slice.
Example
use zerovec::ZeroVec;
use zerovec::ule::AsULE;
let bytes: &[u8] = &[0xD3, 0x00, 0x19, 0x01, 0xA5, 0x01, 0xCD, 0x01];
let zerovec: ZeroVec<u16> = ZeroVec::parse_byte_slice(bytes).expect("infallible");
assert_eq!(4, zerovec.len());
assert_eq!(
bytes.len(),
zerovec.len() * std::mem::size_of::<<u16 as AsULE>::ULE>()
);
Returns whether this slice is empty.
Example
use zerovec::ZeroVec;
let bytes: &[u8] = &[0xD3, 0x00, 0x19, 0x01, 0xA5, 0x01, 0xCD, 0x01];
let zerovec: ZeroVec<u16> = ZeroVec::parse_byte_slice(bytes).expect("infallible");
assert!(!zerovec.is_empty());
let emptyvec: ZeroVec<u16> = ZeroVec::parse_byte_slice(&[]).expect("infallible");
assert!(emptyvec.is_empty());
Gets the element at the specified index. Returns None if out of range.
Example
use zerovec::ZeroVec;
let bytes: &[u8] = &[0xD3, 0x00, 0x19, 0x01, 0xA5, 0x01, 0xCD, 0x01];
let zerovec: ZeroVec<u16> = ZeroVec::parse_byte_slice(bytes).expect("infallible");
assert_eq!(zerovec.get(2), Some(421));
assert_eq!(zerovec.get(4), None);
Gets a subslice of elements within a certain range. Returns None if the range
is out of bounds of this ZeroSlice
.
Example
use zerovec::ZeroVec;
let bytes: &[u8] = &[0xD3, 0x00, 0x19, 0x01, 0xA5, 0x01, 0xCD, 0x01];
let zerovec: ZeroVec<u16> = ZeroVec::parse_byte_slice(bytes).expect("infallible");
assert_eq!(
zerovec.get_subslice(1..3),
Some(&*ZeroVec::from_slice(&[0x0119, 0x01A5]))
);
assert_eq!(zerovec.get_subslice(3..5), None);
Get a borrowed reference to the underlying ULE type at a specified index.
Prefer Self::get()
over this method where possible since working
directly with ULE
types is less ergonomic
Gets the first element. Returns None if empty.
Example
use zerovec::ZeroVec;
let bytes: &[u8] = &[0xD3, 0x00, 0x19, 0x01, 0xA5, 0x01, 0xCD, 0x01];
let zerovec: ZeroVec<u16> = ZeroVec::parse_byte_slice(bytes).expect("infallible");
assert_eq!(zerovec.first(), Some(211));
Gets the last element. Returns None if empty.
Example
use zerovec::ZeroVec;
let bytes: &[u8] = &[0xD3, 0x00, 0x19, 0x01, 0xA5, 0x01, 0xCD, 0x01];
let zerovec: ZeroVec<u16> = ZeroVec::parse_byte_slice(bytes).expect("infallible");
assert_eq!(zerovec.last(), Some(461));
Gets an iterator over the elements.
Example
use zerovec::ZeroVec;
let bytes: &[u8] = &[0xD3, 0x00, 0x19, 0x01, 0xA5, 0x01, 0xCD, 0x01];
let zerovec: ZeroVec<u16> = ZeroVec::parse_byte_slice(bytes).expect("infallible");
let mut it = zerovec.iter();
assert_eq!(it.next(), Some(211));
assert_eq!(it.next(), Some(281));
assert_eq!(it.next(), Some(421));
assert_eq!(it.next(), Some(461));
assert_eq!(it.next(), None);
Binary searches a sorted ZeroVec<T>
for the given element. For more information, see
the primitive function binary_search
.
Example
use zerovec::ZeroVec;
let bytes: &[u8] = &[0xD3, 0x00, 0x19, 0x01, 0xA5, 0x01, 0xCD, 0x01];
let zerovec: ZeroVec<u16> = ZeroVec::parse_byte_slice(bytes).expect("infallible");
assert_eq!(zerovec.binary_search(&281), Ok(1));
assert_eq!(zerovec.binary_search(&282), Err(2));
Trait Implementations
Get element at index
, with a longer lifetime
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
This method returns an ordering between self
and other
values if one exists. Read more
This method tests less than (for self
and other
) and is used by the <
operator. Read more
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
This impl can be made available by enabling the optional serde
feature of the zerovec
crate
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
Parses a byte slice, &[u8]
, and return it as &Self
with the same lifetime. Read more
Given &Self
, returns a &[u8]
with the same lifetime. Read more
type Container = VarZeroVec<'a, ZeroSlice<T>>
type Container = VarZeroVec<'a, ZeroSlice<T>>
The container that can be used with this type: ZeroVec
or VarZeroVec
.
type BorrowedVariant = &'a ZeroSlice<T>
type BorrowedVariant = &'a ZeroSlice<T>
A fully borrowed version of this
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
Check if this vector is in ascending order according to T
s 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