Skip to main content

NonEmptySmallVec

Struct NonEmptySmallVec 

Source
pub struct NonEmptySmallVec<const N: usize, T> {
    pub head: T,
    pub tail: SmallVec<[T; N]>,
}
Expand description

A Non-empty stack vector which can grow to the heap.

See crate::collections::NonEmptyVec for more inforamtion, as it’s identical to it except that we make use of a SmallVec instead of a Vec for tail storage.

Note that the total storage is N+1, as N is the size of the tail, but there’s also the head.

Fields§

§head: T§tail: SmallVec<[T; N]>

Implementations§

Source§

impl<const N: usize, T> NonEmptySmallVec<N, T>

Source

pub fn new(e: T) -> Self

Source

pub fn to_ref(&self) -> NonEmptySmallVec<N, &T>

Converts from &NonEmptySmallVec<N, T> to NonEmptySmallVec<N, &T>, allocating a new tail of borrows. Named to_ (not as_) because it is not a free view.

Source

pub fn collect<I>(iter: I) -> Option<Self>
where I: IntoIterator<Item = T>,

Attempt to convert an iterator into a NonEmptySmallVec vector. Returns None if the iterator was empty.

Source

pub fn singleton(head: T) -> Self

Create a new non-empty list with an initial element.

Source

pub const fn is_empty(&self) -> bool

Always returns false.

Source

pub const fn first(&self) -> &T

Get the first element. Never fails.

Source

pub fn first_mut(&mut self) -> &mut T

Get the mutable reference to the first element. Never fails.

Source

pub fn tail(&self) -> &[T]

Get the possibly-empty tail of the list.

Source

pub fn push(&mut self, e: T)

Push an element to the end of the list.

Source

pub fn pop(&mut self) -> Option<T>

Pop an element from the end of the list.

Source

pub fn insert(&mut self, index: usize, element: T)

Inserts an element at position index within the vector, shifting all elements after it to the right.

§Panics

Panics if index > len.

Source

pub fn len(&self) -> usize

Get the length of the list.

Source

pub fn len_nonzero(&self) -> NonZeroUsize

Gets the length of the list as a NonZeroUsize.

Source

pub fn capacity(&self) -> NonZeroUsize

Get the capacity of the list.

Source

pub fn last(&self) -> &T

Get the last element. Never fails.

Source

pub fn last_mut(&mut self) -> &mut T

Get the last element mutably.

Source

pub fn contains(&self, x: &T) -> bool
where T: PartialEq,

Check whether an element is contained in the list.

Source

pub fn get(&self, index: usize) -> Option<&T>

Get an element by index.

Source

pub fn get_mut(&mut self, index: usize) -> Option<&mut T>

Get an element by index, mutably.

Source

pub fn truncate(&mut self, len: NonZeroUsize)

Truncate the list to a certain size. Must be greater than 0.

Source

pub fn iter(&self) -> NonEmptySmallVecIter<'_, T>

Source

pub fn iter_mut(&mut self) -> impl DoubleEndedIterator<Item = &mut T> + '_

Source

pub fn from_slice(slice: &[T]) -> Option<Self>
where T: Clone,

Often we have a Vec (or slice &[T]) but want to ensure that it is NonEmptySmallVec before proceeding with a computation. Using from_slice will give us a proof that we have a NonEmptySmallVec in the Some branch, otherwise it allows the caller to handle the None case.

Source

pub fn from_smallvec(vec: SmallVec<[T; N]>) -> Option<Self>

Often we have a Vec (or slice &[T]) but want to ensure that it is NonEmptySmallVec before proceeding with a computation. Using from_smallvec will give us a proof that we have a NonEmptySmallVec in the Some branch, otherwise it allows the caller to handle the None case.

This version will consume the Vec you pass in. If you would rather pass the data as a slice then use NonEmptySmallVec::from_slice.

Source

pub fn split_first(&self) -> (&T, &[T])

Deconstruct a NonEmptySmallVec into its head and tail. This operation never fails since we are guaranteed to have a head element.

Source

pub fn split(&self) -> (&T, &[T], Option<&T>)

Deconstruct a NonEmptySmallVec into its first, last, and middle elements, in that order.

If there is only one element then last is None.

Source

pub fn append(&mut self, other: &mut SmallVec<[T; N]>)

Append a Vec to the tail of the NonEmptySmallVec.

Source

pub fn map<U, F>(self, f: F) -> NonEmptySmallVec<N, U>
where F: FnMut(T) -> U,

