Struct tinyvec::SliceVec [−][src]
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]
T: Default,
#[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]
T: Default,
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]
&'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;
T: Default,
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]
T: Clone,
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]
T: Default,
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]
T: Default,
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]
T: Clone,
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]
T: Default,
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]
T: Default,
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]
T: Default,
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]
impl<'s, T> AsRef<[T]> for SliceVec<'s, T>
[src]
impl<'s, T> Binary for SliceVec<'s, T> where
T: Binary,
[src]
T: Binary,
impl<'s, T> Borrow<[T]> for SliceVec<'s, T>
[src]
impl<'s, T> BorrowMut<[T]> for SliceVec<'s, T>
[src]
#[must_use]fn borrow_mut(&mut self) -> &mut [T]
[src]
impl<'s, T> Debug for SliceVec<'s, T> where
T: Debug,
[src]
T: Debug,
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.
#[must_use]fn deref(&self) -> &Self::Target
[src]
impl<'s, T> DerefMut for SliceVec<'s, T>
[src]
impl<'s, T> Display for SliceVec<'s, T> where
T: Display,
[src]
T: Display,
impl<'s, T> Eq for SliceVec<'s, T> where
T: Eq,
[src]
T: Eq,
impl<'s, T> Extend<T> for SliceVec<'s, T>
[src]
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
[src]
pub fn extend_one(&mut self, item: A)
[src]
pub fn extend_reserve(&mut self, additional: usize)
[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]
A: AsMut<[T]>,
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]
T: Hash,
fn hash<H: Hasher>(&self, state: &mut H)
[src]
pub fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
H: Hasher,
impl<'s, T, I> Index<I> for SliceVec<'s, T> where
I: SliceIndex<[T]>,
[src]
I: SliceIndex<[T]>,
type Output = <I as SliceIndex<[T]>>::Output
The returned type after indexing.
#[must_use]fn index(&self, index: I) -> &Self::Output
[src]
impl<'s, T, I> IndexMut<I> for SliceVec<'s, T> where
I: SliceIndex<[T]>,
[src]
I: SliceIndex<[T]>,
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]
impl<'s, T> LowerExp for SliceVec<'s, T> where
T: LowerExp,
[src]
T: LowerExp,
impl<'s, T> LowerHex for SliceVec<'s, T> where
T: LowerHex,
[src]
T: LowerHex,
impl<'s, T> Octal for SliceVec<'s, T> where
T: Octal,
[src]
T: Octal,
impl<'s, T> Ord for SliceVec<'s, T> where
T: Ord,
[src]
T: Ord,
#[must_use]fn cmp(&self, other: &Self) -> Ordering
[src]
#[must_use]pub fn max(self, other: Self) -> Self
1.21.0[src]
#[must_use]pub fn min(self, other: Self) -> Self
1.21.0[src]
#[must_use]pub fn clamp(self, min: Self, max: Self) -> Self
1.50.0[src]
impl<'s, T> PartialEq<&'_ [T]> for SliceVec<'s, T> where
T: PartialEq,
[src]
T: PartialEq,
#[must_use]fn eq(&self, other: &&[T]) -> bool
[src]
#[must_use]pub fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
impl<'s, T> PartialEq<SliceVec<'s, T>> for SliceVec<'s, T> where
T: PartialEq,
[src]
T: PartialEq,
#[must_use]fn eq(&self, other: &Self) -> bool
[src]
#[must_use]pub fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
impl<'s, T> PartialOrd<SliceVec<'s, T>> for SliceVec<'s, T> where
T: PartialOrd,
[src]
T: PartialOrd,
#[must_use]fn partial_cmp(&self, other: &Self) -> Option<Ordering>
[src]
#[must_use]pub fn lt(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]pub fn le(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]pub fn gt(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]pub fn ge(&self, other: &Rhs) -> bool
1.0.0[src]
impl<'s, T> Pointer for SliceVec<'s, T> where
T: Pointer,
[src]
T: Pointer,
impl<'s, T> UpperExp for SliceVec<'s, T> where
T: UpperExp,
[src]
T: UpperExp,
impl<'s, T> UpperHex for SliceVec<'s, T> where
T: UpperHex,
[src]
T: UpperHex,
Auto Trait Implementations
impl<'s, T> Send for SliceVec<'s, T> where
T: Send,
T: Send,
impl<'s, T> Sync for SliceVec<'s, T> where
T: Sync,
T: Sync,
impl<'s, T> Unpin for SliceVec<'s, T>
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToString for T where
T: Display + ?Sized,
[src]
T: Display + ?Sized,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
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]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,