[][src]Struct tinyvec::SliceVec

pub struct SliceVec<'s, T> { /* fields omitted */ }

A slice-backed vector-like data structure.

This is a very similar concept to ArrayVec, but instead of the backing memory being an owned array, the backing memory is a unique-borrowed slice. You can thus create one of these structures "around" some slice that you're working with to make it easier to manipulate.

  • Has a fixed capacity (the initial slice size).
  • Has a variable length.

Implementations

impl<'s, T> SliceVec<'s, T>[src]

pub fn append(&mut self, other: &mut Self) where
    T: Default
[src]

#[must_use]pub fn as_mut_ptr(&mut self) -> *mut T[src]

A *mut pointer to the backing slice.

Safety

This pointer has provenance over the entire backing slice.

#[must_use]pub fn as_mut_slice(&mut self) -> &mut [T][src]

Performs a deref_mut, into unique slice form.

#[must_use]pub fn as_ptr(&self) -> *const T[src]

A *const pointer to the backing slice.

Safety

This pointer has provenance over the entire backing slice.

#[must_use]pub fn as_slice(&self) -> &[T][src]

Performs a deref, into shared slice form.

#[must_use]pub fn capacity(&self) -> usize[src]

The capacity of the SliceVec.

This the length of the initial backing slice.

pub fn clear(&mut self) where
    T: Default
[src]

Truncates the SliceVec down to length 0.

pub fn drain<'p, R: RangeBounds<usize>>(
    &'p mut self,
    range: R
) -> SliceVecDrain<'p, 's, T>

Important traits for SliceVecDrain<'p, 's, T>

impl<'p, 's, T: Default> Iterator for SliceVecDrain<'p, 's, T> type Item = T;
where
    T: Default
[src]

Creates a draining iterator that removes the specified range in the vector and yields the removed items.

Panics

  • If the start is greater than the end
  • If the end is past the edge of the vec.

Example

let mut arr = [6, 7, 8];
let mut sv = SliceVec::from(&mut arr);
let drained_values: ArrayVec<[i32; 4]> = sv.drain(1..).collect();
assert_eq!(sv.as_slice(), &[6][..]);
assert_eq!(drained_values.as_slice(), &[7, 8][..]);

sv.drain(..);
assert_eq!(sv.as_slice(), &[]);

pub fn extend_from_slice(&mut self, sli: &[T]) where
    T: Clone
[src]

pub fn fill<I: IntoIterator<Item = T>>(&mut self, iter: I) -> I::IntoIter[src]

Fill the vector until its capacity has been reached.

Successively fills unused space in the spare slice of the vector with elements from the iterator. It then returns the remaining iterator without exhausting it. This also allows appending the head of an infinite iterator.

This is an alternative to Extend::extend method for cases where the length of the iterator can not be checked. Since this vector can not reallocate to increase its capacity, it is unclear what to do with remaining elements in the iterator and the iterator itself. The interface also provides no way to communicate this to the caller.

Panics

  • If the next method of the provided iterator panics.

Example

let mut arr = [7, 7, 7, 7];
let mut sv = SliceVec::from_slice_len(&mut arr, 0);
let mut to_inf = sv.fill(0..);
assert_eq!(&sv[..], [0, 1, 2, 3]);
assert_eq!(to_inf.next(), Some(4));

#[must_use]pub fn from_slice_len(data: &'s mut [T], len: usize) -> Self[src]

Wraps up a slice and uses the given length as the initial length.

If you want to simply use the full slice, use from instead.

Panics

  • The length specified must be less than or equal to the capacity of the slice.

pub fn insert(&mut self, index: usize, item: T)[src]

Inserts an item at the position given, moving all following elements +1 index.

Panics

  • If index > len
  • If the capacity is exhausted

Example

let mut arr = [1, 2, 3, 0, 0];
let mut sv = SliceVec::from_slice_len(&mut arr, 3);
sv.insert(1, 4);
assert_eq!(sv.as_slice(), &[1, 4, 2, 3]);
sv.insert(4, 5);
assert_eq!(sv.as_slice(), &[1, 4, 2, 3, 5]);

#[must_use]pub fn is_empty(&self) -> bool[src]

Checks if the length is 0.

#[must_use]pub fn len(&self) -> usize[src]

The length of the SliceVec (in elements).

pub fn pop(&mut self) -> Option<T> where
    T: Default
[src]

Remove and return the last element of the vec, if there is one.

Failure

  • If the vec is empty you get None.

Example

let mut arr = [1, 2];
let mut sv = SliceVec::from(&mut arr);
assert_eq!(sv.pop(), Some(2));
assert_eq!(sv.pop(), Some(1));
assert_eq!(sv.pop(), None);

pub fn push(&mut self, val: T)[src]

