Struct zerovec::flexzerovec::owned::FlexZeroVecOwned

source ·
pub struct FlexZeroVecOwned(Vec<u8>);
Expand description

The fully-owned variant of FlexZeroVec. Contains all mutation methods.

Tuple Fields§

§0: Vec<u8>

Implementations§

source§

impl FlexZeroVecOwned

source

pub fn new_empty() -> Self

Creates a new FlexZeroVecOwned with zero elements.

source

pub fn from_slice(other: &FlexZeroSlice) -> FlexZeroVecOwned

Creates a FlexZeroVecOwned from a FlexZeroSlice.

source

pub fn as_slice(&self) -> &FlexZeroSlice

Obtains this FlexZeroVecOwned as a FlexZeroSlice.

source

pub(crate) fn as_mut_slice(&mut self) -> &mut FlexZeroSlice

Mutably obtains this FlexZeroVecOwned as a FlexZeroSlice.

source

pub fn into_flexzerovec(self) -> FlexZeroVec<'static>

Converts this FlexZeroVecOwned into a FlexZeroVec::Owned.

source

pub fn clear(&mut self)

Clears all values out of this FlexZeroVecOwned.

source

pub fn push(&mut self, item: usize)

Appends an item to the end of the vector.

§Panics

Panics if inserting the element would require allocating more than usize::MAX bytes.

§Examples
use zerovec::vecs::FlexZeroVec;

let mut zv: FlexZeroVec = [22, 44, 66].iter().copied().collect();
zv.to_mut().push(33);
assert_eq!(zv.to_vec(), vec![22, 44, 66, 33]);
source

pub fn insert(&mut self, index: usize, item: usize)

Inserts an element into the middle of the vector.

Caution: Both arguments to this function are of type usize. Please be careful to pass the index first followed by the value second.

§Panics

Panics if index > len.

Panics if inserting the element would require allocating more than usize::MAX bytes.

§Examples
use zerovec::vecs::FlexZeroVec;

let mut zv: FlexZeroVec = [22, 44, 66].iter().copied().collect();
zv.to_mut().insert(2, 33);
assert_eq!(zv.to_vec(), vec![22, 44, 33, 66]);
source

pub fn insert_sorted(&mut self, item: usize)

Inserts an element into an ascending sorted vector at a position that keeps the vector sorted.

§Panics

Panics if inserting the element would require allocating more than usize::MAX bytes.

§Examples
use zerovec::vecs::FlexZeroVecOwned;

let mut fzv = FlexZeroVecOwned::new_empty();
fzv.insert_sorted(10);
fzv.insert_sorted(5);
fzv.insert_sorted(8);

assert!(Iterator::eq(fzv.iter(), [5, 8, 10].iter().copied()));
source

pub fn remove(&mut self, index: usize) -> usize

Removes and returns the element at the specified index.

§Panics

Panics if index >= len.

§Examples
use zerovec::vecs::FlexZeroVec;

let mut zv: FlexZeroVec = [22, 44, 66].iter().copied().collect();
let removed_item = zv.to_mut().remove(1);
assert_eq!(44, removed_item);
assert_eq!(zv.to_vec(), vec![22, 66]);
source

pub fn pop_sorted(&mut self) -> usize

Removes and returns the last element from an ascending sorted vector.

If the vector is not sorted, use FlexZeroVecOwned::remove() instead. Calling this function would leave the FlexZeroVec in a safe, well-defined state; however, information may be lost and/or the equality invariant might not hold.

§Panics

Panics if self.is_empty().

§Examples
use zerovec::vecs::FlexZeroVec;

let mut zv: FlexZeroVec = [22, 44, 66].iter().copied().collect();
let popped_item = zv.to_mut().pop_sorted();
assert_eq!(66, popped_item);
assert_eq!(zv.to_vec(), vec![22, 44]);

Calling this function on a non-ascending vector could cause surprising results:

use zerovec::vecs::FlexZeroVec;