A structure preserving map. This is useful for when we wish to keep the NonEmptySmallVec structure guaranteeing that there is at least one element. Otherwise, we can use non_empty_smallvec.iter().map(f).

Source

pub fn try_map<E, U, F>(self, f: F) -> Result<NonEmptySmallVec<N, U>, E>
where F: FnMut(T) -> Result<U, E>,

A structure preserving, fallible mapping function.

Source

pub fn flat_map<U, F>(self, f: F) -> NonEmptySmallVec<N, U>
where F: FnMut(T) -> NonEmptySmallVec<N, U>,

When we have a function that goes from some T to a NonEmptySmallVec<U>, we may want to apply it to a NonEmptySmallVec<T> but keep the structure flat. This is where flat_map shines.

Source

pub fn flatten(full: NonEmptySmallVec<N, Self>) -> Self

Flatten nested NonEmptySmallVecs into a single one.

Binary searches this sorted non-empty vector for a given element.

If the value is found then Result::Ok is returned, containing the index of the matching element. If there are multiple matches, then any one of the matches could be returned.

If the value is not found then Result::Err is returned, containing the index where a matching element could be inserted while maintaining sorted order.

Source

pub fn binary_search_by<'a, F>(&'a self, f: F) -> Result<usize, usize>
where F: FnMut(&'a T) -> Ordering,

Binary searches this sorted non-empty with a comparator function.

The comparator function should implement an order consistent with the sort order of the underlying slice, returning an order code that indicates whether its argument is Less, Equal or Greater the desired target.

If the value is found then Result::Ok is returned, containing the index of the matching element. If there are multiple matches, then any one of the matches could be returned. If the value is not found then Result::Err is returned, containing the index where a matching element could be inserted while maintaining sorted order.

Source

pub fn binary_search_by_key<'a, B, F>( &'a self, b: &B, f: F, ) -> Result<usize, usize>
where B: Ord, F: FnMut(&'a T) -> B,

Binary searches this sorted non-empty vector with a key extraction function.

Assumes that the vector is sorted by the key.

If the value is found then Result::Ok is returned, containing the index of the matching element. If there are multiple matches, then any one of the matches could be returned. If the value is not found then Result::Err is returned, containing the index where a matching element could be inserted while maintaining sorted order.

Source

pub fn maximum(&self) -> &T
where T: Ord,

Returns the maximum element in the non-empty vector.

This will return the first item in the vector if the tail is empty.

Source

pub fn minimum(&self) -> &T
where T: Ord,

Returns the minimum element in the non-empty vector.

This will return the first item in the vector if the tail is empty.

Source

pub fn maximum_by<F>(&self, compare: F) -> &T
where F: FnMut(&T, &T) -> Ordering,

Returns the element that gives the maximum value with respect to the specified comparison function.

This will return the first item in the vector if the tail is empty.

Source

pub fn minimum_by<F>(&self, compare: F) -> &T
where F: FnMut(&T, &T) -> Ordering,

Returns the element that gives the minimum value with respect to the specified comparison function.

This will return the first item in the vector if the tail is empty.

Source

pub fn maximum_by_key<U, F>(&self, f: F) -> &T
where U: Ord, F: FnMut(&T) -> U,

Returns the element that gives the maximum value with respect to the specified function.

This will return the first item in the vector if the tail is empty.

Source

pub fn minimum_by_key<U, F>(&self, f: F) -> &T
where U: Ord, F: FnMut(&T) -> U,

Returns the element that gives the minimum value with respect to the specified function.

This will return the first item in the vector if the tail is empty.

Source

pub fn sort(&mut self)
where T: Ord,

Sorts the NonEmptySmallVec.

The implementation uses slice::sort for the tail and then checks where the head belongs. If the head is already the smallest element, this should be as fast as sorting a slice. However, if the head needs to be inserted, then it incurs extra cost for removing the new head from the tail and adding the old head at the correct index.

Source

pub fn sort_by<F>(&mut self, compare: F)
where F: FnMut(&T, &T) -> Ordering,

Sorts the NonEmptySmallVec with a comparator function.

The implementation uses slice::sort_by for the tail and then checks where the head belongs. If the head is already the smallest element, this should be as fast as sorting a slice. However, if the head needs to be inserted, then it incurs extra cost for removing the new head from the tail and adding the old head at the correct index.

Source

pub fn sort_by_key<K, F>(&mut self, f: F)
where F: FnMut(&T) -> K, K: Ord,

Sorts the NonEmptySmallVec with a key extraction function.

Source

pub fn sort_by_cached_key<K, F>(&mut self, f: F)
where F: FnMut(&T) -> K, K: Ord,

Sorts the NonEmptySmallVec with a key extraction function, caching the keys.

The implementation uses slice::sort_by_cached_key for the tail and then determines where the head belongs using the cached head key.

Trait Implementations§

Source§

impl<const N: usize, T: Clone> Clone for NonEmptySmallVec<N, T>

Source§

fn clone(&self) -> NonEmptySmallVec<N, T>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<const N: usize, T: Debug> Debug for NonEmptySmallVec<N, T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<const N: usize, T: Default> Default for NonEmptySmallVec<N, T>

Source§

fn default() -> Self

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

impl<'de, const N: usize, T: Deserialize<'de>> Deserialize<'de> for NonEmptySmallVec<N, T>

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<const N: usize, T: Eq> Eq for NonEmptySmallVec<N, T>

Source§

impl<const N: usize, A> Extend<A> for NonEmptySmallVec<N, A>

Source§

fn extend<T: IntoIterator<Item = A>>(&mut self, iter: T)

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

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<const N: usize, T> From<(T, SmallVec<[T; N]>)> for NonEmptySmallVec<N, T>

Source§

fn from((head, tail): (T, SmallVec<[T; N]>)) -> Self

Turns a pair of an element and a Vec into a NonEmptySmallVec.

Source§

impl<const N: usize, T> From<NonEmptySmallVec<N, T>> for SmallVec<[T; N]>

Source§

fn from(non_empty_smallvec: NonEmptySmallVec<N, T>) -> Self

Turns a non-empty list into a Vec.

Source§

impl<const N: usize, T> From<NonEmptySmallVec<N, T>> for (T, SmallVec<[T; N]>)

Source§

fn from(non_empty_smallvec: NonEmptySmallVec<N, T>) -> (T, SmallVec<[T; N]>)

Turns a non-empty list into a SmallVec.

Source§

impl<const N: usize, T: Hash> Hash for NonEmptySmallVec<N, T>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

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

impl<const N: usize, T> Index<usize> for NonEmptySmallVec<N, T>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &T

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

impl<const N: usize, T> IndexMut<usize> for NonEmptySmallVec<N, T>

Source§

fn index_mut(&mut self, index: usize) -> &mut T

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

impl<const N: usize, T> IntoIterator for NonEmptySmallVec<N, T>

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = Chain<Once<T>, IntoIter<[<NonEmptySmallVec<N, T> as IntoIterator>::Item; N]>>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, const N: usize, T> IntoIterator for &'a NonEmptySmallVec<N, T>

Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = Chain<Once<&'a T>, Iter<'a, T>>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<const N: usize, T: Ord> Ord for NonEmptySmallVec<N, T>

Source§

fn cmp(&self, other: &NonEmptySmallVec<N, T>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<const N: usize, T: PartialEq> PartialEq for NonEmptySmallVec<N, T>

Source§

fn eq(&self, other: &NonEmptySmallVec<N, T>) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<const N: usize, T: PartialOrd> PartialOrd for NonEmptySmallVec<N, T>

Source§

fn partial_cmp(&self, other: &NonEmptySmallVec<N, T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<const N: usize, T: Serialize> Serialize for NonEmptySmallVec<N, T>

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<const N: usize, T: PartialEq> StructuralPartialEq for NonEmptySmallVec<N, T>

Source§

impl<const N: usize, T> TryFrom<SmallVec<[T; N]>> for NonEmptySmallVec<N, T>

Source§

type Error = NonEmptySmallVecEmptyError

The type returned in the event of a conversion error.
Source§

fn try_from(vec: SmallVec<[T; N]>) -> Result<Self, Self::Error>

Performs the conversion.

Auto Trait Implementations§

§

impl<const N: usize, T> Freeze for NonEmptySmallVec<N, T>
where T: Freeze,

§

impl<const N: usize, T> RefUnwindSafe for NonEmptySmallVec<N, T>
where T: RefUnwindSafe,

§

impl<const N: usize, T> Send for NonEmptySmallVec<N, T>
where T: Send,

§

impl<const N: usize, T> Sync for NonEmptySmallVec<N, T>
where T: Sync,

§

impl<const N: usize, T> Unpin for NonEmptySmallVec<N, T>
where T: Unpin,

§

impl<const N: usize, T> UnsafeUnpin for NonEmptySmallVec<N, T>
where T: UnsafeUnpin,

§

impl<const N: usize, T> UnwindSafe for NonEmptySmallVec<N, T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more