pub struct FlexZeroVecOwned(/* private fields */);
Expand description
The fully-owned variant of FlexZeroVec
. Contains all mutation methods.
Implementations§
Source§impl FlexZeroVecOwned
impl FlexZeroVecOwned
Sourcepub fn new_empty() -> Self
pub fn new_empty() -> Self
Creates a new FlexZeroVecOwned
with zero elements.
Sourcepub fn from_slice(other: &FlexZeroSlice) -> FlexZeroVecOwned
pub fn from_slice(other: &FlexZeroSlice) -> FlexZeroVecOwned
Creates a FlexZeroVecOwned
from a FlexZeroSlice
.
Sourcepub fn as_slice(&self) -> &FlexZeroSlice
pub fn as_slice(&self) -> &FlexZeroSlice
Obtains this FlexZeroVecOwned
as a FlexZeroSlice
.
Sourcepub fn into_flexzerovec(self) -> FlexZeroVec<'static>
pub fn into_flexzerovec(self) -> FlexZeroVec<'static>
Converts this FlexZeroVecOwned
into a FlexZeroVec::Owned
.
Sourcepub fn push(&mut self, item: usize)
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]);
Sourcepub fn insert(&mut self, index: usize, item: usize)
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]);
Sourcepub fn insert_sorted(&mut self, item: usize)
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()));
Sourcepub fn remove(&mut self, index: usize) -> usize
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]);
Sourcepub fn pop_sorted(&mut self) -> usize
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>§
Sourcepub fn as_bytes(&self) -> &[u8]
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());
Sourcepub fn as_flexzerovec(&self) -> FlexZeroVec<'_>
pub 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> + '_ + ExactSizeIterator<Item = usize>
pub fn iter( &self, ) -> impl DoubleEndedIterator<Item = usize> + '_ + ExactSizeIterator<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§
Source§impl Clone for FlexZeroVecOwned
impl Clone for FlexZeroVecOwned
Source§fn clone(&self) -> FlexZeroVecOwned
fn clone(&self) -> FlexZeroVecOwned
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for FlexZeroVecOwned
impl Debug for FlexZeroVecOwned
Source§impl Deref for FlexZeroVecOwned
impl Deref for FlexZeroVecOwned
Source§impl From<&FlexZeroSlice> for FlexZeroVecOwned
impl From<&FlexZeroSlice> for FlexZeroVecOwned
Source§fn from(other: &FlexZeroSlice) -> Self
fn from(other: &FlexZeroSlice) -> Self
Source§impl FromIterator<usize> for FlexZeroVecOwned
impl FromIterator<usize> for FlexZeroVecOwned
Source§fn from_iter<I>(iter: I) -> Selfwhere
I: IntoIterator<Item = usize>,
fn from_iter<I>(iter: I) -> Selfwhere
I: IntoIterator<Item = usize>,
Creates a FlexZeroVecOwned
from an iterator of usize
.
Source§impl PartialEq for FlexZeroVecOwned
impl PartialEq for FlexZeroVecOwned
impl Eq for FlexZeroVecOwned
impl StructuralPartialEq for FlexZeroVecOwned
Auto Trait Implementations§
impl Freeze for FlexZeroVecOwned
impl RefUnwindSafe for FlexZeroVecOwned
impl Send for FlexZeroVecOwned
impl Sync for FlexZeroVecOwned
impl Unpin for FlexZeroVecOwned
impl UnwindSafe for FlexZeroVecOwned
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)