Struct minsize::MinSizedVec[][src]

pub struct MinSizedVec<T, const N: usize>(_);
Expand description

A Vec with a minimum size.

This wraps a plain standard library Vec, but inserts checks that it never falls below the required length.

This type derefs to slices in the same way Vec does, so most of the methods you already know are available too.

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

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

Extract the contained Vec, thus removing the length restriction.

Examples

let v: MinSizedVec<_, 3> = MinSizedVec::new(vec![1, 2, 3]).unwrap();
let v: Vec<_> = v.into_inner();
assert_eq!(v, vec![1, 2, 3]);

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

Returns the number of elements the collection can hold without reallocating.

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

Returns a reference to the last element.

Examples

let mut v = MinSizedVec::from([1, 2]);

let last = v.last();
assert_eq!(*last, 2);

Trait Implementations

Performs the conversion.

Performs the conversion.

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Creates a MinSizedVec filled to its minimum capacity with the default value for the contained type.

The resulting type after dereferencing.

Dereferences the value.

Mutably dereferences the value.

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

Performs the conversion.

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

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

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

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

The type returned in the event of a conversion error.

Performs the conversion.

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.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

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

recently added

Uses borrowed data to replace owned data, usually by cloning. 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.