Struct zerovec::varzerovec::owned::VarZeroVecOwned

source ·
pub struct VarZeroVecOwned<T: ?Sized, F = Index16> {
    marker: PhantomData<(Box<T>, F)>,
    entire_slice: Vec<u8>,
}
Expand description

A fully-owned VarZeroVec. This type has no lifetime but has the same internal buffer representation of VarZeroVec, making it cheaply convertible to VarZeroVec and VarZeroSlice.

The F type parameter is a VarZeroVecFormat (see its docs for more details), which can be used to select the precise format of the backing buffer with various size and performance tradeoffs. It defaults to Index16.

Fields§

§marker: PhantomData<(Box<T>, F)>§entire_slice: Vec<u8>

Implementations§

source§

impl<T: VarULE + ?Sized, F> VarZeroVecOwned<T, F>

source

pub fn new() -> Self

Construct an empty VarZeroVecOwned

source§

impl<T: VarULE + ?Sized, F: VarZeroVecFormat> VarZeroVecOwned<T, F>

source

pub fn from_slice(slice: &VarZeroSlice<T, F>) -> Self

Construct a VarZeroVecOwned from a VarZeroSlice by cloning the internal data

source

pub fn try_from_elements<A>(elements: &[A]) -> Result<Self, &'static str>
where A: EncodeAsVarULE<T>,

Construct a VarZeroVecOwned from a list of elements

source

pub fn as_slice(&self) -> &VarZeroSlice<T, F>

Obtain this VarZeroVec as a VarZeroSlice

source

pub(crate) fn with_capacity(capacity: usize) -> Self

Try to allocate a buffer with enough capacity for capacity elements. Since T can take up an arbitrary size this will just allocate enough space for 4-byte Ts

source

pub(crate) fn reserve(&mut self, capacity: usize)

Try to reserve space for capacity elements. Since T can take up an arbitrary size this will just allocate enough space for 4-byte Ts

source

unsafe fn element_position_unchecked(&self, idx: usize) -> usize

Get the position of a specific element in the data segment.

If idx == self.len(), it will return the size of the data segment (where a new element would go).

§Safety

idx <= self.len() and self.as_encoded_bytes() is well-formed.

source

unsafe fn element_range_unchecked(&self, idx: usize) -> Range<usize>

Get the range of a specific element in the data segment.

§Safety

idx < self.len() and self.as_encoded_bytes() is well-formed.

source

unsafe fn set_len(&mut self, len: usize)

Set the number of elements in the list without any checks.

§Safety

No safe functions may be called until self.as_encoded_bytes() is well-formed.

source

fn index_range(index: usize) -> Range<usize>

source

unsafe fn index_data(&self, index: usize) -> &F::RawBytes

Return the raw bytes representing the given index.

§Safety

The index must be valid, and self.as_encoded_bytes() must be well-formed

source

unsafe fn index_data_mut(&mut self, index: usize) -> &mut F::RawBytes

Return the mutable slice representing the given index.

§Safety

The index must be valid. self.as_encoded_bytes() must have allocated space for this index, but need not have its length appropriately set.

source

unsafe fn shift_indices(&mut self, starting_index: usize, amount: i32)

Shift the indices starting with and after starting_index by the provided amount.

§Safety

Adding amount to each index after starting_index must not result in the slice from becoming malformed. The length of the slice must be correctly set.

source

pub fn as_varzerovec<'a>(&'a self) -> VarZeroVec<'a, T, F>

Get this VarZeroVecOwned as a borrowed VarZeroVec

If you wish to repeatedly call methods on this VarZeroVecOwned, it is more efficient to perform this conversion first

source

pub fn clear(&mut self)

Empty the vector

source

pub fn into_bytes(self) -> Vec<u8>

Consume this vector and return the backing buffer

source

unsafe fn shift( &mut self, index: usize, new_size: usize, shift_type: ShiftType, ) -> &mut [u8]

Invalidate and resize the data at an index, optionally inserting or removing the index. Also updates affected indices and the length. Returns a slice to the new element data - it doesn’t contain uninitialized data but its value is indeterminate.

§Safety
  • index must be a valid index, or, if shift_type == ShiftType::Insert, index == self.len() is allowed.
  • new_size musn’t result in the data segment growing larger than F::MAX_VALUE.
source

fn verify_integrity(&self) -> bool

Checks the internal invariants of the vec to ensure safe code will not cause UB. Returns whether integrity was verified.

Note: an index is valid if it doesn’t point to data past the end of the slice and is less than or equal to all future indices. The length of the index segment is not part of each index.

source

pub fn push<A: EncodeAsVarULE<T> + ?Sized>(&mut self, element: &A)

Insert an element at the end of this vector

source

pub fn insert<A: EncodeAsVarULE<T> + ?Sized>( &mut self, index: usize, element: &A, )

Insert an element at index idx

source

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

Remove the element at index idx

source