let mut zv1: FlexZeroVec = [444, 222, 111].iter().copied().collect();
let popped_item = zv1.to_mut().pop_sorted();
assert_eq!(111, popped_item);

// Oops!
assert_eq!(zv1.to_vec(), vec![188, 222]);

Methods from Deref<Target = FlexZeroSlice>§

source

pub fn as_bytes(&self) -> &[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());
source

pub fn as_flexzerovec(&self) -> FlexZeroVec<'_>

Borrows this FlexZeroSlice as a FlexZeroVec::Borrowed.

source

pub fn len(&self) -> usize

Returns the number of elements in the FlexZeroSlice.

source

pub(crate) fn get_width(&self) -> usize

source

pub fn is_empty(&self) -> bool

Returns whether there are zero elements in the FlexZeroSlice.

source

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);
source

pub(crate) fn get_chunk(&self, index: usize) -> Option<&[u8]>

Gets the element at index as a chunk of bytes, or None if index >= self.len().

source

pub unsafe fn get_unchecked(&self, index: usize) -> usize

Gets the element at index without checking bounds.

§Safety

index must be in-range.

source

pub fn first(&self) -> Option<usize>

Gets the first element of the slice, or None if the slice is empty.

source

pub fn last(&self) -> Option<usize>

Gets the last element of the slice, or None if the slice is empty.

source

pub fn iter( &self, ) -> impl DoubleEndedIterator<Item = usize> + '_ + ExactSizeIterator<Item = usize>

Gets an iterator over the elements of the slice as usize.

source

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);
source

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());

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));
source

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);
source

pub fn binary_search_by( &self, predicate: impl FnMut(usize) -> Ordering, ) -> Result<usize, usize>

Binary searches a sorted FlexZeroSlice according to a predicate function.

source

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.

source

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.

source

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.

source

fn binary_search_impl( &self, predicate: impl FnMut(usize) -> Ordering, scaled_slice: &[u8], ) -> Result<usize, usize>

§Safety

scaled_slice must be a subslice of self.data

source

fn binary_search_with_index_impl( &self, predicate: impl FnMut(usize) -> Ordering, scaled_slice: &[u8], ) -> Result<usize, usize>

predicate is passed a valid index as an argument.

§Safety

scaled_slice must be a subslice of self.data

source

pub(crate) fn get_insert_info(&self, new_item: usize) -> InsertInfo

Compute the InsertInfo for inserting the specified item anywhere into the vector.

§Panics

Panics if inserting the element would require allocating more than usize::MAX bytes.

source

pub(crate) fn get_remove_info(&self, remove_index: usize) -> RemoveInfo

Compute the RemoveInfo for removing the item at the specified index.

source

pub(crate) fn get_sorted_pop_info(&self) -> RemoveInfo

Returns the RemoveInfo for removing the last element. Should be called on a slice sorted in ascending order.

This is more efficient than get_remove_info() because it doesn’t require a linear traversal of the vector in order to calculate new_width.

Trait Implementations§

source§

impl Clone for FlexZeroVecOwned

source§

fn clone(&self) -> FlexZeroVecOwned

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for FlexZeroVecOwned

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Deref for FlexZeroVecOwned

source§

type Target = FlexZeroSlice

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl From<&FlexZeroSlice> for FlexZeroVecOwned

source§

fn from(other: &FlexZeroSlice) -> Self

Converts to this type from the input type.
source§

impl FromIterator<usize> for FlexZeroVecOwned

source§

fn from_iter<I>(iter: I) -> Self
where I: IntoIterator<Item = usize>,

Creates a FlexZeroVecOwned from an iterator of usize.

source§

impl PartialEq for FlexZeroVecOwned

source§

fn eq(&self, other: &FlexZeroVecOwned) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for FlexZeroVecOwned

source§

impl StructuralPartialEq for FlexZeroVecOwned

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

source§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> ErasedDestructor for T
where T: 'static,