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.
The capacity of the SliceVec
.
This the length of the initial backing slice.
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,
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,
impl<'p, 's, T: Default> Iterator for SliceVecDrain<'p, 's, T> type Item = T;
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]);
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!
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
Mutably borrows from an owned value. Read more
Extends a collection with the contents of an iterator. Read more
extend_one
)Extends a collection with exactly one element.
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[..]);
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