pub fn replace<A: EncodeAsVarULE<T> + ?Sized>( &mut self, index: usize, element: &A, )

Replace the element at index idx with another

Methods from Deref<Target = VarZeroSlice<T, F>>§

source

pub(crate) fn as_components<'a>(&'a self) -> VarZeroVecComponents<'a, T, F>

Obtain a VarZeroVecComponents borrowing from the internal buffer

source

pub fn len(&self) -> usize

Get the number of elements in this slice

§Example

let strings = vec!["foo", "bar", "baz", "quux"];
let vec = VarZeroVec::<str>::from(&strings);

assert_eq!(vec.len(), 4);
source

pub fn is_empty(&self) -> bool

Returns true if the slice contains no elements.

§Examples

let strings: Vec<String> = vec![];
let vec = VarZeroVec::<str>::from(&strings);

assert!(vec.is_empty());
source

pub fn iter<'b>(&'b self) -> impl Iterator<Item = &'b T>

Obtain an iterator over this slice’s elements

§Example

let strings = vec!["foo", "bar", "baz", "quux"];
let vec = VarZeroVec::<str>::from(&strings);

let mut iter_results: Vec<&str> = vec.iter().collect();
assert_eq!(iter_results[0], "foo");
assert_eq!(iter_results[1], "bar");
assert_eq!(iter_results[2], "baz");
assert_eq!(iter_results[3], "quux");
source

pub fn get(&self, idx: usize) -> Option<&T>

Get one of this slice’s elements, returning None if the index is out of bounds

§Example

let strings = vec!["foo", "bar", "baz", "quux"];
let vec = VarZeroVec::<str>::from(&strings);

let mut iter_results: Vec<&str> = vec.iter().collect();
assert_eq!(vec.get(0), Some("foo"));
assert_eq!(vec.get(1), Some("bar"));
assert_eq!(vec.get(2), Some("baz"));
assert_eq!(vec.get(3), Some("quux"));
assert_eq!(vec.get(4), None);
source

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

Get one of this slice’s elements

§Safety

index must be in range

§Example

let strings = vec!["foo", "bar", "baz", "quux"];
let vec = VarZeroVec::<str>::from(&strings);

let mut iter_results: Vec<&str> = vec.iter().collect();
unsafe {
    assert_eq!(vec.get_unchecked(0), "foo");
    assert_eq!(vec.get_unchecked(1), "bar");
    assert_eq!(vec.get_unchecked(2), "baz");
    assert_eq!(vec.get_unchecked(3), "quux");
}
source

pub fn to_vec(&self) -> Vec<Box<T>>

Obtain an owned Vec<Box<T>> out of this

source

pub fn as_bytes(&self) -> &[u8]

Get a reference to the entire encoded backing buffer of this slice

The bytes can be passed back to Self::parse_byte_slice().

To take the bytes as a vector, see VarZeroVec::into_bytes().

§Example

let strings = vec!["foo", "bar", "baz"];
let vzv = VarZeroVec::<str>::from(&strings);

assert_eq!(vzv, VarZeroVec::parse_byte_slice(vzv.as_bytes()).unwrap());
source

pub fn as_varzerovec<'a>(&'a self) -> VarZeroVec<'a, T, F>

Get this VarZeroSlice as a borrowed VarZeroVec

If you wish to repeatedly call methods on this VarZeroSlice, it is more efficient to perform this conversion first

Binary searches a sorted VarZeroVec<T> for the given element. For more information, see the standard library function binary_search.

§Example

let strings = vec!["a", "b", "f", "g"];
let vec = VarZeroVec::<str>::from(&strings);

assert_eq!(vec.binary_search("f"), Ok(2));
assert_eq!(vec.binary_search("e"), Err(2));
source

pub fn binary_search_in_range( &self, x: &T, range: Range<usize>, ) -> Option<Result<usize, usize>>

Binary searches a VarZeroVec<T> for the given element within a certain sorted range.

If the range is out of bounds, returns None. Otherwise, returns a Result according to the behavior of the standard library function binary_search.

The index is returned relative to the start of the range.

§Example

let strings = vec!["a", "b", "f", "g", "m", "n", "q"];
let vec = VarZeroVec::<str>::from(&strings);

// Same behavior as binary_search when the range covers the whole slice:
assert_eq!(vec.binary_search_in_range("g", 0..7), Some(Ok(3)));
assert_eq!(vec.binary_search_in_range("h", 0..7), Some(Err(4)));

// Will not look outside of the range:
assert_eq!(vec.binary_search_in_range("g", 0..1), Some(Err(1)));
assert_eq!(vec.binary_search_in_range("g", 6..7), Some(Err(0)));

// Will return indices relative to the start of the range:
assert_eq!(vec.binary_search_in_range("g", 1..6), Some(Ok(2)));
assert_eq!(vec.binary_search_in_range("h", 1..6), Some(Err(3)));

