Struct zerovec::vecs::FlexZeroSlice
source · [−]#[repr(packed)]pub struct FlexZeroSlice { /* private fields */ }
Expand description
A zero-copy “slice” that efficiently represents [usize]
.
Implementations
sourceimpl FlexZeroSlice
impl FlexZeroSlice
sourcepub const fn new_empty() -> &'static Self
pub const fn new_empty() -> &'static Self
Constructs a new empty FlexZeroSlice
.
use zerovec::vecs::FlexZeroSlice;
const EMPTY_SLICE: &FlexZeroSlice = FlexZeroSlice::new_empty();
assert!(EMPTY_SLICE.is_empty());
assert_eq!(EMPTY_SLICE.len(), 0);
assert_eq!(EMPTY_SLICE.first(), None);
sourcepub const fn parse_byte_slice(bytes: &[u8]) -> Result<&Self, ZeroVecError>
pub const fn parse_byte_slice(bytes: &[u8]) -> Result<&Self, ZeroVecError>
Safely constructs a FlexZeroSlice
from a byte array.
Examples
use zerovec::vecs::FlexZeroSlice;
const FZS: &FlexZeroSlice = match FlexZeroSlice::parse_byte_slice(&[
2, // width
0x42, 0x00, // first value
0x07, 0x09, // second value
0xFF, 0xFF, // third value
]) {
Ok(v) => v,
Err(_) => panic!("invalid bytes")
};
assert!(!FZS.is_empty());
assert_eq!(FZS.len(), 3);
assert_eq!(FZS.first(), Some(0x0042));
assert_eq!(FZS.get(0), Some(0x0042));
assert_eq!(FZS.get(1), Some(0x0907));
assert_eq!(FZS.get(2), Some(0xFFFF));
assert_eq!(FZS.get(3), None);
assert_eq!(FZS.last(), Some(0xFFFF));
sourcepub const unsafe fn from_byte_slice_unchecked(bytes: &[u8]) -> &Self
pub const unsafe fn from_byte_slice_unchecked(bytes: &[u8]) -> &Self
Constructs a FlexZeroSlice
without checking invariants.
Panics
Panics if bytes
is empty.
Safety
Must be called on a valid FlexZeroSlice
byte array.
sourcepub fn as_bytes(&self) -> &[u8]ⓘNotable traits for &[u8]impl Read for &[u8]impl Write for &mut [u8]
pub fn as_bytes(&self) -> &[u8]ⓘNotable traits for &[u8]impl Read for &[u8]impl Write for &mut [u8]
Returns this slice as its underlying &[u8]
byte buffer representation.
Useful for serialization.
Example
use zerovec::vecs::FlexZeroSlice;
let bytes: &[u8] = &[2, 0xD3, 0x00, 0x19, 0x01, 0xA5, 0x01, 0xCD, 0x80];
let fzv = FlexZeroSlice::parse_byte_slice(bytes).expect("valid bytes");
assert_eq!(bytes, fzv.as_bytes());
sourcepub const fn as_flexzerovec(&self) -> FlexZeroVec<'_>
pub const fn as_flexzerovec(&self) -> FlexZeroVec<'_>
Borrows this FlexZeroSlice
as a FlexZeroVec::Borrowed
.
sourcepub fn get(&self, index: usize) -> Option<usize>
pub fn get(&self, index: usize) -> Option<usize>
Gets the element at index
, or None
if index >= self.len()
.
Examples
use zerovec::vecs::FlexZeroVec;
let fzv: FlexZeroVec = [22, 33].iter().copied().collect();
assert_eq!(fzv.get(0), Some(22));
assert_eq!(fzv.get(1), Some(33));
assert_eq!(fzv.get(2), None);
sourcepub unsafe fn get_unchecked(&self, index: usize) -> usize
pub unsafe fn get_unchecked(&self, index: usize) -> usize
sourcepub fn first(&self) -> Option<usize>
pub fn first(&self) -> Option<usize>
Gets the first element of the slice, or None
if the slice is empty.
sourcepub fn last(&self) -> Option<usize>
pub fn last(&self) -> Option<usize>
Gets the last element of the slice, or None
if the slice is empty.
sourcepub fn iter(&self) -> impl DoubleEndedIterator<Item = usize> + '_
pub fn iter(&self) -> impl DoubleEndedIterator<Item = usize> + '_
Gets an iterator over the elements of the slice as usize
.
sourcepub fn iter_pairs(&self) -> impl Iterator<Item = (usize, Option<usize>)> + '_
pub fn iter_pairs(&self) -> impl Iterator<Item = (usize, Option<usize>)> + '_
Gets an iterator over pairs of elements.
The second element of the final pair is None
.
Examples
use zerovec::vecs::FlexZeroVec;
let nums: &[usize] = &[211, 281, 421, 461];
let fzv: FlexZeroVec = nums.iter().copied().collect();
let mut pairs_it = fzv.iter_pairs();
assert_eq!(pairs_it.next(), Some((211, Some(281))));
assert_eq!(pairs_it.next(), Some((281, Some(421))));
assert_eq!(pairs_it.next(), Some((421, Some(461))));
assert_eq!(pairs_it.next(), Some((461, None)));
assert_eq!(pairs_it.next(), None);
sourcepub fn to_vec(&self) -> Vec<usize>
pub fn to_vec(&self) -> Vec<usize>
Creates a Vec<usize>
from a FlexZeroSlice
(or FlexZeroVec
).
Examples
use zerovec::vecs::FlexZeroVec;
let nums: &[usize] = &[211, 281, 421, 461];
let fzv: FlexZeroVec = nums.iter().copied().collect();
let vec: Vec<usize> = fzv.to_vec();
assert_eq!(nums, vec.as_slice());
sourcepub fn binary_search(&self, needle: usize) -> Result<usize, usize>
pub fn binary_search(&self, needle: usize) -> Result<usize, usize>
Binary searches a sorted FlexZeroSlice
for the given usize
value.
Examples
use zerovec::vecs::FlexZeroVec;
let nums: &[usize] = &[211, 281, 421, 461];
let fzv: FlexZeroVec = nums.iter().copied().collect();
assert_eq!(fzv.binary_search(0), Err(0));
assert_eq!(fzv.binary_search(211), Ok(0));
assert_eq!(fzv.binary_search(250), Err(1));
assert_eq!(fzv.binary_search(281), Ok(1));
assert_eq!(fzv.binary_search(300), Err(2));
assert_eq!(fzv.binary_search(421), Ok(2));
assert_eq!(fzv.binary_search(450), Err(3));
assert_eq!(fzv.binary_search(461), Ok(3));
assert_eq!(fzv.binary_search(462), Err(4));
sourcepub fn binary_search_in_range(
&self,
needle: usize,
range: Range<usize>
) -> Option<Result<usize, usize>>
pub fn binary_search_in_range(
&self,
needle: usize,
range: Range<usize>
) -> Option<Result<usize, usize>>
Binary searches a sorted range of a FlexZeroSlice
for the given usize
value.
The indices in the return value are relative to the start of the range.
Examples
use zerovec::vecs::FlexZeroVec;
// Make a FlexZeroVec with two sorted ranges: 0..3 and 3..5
let nums: &[usize] = &[111, 222, 444, 333, 555];
let fzv: FlexZeroVec = nums.iter().copied().collect();
// Search in the first range:
assert_eq!(fzv.binary_search_in_range(0, 0..3), Some(Err(0)));
assert_eq!(fzv.binary_search_in_range(111, 0..3), Some(Ok(0)));
assert_eq!(fzv.binary_search_in_range(199, 0..3), Some(Err(1)));
assert_eq!(fzv.binary_search_in_range(222, 0..3), Some(Ok(1)));
assert_eq!(fzv.binary_search_in_range(399, 0..3), Some(Err(2)));
assert_eq!(fzv.binary_search_in_range(444, 0..3), Some(Ok(2)));
assert_eq!(fzv.binary_search_in_range(999, 0..3), Some(Err(3)));
// Search in the second range:
assert_eq!(fzv.binary_search_in_range(0, 3..5), Some(Err(0)));
assert_eq!(fzv.binary_search_in_range(333, 3..5), Some(Ok(0)));
assert_eq!(fzv.binary_search_in_range(399, 3..5), Some(Err(1)));
assert_eq!(fzv.binary_search_in_range(555, 3..5), Some(Ok(1)));
assert_eq!(fzv.binary_search_in_range(999, 3..5), Some(Err(2)));
// Out-of-bounds range:
assert_eq!(fzv.binary_search_in_range(0, 4..6), None);
sourcepub fn binary_search_by(
&self,
predicate: impl FnMut(usize) -> Ordering
) -> Result<usize, usize>
pub fn binary_search_by(
&self,
predicate: impl FnMut(usize) -> Ordering
) -> Result<usize, usize>
Binary searches a sorted FlexZeroSlice
according to a predicate function.
sourcepub fn binary_search_in_range_by(
&self,
predicate: impl FnMut(usize) -> Ordering,
range: Range<usize>
) -> Option<Result<usize, usize>>
pub fn binary_search_in_range_by(
&self,
predicate: impl FnMut(usize) -> Ordering,
range: Range<usize>
) -> Option<Result<usize, usize>>
Binary searches a sorted range of a FlexZeroSlice
according to a predicate function.
The indices in the return value are relative to the start of the range.
sourcepub fn binary_search_with_index(
&self,
predicate: impl FnMut(usize) -> Ordering
) -> Result<usize, usize>
pub fn binary_search_with_index(
&self,
predicate: impl FnMut(usize) -> Ordering
) -> Result<usize, usize>
Binary searches a FlexZeroSlice
by its indices.
The predicate
function is passed in-bounds indices into the FlexZeroSlice
.
sourcepub fn binary_search_in_range_with_index(
&self,
predicate: impl FnMut(usize) -> Ordering,
range: Range<usize>
) -> Option<Result<usize, usize>>
pub fn binary_search_in_range_with_index(
&self,
predicate: impl FnMut(usize) -> Ordering,
range: Range<usize>
) -> Option<Result<usize, usize>>
Binary searches a range of a FlexZeroSlice
by its indices.
The predicate
function is passed in-bounds indices into the FlexZeroSlice
, which are
relative to the start of the entire slice.
The indices in the return value are relative to the start of the range.
Trait Implementations
sourceimpl<'a> AsRef<FlexZeroSlice> for FlexZeroVec<'a>
impl<'a> AsRef<FlexZeroSlice> for FlexZeroVec<'a>
sourcefn as_ref(&self) -> &FlexZeroSlice
fn as_ref(&self) -> &FlexZeroSlice
Converts this type into a shared reference of the (usually inferred) input type.
sourceimpl Bake for &FlexZeroSlice
impl Bake for &FlexZeroSlice
sourcefn bake(&self, env: &CrateEnv) -> TokenStream
fn bake(&self, env: &CrateEnv) -> TokenStream
Returns a TokenStream
that would evalutate to self
. Read more
sourceimpl Debug for &FlexZeroSlice
impl Debug for &FlexZeroSlice
sourceimpl<'de, 'a> Deserialize<'de> for &'a FlexZeroSlice where
'de: 'a,
impl<'de, 'a> Deserialize<'de> for &'a FlexZeroSlice where
'de: 'a,
This impl can be made available by enabling the optional serde
feature of the zerovec
crate
sourcefn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where
D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
sourceimpl From<&FlexZeroSlice> for FlexZeroVecOwned
impl From<&FlexZeroSlice> for FlexZeroVecOwned
sourcefn from(other: &FlexZeroSlice) -> Self
fn from(other: &FlexZeroSlice) -> Self
Converts to this type from the input type.
sourceimpl PartialEq<FlexZeroSlice> for FlexZeroSlice
impl PartialEq<FlexZeroSlice> for FlexZeroSlice
sourcefn eq(&self, other: &FlexZeroSlice) -> bool
fn eq(&self, other: &FlexZeroSlice) -> bool
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
sourcefn ne(&self, other: &FlexZeroSlice) -> bool
fn ne(&self, other: &FlexZeroSlice) -> bool
This method tests for !=
.
sourceimpl Serialize for FlexZeroSlice
impl Serialize for FlexZeroSlice
This impl can be made available by enabling the optional serde
feature of the zerovec
crate
sourceimpl<'zf> ZeroFrom<'zf, FlexZeroSlice> for &'zf FlexZeroSlice
impl<'zf> ZeroFrom<'zf, FlexZeroSlice> for &'zf FlexZeroSlice
sourcefn zero_from(other: &'zf FlexZeroSlice) -> Self
fn zero_from(other: &'zf FlexZeroSlice) -> Self
Clone the other C
into a struct that may retain references into C
.
sourceimpl<'zf> ZeroFrom<'zf, FlexZeroSlice> for FlexZeroVec<'zf>
impl<'zf> ZeroFrom<'zf, FlexZeroSlice> for FlexZeroVec<'zf>
sourcefn zero_from(other: &'zf FlexZeroSlice) -> Self
fn zero_from(other: &'zf FlexZeroSlice) -> Self
Clone the other C
into a struct that may retain references into C
.
sourceimpl ZeroVecLike<usize> for FlexZeroSlice
impl ZeroVecLike<usize> for FlexZeroSlice
type SliceVariant = FlexZeroSlice
type SliceVariant = FlexZeroSlice
A fully borrowed version of this
sourcefn zvl_new_borrowed() -> &'static Self::SliceVariant
fn zvl_new_borrowed() -> &'static Self::SliceVariant
Create a new, empty borrowed variant
sourcefn zvl_binary_search(&self, k: &usize) -> Result<usize, usize>
fn zvl_binary_search(&self, k: &usize) -> Result<usize, usize>
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
sourcefn zvl_binary_search_in_range(
&self,
k: &usize,
range: Range<usize>
) -> Option<Result<usize, usize>>
fn zvl_binary_search_in_range(
&self,
k: &usize,
range: Range<usize>
) -> Option<Result<usize, usize>>
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
sourcefn zvl_binary_search_by(
&self,
predicate: impl FnMut(&usize) -> Ordering
) -> Result<usize, usize>
fn zvl_binary_search_by(
&self,
predicate: impl FnMut(&usize) -> Ordering
) -> Result<usize, usize>
Search for a key in a sorted vector by a predicate, 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
sourcefn zvl_binary_search_in_range_by(
&self,
predicate: impl FnMut(&usize) -> Ordering,
range: Range<usize>
) -> Option<Result<usize, usize>>
fn zvl_binary_search_in_range_by(
&self,
predicate: impl FnMut(&usize) -> Ordering,
range: Range<usize>
) -> Option<Result<usize, usize>>
Search for a key within a certain range in a sorted vector by a predicate.
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
sourcefn zvl_as_borrowed(&self) -> &FlexZeroSlice
fn zvl_as_borrowed(&self) -> &FlexZeroSlice
Construct a borrowed variant by borrowing from &self
. Read more
sourcefn zvl_get_as_t<R>(g: &Self::GetType, f: impl FnOnce(&usize) -> R) -> R
fn zvl_get_as_t<R>(g: &Self::GetType, f: impl FnOnce(&usize) -> R) -> R
Obtain a reference to T, passed to a closure Read more
sourcefn zvl_is_ascending(&self) -> bool where
T: Ord,
fn zvl_is_ascending(&self) -> bool where
T: Ord,
Check if this vector is in ascending order according to T
s Ord
impl
sourcefn zvl_is_empty(&self) -> bool
fn zvl_is_empty(&self) -> bool
Check if this vector is empty