Place an element onto the end of the vec.

Panics

  • If the length of the vec would overflow the capacity.

Example

let mut arr = [0, 0];
let mut sv = SliceVec::from_slice_len(&mut arr, 0);
assert_eq!(&sv[..], []);
sv.push(1);
assert_eq!(&sv[..], [1]);
sv.push(2);
assert_eq!(&sv[..], [1, 2]);
// sv.push(3); this would overflow the ArrayVec and panic!

pub fn remove(&mut self, index: usize) -> T where
    T: Default
[src]

Removes the item at index, shifting all others down by one index.

Returns the removed element.

Panics

  • If the index is out of bounds.

Example

let mut arr = [1, 2, 3];
let mut sv = SliceVec::from(&mut arr);
assert_eq!(sv.remove(1), 2);
assert_eq!(&sv[..], [1, 3]);

pub fn resize(&mut self, new_len: usize, new_val: T) where
    T: Clone
[src]

As resize_with and it clones the value as the closure.

Example

// bigger
let mut arr = ["hello", "", "", "", ""];
let mut sv = SliceVec::from_slice_len(&mut arr, 1);
sv.resize(3, "world");
assert_eq!(&sv[..], ["hello", "world", "world"]);

// smaller
let mut arr = ['a', 'b', 'c', 'd'];
let mut sv = SliceVec::from(&mut arr);
sv.resize(2, 'z');
assert_eq!(&sv[..], ['a', 'b']);

pub fn resize_with<F: FnMut() -> T>(&mut self, new_len: usize, f: F)[src]

Resize the vec to the new length.

  • If it needs to be longer, it's filled with repeated calls to the provided function.
  • If it needs to be shorter, it's truncated.
    • If the type needs to drop the truncated slots are filled with calls to the provided function.

Example

let mut arr = [1, 2, 3, 7, 7, 7, 7];
let mut sv = SliceVec::from_slice_len(&mut arr, 3);
sv.resize_with(5, Default::default);
assert_eq!(&sv[..], [1, 2, 3, 0, 0]);

let mut arr = [0, 0, 0, 0];
let mut sv = SliceVec::from_slice_len(&mut arr, 0);
let mut p = 1;
sv.resize_with(4, || {
  p *= 2;
  p
});
assert_eq!(&sv[..], [2, 4, 8, 16]);

pub fn retain<F: FnMut(&T) -> bool>(&mut self, acceptable: F) where
    T: Default
[src]

Walk the vec and keep only the elements that pass the predicate given.

Example


let mut arr = [1, 1, 2, 3, 3, 4];
let mut sv = SliceVec::from(&mut arr);
sv.retain(|&x| x % 2 == 0);
assert_eq!(&sv[..], [2, 4]);

pub fn set_len(&mut self, new_len: usize)[src]

Forces the length of the vector to new_len.

Panics

  • If new_len is greater than the vec's capacity.

Safety

  • This is a fully safe operation! The inactive memory already counts as "initialized" by Rust's rules.
  • Other than "the memory is initialized" there are no other guarantees regarding what you find in the inactive portion of the vec.

pub fn split_off<'a>(&'a mut self, at: usize) -> SliceVec<'s, T>[src]

Splits the collection at the point given.

  • [0, at) stays in this vec (and this vec is now full).
  • [at, len) ends up in the new vec (with any spare capacity).

Panics

  • if at > self.len()

Example

let mut arr = [1, 2, 3];
let mut sv = SliceVec::from(&mut arr);
let sv2 = sv.split_off(1);
assert_eq!(&sv[..], [1]);
assert_eq!(&sv2[..], [2, 3]);

pub fn swap_remove(&mut self, index: usize) -> T where
    T: Default
[src]

Remove an element, swapping the end of the vec into its place.

Panics

  • If the index is out of bounds.

Example

let mut arr = ["foo", "bar", "quack", "zap"];
let mut sv = SliceVec::from(&mut arr);

assert_eq!(sv.swap_remove(1), "bar");
assert_eq!(&sv[..], ["foo", "zap", "quack"]);

assert_eq!(sv.swap_remove(0), "foo");
assert_eq!(&sv[..], ["quack", "zap"]);

pub fn truncate(&mut self, new_len: usize) where
    T: Default
[src]

Reduces the vec's length to the given value.

If the vec is already shorter than the input, nothing happens.

