Struct minsize::MinSizedVec [−][src]
pub struct MinSizedVec<T, const N: usize>(_);
Expand description
Implementations
The minimum size of this collection.
Create a MinSizedVec from a plain Vec.
This checks the length, handing you back the input if it is less than the required length.
This is equivalent to the TryFrom<Vec<T>> impl.
Examples
let min_size: MinSizedVec<_, 3> = vec![1, 2, 3].try_into().unwrap(); // Too short input: let result: Result<MinSizedVec<_, 2>, _> = vec![1].try_into(); assert_eq!(result, Err(vec![1]));
pub fn from_iterator<I>(
iterator: I
) -> Result<MinSizedVec<T, MINIMUM_SIZE>, Vec<T>> where
I: IntoIterator<Item = T>,
pub fn from_iterator<I>(
iterator: I
) -> Result<MinSizedVec<T, MINIMUM_SIZE>, Vec<T>> where
I: IntoIterator<Item = T>,
Create a MinSizedVec from an iterator.
This collects the given iterator into a plain Vec and then checks the length, returning
either a MinSizedVec, or the raw collected Vec if it is too short.
Examples
let v = MinSizedVec::<_, 2>::from_iterator(vec![1, 2, 3]).unwrap(); assert_eq!(v.len(), 3); let v = MinSizedVec::<_, 2>::from_iterator(vec![1]); assert_eq!(v, Err(vec![1]));
Append a value.
Examples
let mut v: MinSizedVec<_, 2> = MinSizedVec::new(vec![1, 2]).unwrap(); v.push(3); assert_eq!(v, &[1, 2, 3]);
Remove a value from the end of the collection.
This will return None when the minimum length is reached.
Examples
let mut v: MinSizedVec<_, 2> = MinSizedVec::new(vec![1, 2, 3]).unwrap(); assert_eq!(v.pop(), Some(3)); assert_eq!(v.pop(), None); assert_eq!(v, &[1, 2]);
Clear the collection to the minimum size required.
This drops any elements after the index required for the minimum size.
Examples
let mut v: MinSizedVec<_, 2> = MinSizedVec::new(vec![1, 2, 3]).unwrap(); v.clear_to_minimum(); assert_eq!(v, &[1, 2]);
Shorten the collection, keeping the first len elements.
If len is greater than the current length, this has no effect. If len is smaller than
the minimum size, the collection is shortened to the minimum size.
Examples
let mut v: MinSizedVec<_, 2> = MinSizedVec::new(vec![1, 2, 3, 4]).unwrap(); v.truncate(3); assert_eq!(v, &[1, 2, 3]); // The length is still capped to the minimum size: v.truncate(0); assert_eq!(v, &[1, 2]);
Clones and appends all elements in other to the collection.
Examples
let mut v: MinSizedVec<_, 2> = MinSizedVec::new(vec![1, 2]).unwrap(); v.extend_from_slice(&[3, 4]); assert_eq!(v, &[1, 2, 3, 4]);
Reserves capacity for at least additional more elements to be inserted.
See Vec::reserve for details.
Reserves the minimum capacity for exactly additional more elements to be inserted.
See Vec::reserve_exact for details.
Resizes the collection so that the length equals new_len.
If the current length is less than new_len, clones of value are appended.
If the current length is larger than new_len, the collection is truncated to new_len or
the minimum size, whichever is larger.
Examples
let mut v: MinSizedVec<_, 2> = MinSizedVec::new(vec![1, 2, 3]).unwrap(); v.resize(5, 4); assert_eq!(v, &[1, 2, 3, 4, 4]); v.resize(3, 4); assert_eq!(v, &[1, 2, 3]); v.resize(0, 4); assert_eq!(v, &[1, 2]);
Resizes the collection so that the length equals new_len.
If the current length is less than new_len, the collection is extended by the values
returned from calling the f closure.
If the current length is larger than new_len, the collection is truncated to new_len or
the minimum size, whichever is larger.
Examples
let mut v: MinSizedVec<_, 2> = MinSizedVec::new(vec![1, 2, 3]).unwrap(); let mut start = v.len(); v.resize_with(5, || { start += 1; start }); assert_eq!(v, &[1, 2, 3, 4, 5]); v.resize_with(3, || 4); assert_eq!(v, &[1, 2, 3]); v.resize_with(0, || 4); assert_eq!(v, &[1, 2]);
Insert the given element at the given index, shifting all elements after it to the right.
Panics
Panics if the given index is greater than the length of the collection.
Examples
let mut v = MinSizedVec::from([1, 2]); v.insert(2, 3); assert_eq!(v, &[1, 2, 3]); v.insert(2, 4); assert_eq!(v, &[1, 2, 4, 3]);
Remove and and return the element at the given index from the collection.
This will shift all elements after index to the left.
Panics
This will panic if index is out of bounds, or if removing the element would put the
collection below the minimum size.
Examples
let mut v: MinSizedVec<_, 2> = MinSizedVec::new(vec![1, 2, 3]).unwrap(); assert_eq!(v.remove(1), 2); assert_eq!(v, &[1, 3]);
let mut v: MinSizedVec<_, 2> = MinSizedVec::new(vec![1, 2]).unwrap(); // This will panic, since the minimum size is 2: v.remove(1);
Remove and and return the element at the given index from the collection.
This will replace the empty slot left by the removal with the last element in the collection. This does not preserve ordering, but is O(1).
Panics
This will panic if index is out of bounds, or if removing the element would put the
collection below the minimum size.
Examples
let mut v: MinSizedVec<_, 2> = MinSizedVec::new(vec![1, 2, 3]).unwrap(); assert_eq!(v.swap_remove(0), 1); assert_eq!(v, &[3, 2]);
let mut v: MinSizedVec<_, 2> = MinSizedVec::new(vec![1, 2]).unwrap(); // This will panic, since the minimum size is 2: v.swap_remove(1);
Retain only the elements specified by the predicate, capped to the minimum size.
This will remove all elements for which f returns false from the collection, until the
minimum size has been reached. The closure will be called with each element starting from
the beginning, but will stop being called after the maximum removable number of elements
have been removed.
Examples
let mut v: MinSizedVec<_, 2> = MinSizedVec::new(vec![1, 2, 3, 4, 5]).unwrap(); v.capped_retain(|&e| e % 2 == 0); assert_eq!(v, &[2, 4]);
The length remains capped, and will stop removing elements once it is reached:
let mut v: MinSizedVec<_, 2> = MinSizedVec::new(vec![1, 2, 3, 4, 5]).unwrap(); v.capped_retain(|_| false); // try to remove everything assert_eq!(v, &[4, 5]); // the last two elements remain
Retain only the elements specified by the predicate.
This will remove all elements for which f returns false from the collection. The
closure will be called with each element starting from the beginning.
If the length after the removal operations is still greater than the minimum size, the
collection is returned in the Ok() variant. If it is less than the minimum size, the
collection is degraded to a plain Vec and return in the Err(_) variant.
Examples
let mut v: MinSizedVec<_, 2> = MinSizedVec::new(vec![1, 2, 3, 4, 5]).unwrap(); let v = v.try_retain(|&e| e % 2 == 0); assert_eq!(v, Ok(MinSizedVec::from([2, 4])));
The length remains capped, and will stop removing elements once it is reached:
let mut v: MinSizedVec<_, 2> = MinSizedVec::new(vec![1, 2, 3, 4, 5]).unwrap(); let v = v.try_retain(|_| false); // try to remove everything assert_eq!(v, Err(vec![])); // a plain empty Vec remains
Try to change the mininum size of the collection to NEW.
This will inspect the current runtime length of the collection. If it is greater than
or equal to NEW, a successful Result containing a collection with the updated minimum
size is returned. If it is smaller, the original collection is returned in an Err
variant.
Examples
let v: MinSizedVec<_, 2> = MinSizedVec::new(vec![1, 2, 3, 4, 5]).unwrap(); assert!(v.clone().change_minimum_size::<5>().is_ok()); assert!(v.clone().change_minimum_size::<6>().is_err());
impl<T, const MINIMUM_SIZE: usize> MinSizedVec<T, MINIMUM_SIZE> where
MinSizedVec<T, MINIMUM_SIZE>: NotEmpty,
impl<T, const MINIMUM_SIZE: usize> MinSizedVec<T, MINIMUM_SIZE> where
MinSizedVec<T, MINIMUM_SIZE>: NotEmpty,
NOTE: This impl block contains methods that are available if the minimum size is greater than 0.
Returns a mutable reference to the first element.
Examples
let mut v = MinSizedVec::from([1, 2]); let first = v.first_mut(); assert_eq!(*first, 1); *first = 3; assert_eq!(v, &[3, 2]);
Returns a mutable reference to the last element.
Examples
let mut v = MinSizedVec::from([1, 2]); let last = v.last_mut(); assert_eq!(*last, 2); *last = 3; assert_eq!(v, &[1, 3]);
Returns a reference to the first element.
Examples
let mut v = MinSizedVec::from([1, 2]); let first = v.first(); assert_eq!(*first, 1);
Trait Implementations
Creates a MinSizedVec filled to its minimum capacity with the default value for the
contained type.
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
impl<T, U, const A: usize, const B: usize> PartialEq<MinSizedVec<U, B>> for MinSizedVec<T, A> where
T: PartialEq<U>,
impl<T, U, const A: usize, const B: usize> PartialEq<MinSizedVec<U, B>> for MinSizedVec<T, A> where
T: PartialEq<U>,
impl<T, const A: usize, const B: usize> PartialOrd<MinSizedVec<T, B>> for MinSizedVec<T, A> where
T: PartialOrd,
impl<T, const A: usize, const B: usize> PartialOrd<MinSizedVec<T, B>> for MinSizedVec<T, A> where
T: PartialOrd,
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
Auto Trait Implementations
impl<T, const N: usize> RefUnwindSafe for MinSizedVec<T, N> where
T: RefUnwindSafe,
impl<T, const N: usize> Send for MinSizedVec<T, N> where
T: Send,
impl<T, const N: usize> Sync for MinSizedVec<T, N> where
T: Sync,
impl<T, const N: usize> Unpin for MinSizedVec<T, N> where
T: Unpin,
impl<T, const N: usize> UnwindSafe for MinSizedVec<T, N> where
T: UnwindSafe,
Blanket Implementations
Mutably borrows from an owned value. Read more