# tinyvec::SliceVec - Rust
API documentation for the Rust `SliceVec` struct in crate `tinyvec`.
Keywords: rust, rustlang, rust-lang, SliceVec

# Struct tinyvec::SliceVec [−] [src]

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

### Notable 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`.

## Trait Implementations

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

#### `#[must_use]fn as_mut(&mut self) -> &mut [T]` [src]

Performs the conversion.

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

#### `#[must_use]fn as_ref(&self) -> &[T]` [src]

Performs the conversion.

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

#### `fn fmt(&self, f: &mut Formatter<'_>) -> Result` [src]

Formats the value using the given formatter.

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

#### `#[must_use]fn borrow(&self) -> &[T]` [src]

Immutably borrows from an owned value. Read more

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

#### `#[must_use]fn borrow_mut(&mut self) -> &mut [T]` [src]

Mutably borrows from an owned value. Read more

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

#### `fn fmt(&self, f: &mut Formatter<'_>) -> Result` [src]

Formats the value using the given formatter. Read more

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

#### `#[must_use]fn default() -> Self` [src]

Returns the "default value" for a type. Read more

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

#### `type Target = [T]`

The resulting type after dereferencing.

#### `#[must_use]fn deref(&self) -> &Self::Target` [src]

Dereferences the value.

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

#### `#[must_use]fn deref_mut(&mut self) -> &mut Self::Target` [src]

Mutably dereferences the value.

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

#### `fn fmt(&self, f: &mut Formatter<'_>) -> Result` [src]

Formats the value using the given formatter. Read more

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

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

#### `fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)` [src]

Extends a collection with the contents of an iterator. Read more

#### `pub fn extend_one(&mut self, item: A)` [src]

🔬 This is a nightly-only experimental API. (`extend_one`)

Extends a collection with exactly one element.

#### `pub fn extend_reserve(&mut self, additional: usize)` [src]

🔬 This is a nightly-only experimental API. (`extend_one`)

Reserves capacity in a collection for the given number of additional elements. Read more

### `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]

#### `fn hash<H: Hasher>(&self, state: &mut H)` [src]

Feeds this value into the given `Hasher`. Read more

#### `pub fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,` 1.3.0 [src]

Feeds a slice of this type into the given `Hasher`. Read more

### `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.

#### `#[must_use]fn index(&self, index: I) -> &Self::Output` [src]

Performs the indexing (`container[index]`) operation.

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

#### `#[must_use]fn index_mut(&mut self, index: I) -> &mut Self::Output` [src]

Performs the mutable indexing (`container[index]`) operation.

### `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?

#### `#[must_use]fn into_iter(self) -> Self::IntoIter` [src]

Creates an iterator from a value. Read more

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

#### `fn fmt(&self, f: &mut Formatter<'_>) -> Result` [src]

Formats the value using the given formatter.

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

#### `fn fmt(&self, f: &mut Formatter<'_>) -> Result` [src]

Formats the value using the given formatter.

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

#### `fn fmt(&self, f: &mut Formatter<'_>) -> Result` [src]

Formats the value using the given formatter.

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

#### `#[must_use]fn cmp(&self, other: &Self) -> Ordering` [src]

This method returns an `Ordering` between `self` and `other`. Read more

#### `#[must_use]pub fn max(self, other: Self) -> Self` 1.21.0 [src]

Compares and returns the maximum of two values. Read more

#### `#[must_use]pub fn min(self, other: Self) -> Self` 1.21.0 [src]

Compares and returns the minimum of two values. Read more

#### `#[must_use]pub fn clamp(self, min: Self, max: Self) -> Self` 1.50.0 [src]

Restrict a value to a certain interval. Read more

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

#### `#[must_use]fn eq(&self, other: &&[T]) -> bool` [src]

This method tests for `self` and `other` values to be equal, and is used by `==`. Read more

#### `#[must_use]pub fn ne(&self, other: &Rhs) -> bool` 1.0.0 [src]

This method tests for `!=`.

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

#### `#[must_use]fn eq(&self, other: &Self) -> bool` [src]

This method tests for `self` and `other` values to be equal, and is used by `==`. Read more

#### `#[must_use]pub fn ne(&self, other: &Rhs) -> bool` 1.0.0 [src]

This method tests for `!=`.

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

#### `#[must_use]fn partial_cmp(&self, other: &Self) -> Option<Ordering>` [src]

This method returns an ordering between `self` and `other` values if one exists. Read more

#### `#[must_use]pub fn lt(&self, other: &Rhs) -> bool` 1.0.0 [src]

This method tests less than (for `self` and `other`) and is used by the `<` operator. Read more

#### `#[must_use]pub fn le(&self, other: &Rhs) -> bool` 1.0.0 [src]

This method tests less than or equal to (for `self` and `other`) and is used by the `<=` operator. Read more

#### `#[must_use]pub fn gt(&self, other: &Rhs) -> bool` 1.0.0 [src]

This method tests greater than (for `self` and `other`) and is used by the `>` operator. Read more

#### `#[must_use]pub fn ge(&self, other: &Rhs) -> bool` 1.0.0 [src]

This method tests greater than or equal to (for `self` and `other`) and is used by the `>=` operator. Read more

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

#### `fn fmt(&self, f: &mut Formatter<'_>) -> Result` [src]

Formats the value using the given formatter.

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

#### `fn fmt(&self, f: &mut Formatter<'_>) -> Result` [src]

Formats the value using the given formatter.

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

#### `fn fmt(&self, f: &mut Formatter<'_>) -> Result` [src]

Formats the value using the given formatter.

## Auto Trait Implementations

### `impl<'s, T> Send for SliceVec<'s, T> where
T: Send,` [src]

### `impl<'s, T> Sync for SliceVec<'s, T> where
T: Sync,` [src]

### `impl<'s, T> Unpin for SliceVec<'s, T>` [src]

## Blanket Implementations

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

#### `pub fn type_id(&self) -> TypeId` [src]

Gets the `TypeId` of `self`. Read more

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

#### `pub fn borrow(&self) -> &T` [src]

Immutably borrows from an owned value. Read more

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

#### `pub fn borrow_mut(&mut self) -> &mut T` [src]

Mutably borrows from an owned value. Read more

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

#### `pub fn from(t: T) -> T` [src]

Performs the conversion.

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

#### `pub fn into(self) -> U` [src]

Performs the conversion.

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

#### `pub default fn to_string(&self) -> String` [src]

Converts the given value to a `String`. Read more

### `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.

#### `pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>` [src]

Performs the conversion.

### `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.

#### `pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>` [src]

Performs the conversion.