pub fn try_from_slice_len(data: &'s mut [T], len: usize) -> Option<Self>[src]

Wraps a slice, using the given length as the starting length.

If you want to use the whole length of the slice, you can just use the From impl.

Failure

If the given length is greater than the length of the slice you get None.

impl<'s, T> SliceVec<'s, T>[src]

pub fn grab_spare_slice(&self) -> &[T][src]

Obtain the shared slice of the array after the active memory.

Example

let mut arr = [0; 4];
let mut sv = SliceVec::from_slice_len(&mut arr, 0);
assert_eq!(sv.grab_spare_slice().len(), 4);
sv.push(10);
sv.push(11);
sv.push(12);
sv.push(13);
assert_eq!(sv.grab_spare_slice().len(), 0);

pub fn grab_spare_slice_mut(&mut self) -> &mut [T][src]

Obtain the mutable slice of the array after the active memory.

Example

let mut arr = [0; 4];
let mut sv = SliceVec::from_slice_len(&mut arr, 0);
assert_eq!(sv.grab_spare_slice_mut().len(), 4);
sv.push(10);
sv.push(11);
assert_eq!(sv.grab_spare_slice_mut().len(), 2);

Trait Implementations

impl<'s, T> AsMut<[T]> for SliceVec<'s, T>[src]

impl<'s, T> AsRef<[T]> for SliceVec<'s, T>[src]

impl<'s, T> Binary for SliceVec<'s, T> where
    T: Binary
[src]

impl<'s, T> Borrow<[T]> for SliceVec<'s, T>[src]

impl<'s, T> BorrowMut<[T]> for SliceVec<'s, T>[src]

impl<'s, T> Debug for SliceVec<'s, T> where
    T: Debug
[src]

impl<'s, T> Default for SliceVec<'s, T>[src]

impl<'s, T> Deref for SliceVec<'s, T>[src]

type Target = [T]

The resulting type after dereferencing.

impl<'s, T> DerefMut for SliceVec<'s, T>[src]

impl<'s, T> Display for SliceVec<'s, T> where
    T: Display
[src]

impl<'s, T> Eq for SliceVec<'s, T> where
    T: Eq
[src]

impl<'s, T> Extend<T> for SliceVec<'s, T>[src]

impl<'s, T> From<&'s mut [T]> for SliceVec<'s, T>[src]

fn from(data: &'s mut [T]) -> Self[src]

Uses the full slice as the initial length.

Example

let mut arr = [0_i32; 2];
let mut sv = SliceVec::from(&mut arr[..]);

impl<'s, T, A> From<&'s mut A> for SliceVec<'s, T> where
    A: AsMut<[T]>, 
[src]

fn from(a: &'s mut A) -> Self[src]

Calls AsRef::as_mut then uses the full slice as the initial length.

Example

let mut arr = [0, 0];
let mut sv = SliceVec::from(&mut arr);

impl<'s, T> Hash for SliceVec<'s, T> where
    T: Hash
[src]

impl<'s, T, I> Index<I> for SliceVec<'s, T> where
    I: SliceIndex<[T]>, 
[src]

type Output = <I as SliceIndex<[T]>>::Output

The returned type after indexing.

impl<'s, T, I> IndexMut<I> for SliceVec<'s, T> where
    I: SliceIndex<[T]>, 
[src]

impl<'s, T> IntoIterator for SliceVec<'s, T>[src]

type Item = &'s mut T

The type of the elements being iterated over.

type IntoIter = IterMut<'s, T>

Which kind of iterator are we turning this into?

impl<'s, T> LowerExp for SliceVec<'s, T> where
    T: LowerExp
[src]

impl<'s, T> LowerHex for SliceVec<'s, T> where
    T: LowerHex
[src]

impl<'s, T> Octal for SliceVec<'s, T> where
    T: Octal
[src]

impl<'s, T> Ord for SliceVec<'s, T> where
    T: Ord
[src]

impl<'s, '_, T> PartialEq<&'_ [T]> for SliceVec<'s, T> where
    T: PartialEq
[src]

impl<'s, T> PartialEq<SliceVec<'s, T>> for SliceVec<'s, T> where
    T: PartialEq
[src]

impl<'s, T> PartialOrd<SliceVec<'s, T>> for SliceVec<'s, T> where
    T: PartialOrd
[src]

impl<'s, T> Pointer for SliceVec<'s, T> where
    T: Pointer
[src]

impl<'s, T> UpperExp for SliceVec<'s, T> where
    T: UpperExp
[src]

impl<'s, T> UpperHex for SliceVec<'s, T> where
    T: UpperHex
[src]

impl<'s> Write for SliceVec<'s, u8>[src]

Auto Trait Implementations

impl<'s, T> Send for SliceVec<'s, T> where
    T: Send

impl<'s, T> Sync for SliceVec<'s, T> where
    T: Sync

impl<'s, T> Unpin for SliceVec<'s, T>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<I> IntoIterator for I where
    I: Iterator
[src]

type Item = <I as Iterator>::Item

The type of the elements being iterated over.

type IntoIter = I

Which kind of iterator are we turning this into?

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.