// Will return `None` if the range is out of bounds:
assert_eq!(vec.binary_search_in_range("x", 100..200), None);
assert_eq!(vec.binary_search_in_range("x", 0..200), None);
source

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

Binary searches a sorted VarZeroVec<T> for the given predicate. For more information, see the standard library function binary_search_by.

§Example

let strings = vec!["a", "b", "f", "g"];
let vec = VarZeroVec::<str>::from(&strings);

assert_eq!(vec.binary_search_by(|probe| probe.cmp("f")), Ok(2));
assert_eq!(vec.binary_search_by(|probe| probe.cmp("e")), Err(2));
source

pub fn binary_search_in_range_by( &self, predicate: impl FnMut(&T) -> Ordering, range: Range<usize>, ) -> Option<Result<usize, usize>>

Binary searches a VarZeroVec<T> for the given predicate within a certain sorted range.

If the range is out of bounds, returns None. Otherwise, returns a Result according to the behavior of the standard library function binary_search.

The index is returned relative to the start of the range.

§Example

let strings = vec!["a", "b", "f", "g", "m", "n", "q"];
let vec = VarZeroVec::<str>::from(&strings);

// Same behavior as binary_search when the range covers the whole slice:
assert_eq!(
    vec.binary_search_in_range_by(|v| v.cmp("g"), 0..7),
    Some(Ok(3))
);
assert_eq!(
    vec.binary_search_in_range_by(|v| v.cmp("h"), 0..7),
    Some(Err(4))
);

// Will not look outside of the range:
assert_eq!(
    vec.binary_search_in_range_by(|v| v.cmp("g"), 0..1),
    Some(Err(1))
);
assert_eq!(
    vec.binary_search_in_range_by(|v| v.cmp("g"), 6..7),
    Some(Err(0))
);

// Will return indices relative to the start of the range:
assert_eq!(
    vec.binary_search_in_range_by(|v| v.cmp("g"), 1..6),
    Some(Ok(2))
);
assert_eq!(
    vec.binary_search_in_range_by(|v| v.cmp("h"), 1..6),
    Some(Err(3))
);

// Will return `None` if the range is out of bounds:
assert_eq!(
    vec.binary_search_in_range_by(|v| v.cmp("x"), 100..200),
    None
);
assert_eq!(vec.binary_search_in_range_by(|v| v.cmp("x"), 0..200), None);

Trait Implementations§

source§

impl<T: ?Sized, F> Clone for VarZeroVecOwned<T, F>

source§

fn clone(&self) -> Self

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<T, F: VarZeroVecFormat> Debug for VarZeroVecOwned<T, F>
where T: Debug + VarULE + ?Sized,

source§

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

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

impl<T: VarULE + ?Sized, F> Default for VarZeroVecOwned<T, F>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<T: VarULE + ?Sized, F: VarZeroVecFormat> Deref for VarZeroVecOwned<T, F>

§

type Target = VarZeroSlice<T, F>

The resulting type after dereferencing.
source§

fn deref(&self) -> &VarZeroSlice<T, F>

Dereferences the value.
source§

impl<'a, T: ?Sized + VarULE, F: VarZeroVecFormat> From<&'a VarZeroSlice<T, F>> for VarZeroVecOwned<T, F>

source§

fn from(other: &'a VarZeroSlice<T, F>) -> Self

Converts to this type from the input type.
source§

impl<'a, T: ?Sized + VarULE, F: VarZeroVecFormat> From<VarZeroVec<'a, T, F>> for VarZeroVecOwned<T, F>

source§

fn from(other: VarZeroVec<'a, T, F>) -> Self

Converts to this type from the input type.
source§

impl<'a, T: ?Sized, F> From<VarZeroVecOwned<T, F>> for VarZeroVec<'a, T, F>

source§

fn from(other: VarZeroVecOwned<T, F>) -> Self

Converts to this type from the input type.
source§

impl<T, A, F> PartialEq<&[A]> for VarZeroVecOwned<T, F>
where T: VarULE + ?Sized + PartialEq, A: AsRef<T>, F: VarZeroVecFormat,

source§

fn eq(&self, other: &&[A]) -> bool

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

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

§

impl<T, F> Freeze for VarZeroVecOwned<T, F>
where T: ?Sized,

§

impl<T, F> RefUnwindSafe for VarZeroVecOwned<T, F>

§

impl<T, F> Send for VarZeroVecOwned<T, F>
where F: Send, T: Send + ?Sized,

§

impl<T, F> Sync for VarZeroVecOwned<T, F>
where F: Sync, T: Sync + ?Sized,

§

impl<T, F> Unpin for VarZeroVecOwned<T, F>
where F: Unpin, T: ?Sized,

§

impl<T, F> UnwindSafe for VarZeroVecOwned<T, F>
where F: UnwindSafe, T: UnwindSafe + ?Sized,

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> 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,

§

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>,

§

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>,

§

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,