Struct tinyvec::SliceVec[][src]

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

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

A *mut pointer to the backing slice.

Safety

This pointer has provenance over the entire backing slice.

Performs a deref_mut, into unique slice form.

A *const pointer to the backing slice.

Safety

This pointer has provenance over the entire backing slice.

Performs a deref, into shared slice form.

The capacity of the SliceVec.

This the length of the initial backing slice.

Truncates the SliceVec down to length 0.

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(), &[]);

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

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.

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

Checks if the length is 0.

The length of the SliceVec (in elements).

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

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!

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

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']);

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

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

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.

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

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"]);

Reduces the vec’s length to the given value.

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

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

Performs the conversion.

Performs the conversion.

Formats the value using the given formatter.

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Formats the value using the given formatter. Read more

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

The resulting type after dereferencing.

Dereferences the value.

Mutably dereferences the value.

Formats the value using the given formatter. Read more

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

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

Extends a collection with exactly one element.

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

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

Uses the full slice as the initial length.

Example

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

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

Feeds this value into the given Hasher. Read more

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

The returned type after indexing.

Performs the indexing (container[index]) operation. Read more

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

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

Formats the value using the given formatter.

Formats the value using the given formatter.

Formats the value using the given formatter.

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

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

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

This method tests for !=.

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

This method tests for !=.

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

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

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

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

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

Formats the value using the given formatter.

Formats the value using the given formatter.

Formats the value using the given formatter.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.