Struct NonEmptySmallVec
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§
§impl<const N: usize, T> NonEmptySmallVec<N, T>
impl<const N: usize, T> NonEmptySmallVec<N, T>
pub fn new(e: T) -> NonEmptySmallVec<N, T>
pub fn new(e: T) -> NonEmptySmallVec<N, T>
Alias for NonEmptySmallVec::singleton.
pub fn as_ref(&self) -> NonEmptySmallVec<N, &T>
pub fn as_ref(&self) -> NonEmptySmallVec<N, &T>
Converts from &NonEmptySmallVec<N, T> to NonEmptySmallVec<N, &T>.
pub fn collect<I>(iter: I) -> Option<NonEmptySmallVec<N, T>>where
I: IntoIterator<Item = T>,
pub fn collect<I>(iter: I) -> Option<NonEmptySmallVec<N, T>>where
I: IntoIterator<Item = T>,
Attempt to convert an iterator into a NonEmptySmallVec vector.
Returns None if the iterator was empty.
pub fn singleton(head: T) -> NonEmptySmallVec<N, T>
pub fn singleton(head: T) -> NonEmptySmallVec<N, T>
Create a new non-empty list with an initial element.
pub fn push(&mut self, e: T)
pub fn push(&mut self, e: T)
Push an element to the end of the list.
pub fn insert(&mut self, index: usize, element: T)
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.
pub fn len_nonzero(&self) -> NonZero<usize>
pub fn len_nonzero(&self) -> NonZero<usize>
Gets the length of the list as a NonZeroUsize.
pub fn contains(&self, x: &T) -> boolwhere
T: PartialEq,
pub fn contains(&self, x: &T) -> boolwhere
T: PartialEq,
Check whether an element is contained in the list.
pub fn truncate(&mut self, len: NonZero<usize>)
pub fn truncate(&mut self, len: NonZero<usize>)
Truncate the list to a certain size. Must be greater than 0.
pub fn iter(&self) -> NonEmptySmallVecIter<'_, T> ⓘ
pub fn iter_mut(&mut self) -> impl DoubleEndedIterator
pub fn from_slice(slice: &[T]) -> Option<NonEmptySmallVec<N, T>>where
T: Clone,
pub fn from_slice(slice: &[T]) -> Option<NonEmptySmallVec<N, T>>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.
pub fn from_smallvec(vec: SmallVec<[T; N]>) -> Option<NonEmptySmallVec<N, T>>
pub fn from_smallvec(vec: SmallVec<[T; N]>) -> Option<NonEmptySmallVec<N, T>>
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.
pub fn split_first(&self) -> (&T, &[T])
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.
pub fn split(&self) -> (&T, &[T], Option<&T>)
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.
pub fn append(&mut self, other: &mut SmallVec<[T; N]>)
pub fn append(&mut self, other: &mut SmallVec<[T; N]>)
Append a Vec to the tail of the NonEmptySmallVec.
pub fn map<U, F>(self, f: F) -> NonEmptySmallVec<N, U>where
F: FnMut(T) -> U,
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).
pub fn try_map<E, U, F>(self, f: F) -> Result<NonEmptySmallVec<N, U>, E>
pub fn try_map<E, U, F>(self, f: F) -> Result<NonEmptySmallVec<N, U>, E>
A structure preserving, fallible mapping function.
pub fn flat_map<U, F>(self, f: F) -> NonEmptySmallVec<N, U>where
F: FnMut(T) -> NonEmptySmallVec<N, U>,
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.
pub fn flatten(
full: NonEmptySmallVec<N, NonEmptySmallVec<N, T>>,
) -> NonEmptySmallVec<N, T>
pub fn flatten( full: NonEmptySmallVec<N, NonEmptySmallVec<N, T>>, ) -> NonEmptySmallVec<N, T>
Flatten nested NonEmptySmallVecs into a single one.
pub fn binary_search(&self, x: &T) -> Result<usize, usize>where
T: Ord,
pub fn binary_search(&self, x: &T) -> Result<usize, usize>where
T: Ord,
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.
pub fn binary_search_by<'a, F>(&'a self, f: F) -> Result<usize, usize>
pub fn binary_search_by<'a, F>(&'a self, f: F) -> Result<usize, usize>
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.
pub fn binary_search_by_key<'a, B, F>(
&'a self,
b: &B,
f: F,
) -> Result<usize, usize>
pub fn binary_search_by_key<'a, B, F>( &'a self, b: &B, f: F, ) -> Result<usize, usize>
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.
pub fn maximum(&self) -> &Twhere
T: Ord,
pub fn maximum(&self) -> &Twhere
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.
pub fn minimum(&self) -> &Twhere
T: Ord,
pub fn minimum(&self) -> &Twhere
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.
pub fn maximum_by<F>(&self, compare: F) -> &T
pub fn maximum_by<F>(&self, compare: F) -> &T
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.
pub fn minimum_by<F>(&self, compare: F) -> &T
pub fn minimum_by<F>(&self, compare: F) -> &T
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.
pub fn maximum_by_key<U, F>(&self, f: F) -> &T
pub fn maximum_by_key<U, F>(&self, f: F) -> &T
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.
pub fn minimum_by_key<U, F>(&self, f: F) -> &T
pub fn minimum_by_key<U, F>(&self, f: F) -> &T
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.
pub fn sort(&mut self)where
T: Ord,
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.
pub fn sort_by<F>(&mut self, compare: F)
pub fn sort_by<F>(&mut self, compare: F)
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.
pub fn sort_by_key<K, F>(&mut self, f: F)
pub fn sort_by_key<K, F>(&mut self, f: F)
Sorts the NonEmptySmallVec with a key extraction function.
pub fn sort_by_cached_key<K, F>(&mut self, f: F)
pub fn sort_by_cached_key<K, F>(&mut self, f: F)
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§
§impl<const N: usize, T> Clone for NonEmptySmallVec<N, T>where
T: Clone,
impl<const N: usize, T> Clone for NonEmptySmallVec<N, T>where
T: Clone,
§fn clone(&self) -> NonEmptySmallVec<N, T>
fn clone(&self) -> NonEmptySmallVec<N, T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more§impl<const N: usize, T> Debug for NonEmptySmallVec<N, T>where
T: Debug,
impl<const N: usize, T> Debug for NonEmptySmallVec<N, T>where
T: Debug,
§impl<const N: usize, T> Default for NonEmptySmallVec<N, T>where
T: Default,
impl<const N: usize, T> Default for NonEmptySmallVec<N, T>where
T: Default,
§fn default() -> NonEmptySmallVec<N, T>
fn default() -> NonEmptySmallVec<N, T>
§impl<'de, const N: usize, T> Deserialize<'de> for NonEmptySmallVec<N, T>where
T: Deserialize<'de>,
impl<'de, const N: usize, T> Deserialize<'de> for NonEmptySmallVec<N, T>where
T: Deserialize<'de>,
§fn deserialize<D>(
deserializer: D,
) -> Result<NonEmptySmallVec<N, T>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<NonEmptySmallVec<N, T>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
§impl<const N: usize, A> Extend<A> for NonEmptySmallVec<N, A>
impl<const N: usize, A> Extend<A> for NonEmptySmallVec<N, A>
§fn extend<T>(&mut self, iter: T)where
T: IntoIterator<Item = A>,
fn extend<T>(&mut self, iter: T)where
T: IntoIterator<Item = A>,
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)§impl<const N: usize, T> From<(T, SmallVec<[T; N]>)> for NonEmptySmallVec<N, T>
impl<const N: usize, T> From<(T, SmallVec<[T; N]>)> for NonEmptySmallVec<N, T>
§fn from(_: (T, SmallVec<[T; N]>)) -> NonEmptySmallVec<N, T>
fn from(_: (T, SmallVec<[T; N]>)) -> NonEmptySmallVec<N, T>
Turns a pair of an element and a Vec into a NonEmptySmallVec.
§impl<const N: usize, T> From<NonEmptySmallVec<N, T>> for (T, SmallVec<[T; N]>)
impl<const N: usize, T> From<NonEmptySmallVec<N, T>> for (T, SmallVec<[T; N]>)
§fn from(non_empty_smallvec: NonEmptySmallVec<N, T>) -> (T, SmallVec<[T; N]>)
fn from(non_empty_smallvec: NonEmptySmallVec<N, T>) -> (T, SmallVec<[T; N]>)
Turns a non-empty list into a SmallVec.
§impl<const N: usize, T> Hash for NonEmptySmallVec<N, T>where
T: Hash,
impl<const N: usize, T> Hash for NonEmptySmallVec<N, T>where
T: Hash,
§impl<const N: usize, T> Index<usize> for NonEmptySmallVec<N, T>
impl<const N: usize, T> Index<usize> for NonEmptySmallVec<N, T>
§impl<const N: usize, T> IndexMut<usize> for NonEmptySmallVec<N, T>
impl<const N: usize, T> IndexMut<usize> for NonEmptySmallVec<N, T>
§impl<'a, const N: usize, T> IntoIterator for &'a NonEmptySmallVec<N, T>
impl<'a, const N: usize, T> IntoIterator for &'a NonEmptySmallVec<N, T>
§impl<const N: usize, T> IntoIterator for NonEmptySmallVec<N, T>
impl<const N: usize, T> IntoIterator for NonEmptySmallVec<N, T>
§type IntoIter = Chain<Once<T>, IntoIter<[<NonEmptySmallVec<N, T> as IntoIterator>::Item; N]>>
type IntoIter = Chain<Once<T>, IntoIter<[<NonEmptySmallVec<N, T> as IntoIterator>::Item; N]>>
§fn into_iter(self) -> <NonEmptySmallVec<N, T> as IntoIterator>::IntoIter
fn into_iter(self) -> <NonEmptySmallVec<N, T> as IntoIterator>::IntoIter
§impl<const N: usize, T> Ord for NonEmptySmallVec<N, T>where
T: Ord,
impl<const N: usize, T> Ord for NonEmptySmallVec<N, T>where
T: Ord,
§fn cmp(&self, other: &NonEmptySmallVec<N, T>) -> Ordering
fn cmp(&self, other: &NonEmptySmallVec<N, T>) -> Ordering
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
§impl<const N: usize, T> PartialEq for NonEmptySmallVec<N, T>where
T: PartialEq,
impl<const N: usize, T> PartialEq for NonEmptySmallVec<N, T>where
T: PartialEq,
§impl<const N: usize, T> PartialOrd for NonEmptySmallVec<N, T>where
T: PartialOrd,
impl<const N: usize, T> PartialOrd for NonEmptySmallVec<N, T>where
T: PartialOrd,
§impl<const N: usize, T> Serialize for NonEmptySmallVec<N, T>where
T: Serialize,
impl<const N: usize, T> Serialize for NonEmptySmallVec<N, T>where
T: Serialize,
§fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
§impl<const N: usize, T> TryFrom<SmallVec<[T; N]>> for NonEmptySmallVec<N, T>
impl<const N: usize, T> TryFrom<SmallVec<[T; N]>> for NonEmptySmallVec<N, T>
§type Error = NonEmptySmallVecEmptyError
type Error = NonEmptySmallVecEmptyError
impl<const N: usize, T> Eq for NonEmptySmallVec<N, T>where
T: Eq,
impl<const N: usize, T> StructuralPartialEq for NonEmptySmallVec<N, T>
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> UnwindSafe for NonEmptySmallVec<N, T>where
T: UnwindSafe + RefUnwindSafe,
Blanket Implementations§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
§fn and<P, B, E>(self, other: P) -> And<T, P>
fn and<P, B, E>(self, other: P) -> And<T, P>
Policy that returns Action::Follow only if self and other return
Action::Follow. Read more