Struct NonEmptyVec
pub struct NonEmptyVec<T> {
pub head: T,
pub tail: Vec<T>,
}Expand description
A Non-empty growable vector.
Non-emptiness can be a powerful guarantee. If your main use of Vec is as
an Iterator, then you may not need to distinguish on emptiness. But there
are indeed times when the Vec you receive as as function argument needs to
be non-empty or your function can’t proceed. Similarly, there are times when
the Vec you return to a calling user needs to promise it actually contains
something.
With NonEmptyVec, you’re freed from the boilerplate of constantly needing to
check is_empty() or pattern matching before proceeding, or erroring if you
can’t. So overall, code, type signatures, and logic become cleaner.
Consider that unlike Vec, NonEmptyVec::first and NonEmptyVec::last don’t
return in Option, they always succeed.
§Examples
The simplest way to construct a NonEmptyVec is via the non_empty_vec macro:
use rama_utils::collections::{NonEmptyVec, non_empty_vec};
let l: NonEmptyVec<u32> = non_empty_vec![1, 2, 3];
assert_eq!(l.head, 1);Unlike the familiar vec! macro, non_empty_vec! requires at least one element:
use rama_utils::collections::non_empty_vec;
let l = non_empty_vec![1];
// Doesn't compile!
// let l = non_empty_vec![];Like Vec, you can also construct a NonEmptyVec
the old fashioned way with NonEmptyVec::new.
§Caveats
Since NonEmptyVec must have a least one element, it is not possible to
implement the FromIterator trait for it. We can’t know, in general, if
any given Iterator actually contains something.
Fields§
§head: T§tail: Vec<T>Implementations§
§impl<T> NonEmptyVec<T>
impl<T> NonEmptyVec<T>
pub const fn new(e: T) -> NonEmptyVec<T>
Available on crate features grpc and http and std only.
pub const fn new(e: T) -> NonEmptyVec<T>
grpc and http and std only.Alias for NonEmptyVec::singleton.
pub fn to_ref(&self) -> NonEmptyVec<&T>
Available on crate features grpc and http and std only.
pub fn to_ref(&self) -> NonEmptyVec<&T>
grpc and http and std only.Converts from &NonEmptyVec<T> to NonEmptyVec<&T>, allocating a new
tail of borrows. Named to_ (not as_) because it is not a free view.
pub fn collect<I>(iter: I) -> Option<NonEmptyVec<T>>where
I: IntoIterator<Item = T>,
Available on crate features grpc and http and std only.
pub fn collect<I>(iter: I) -> Option<NonEmptyVec<T>>where
I: IntoIterator<Item = T>,
grpc and http and std only.Attempt to convert an iterator into a NonEmptyVec vector.
Returns None if the iterator was empty.
pub const fn singleton(head: T) -> NonEmptyVec<T>
Available on crate features grpc and http and std only.
pub const fn singleton(head: T) -> NonEmptyVec<T>
grpc and http and std only.Create a new non-empty list with an initial element.
pub const fn is_empty(&self) -> bool
Available on crate features grpc and http and std only.
pub const fn is_empty(&self) -> bool
grpc and http and std only.Always returns false.
pub const fn first(&self) -> &T
Available on crate features grpc and http and std only.
pub const fn first(&self) -> &T
grpc and http and std only.Get the first element. Never fails.
pub fn first_mut(&mut self) -> &mut T
Available on crate features grpc and http and std only.
pub fn first_mut(&mut self) -> &mut T
grpc and http and std only.Get the mutable reference to the first element. Never fails.
§Examples
use rama_utils::collections::NonEmptyVec;
let mut non_empty = NonEmptyVec::new(42);
let head = non_empty.first_mut();
*head += 1;
assert_eq!(non_empty.first(), &43);
let mut non_empty = NonEmptyVec::from((1, vec![4, 2, 3]));
let head = non_empty.first_mut();
*head *= 42;
assert_eq!(non_empty.first(), &42);pub fn tail(&self) -> &[T]
Available on crate features grpc and http and std only.
pub fn tail(&self) -> &[T]
grpc and http and std only.Get the possibly-empty tail of the list.
use rama_utils::collections::NonEmptyVec;
let non_empty = NonEmptyVec::new(42);
assert_eq!(non_empty.tail(), &[]);
let non_empty = NonEmptyVec::from((1, vec![4, 2, 3]));
assert_eq!(non_empty.tail(), &[4, 2, 3]);pub fn push(&mut self, e: T)
Available on crate features grpc and http and std only.
pub fn push(&mut self, e: T)
grpc and http and std only.Push an element to the end of the list.
pub fn pop(&mut self) -> Option<T>
Available on crate features grpc and http and std only.
pub fn pop(&mut self) -> Option<T>
grpc and http and std only.Pop an element from the end of the list.
pub fn insert(&mut self, index: usize, element: T)
Available on crate features grpc and http and std only.
pub fn insert(&mut self, index: usize, element: T)
grpc and http and std only.Inserts an element at position index within the vector, shifting all elements after it to the right.
§Panics
Panics if index > len.
§Examples
use rama_utils::collections::NonEmptyVec;
let mut non_empty = NonEmptyVec::from((1, vec![2, 3]));
non_empty.insert(1, 4);
assert_eq!(non_empty, NonEmptyVec::from((1, vec![4, 2, 3])));
non_empty.insert(4, 5);
assert_eq!(non_empty, NonEmptyVec::from((1, vec![4, 2, 3, 5])));
non_empty.insert(0, 42);
assert_eq!(non_empty, NonEmptyVec::from((42, vec![1, 4, 2, 3, 5])));pub fn len(&self) -> usize
Available on crate features grpc and http and std only.
pub fn len(&self) -> usize
grpc and http and std only.Get the length of the list.
pub fn len_nonzero(&self) -> NonZero<usize>
Available on crate features grpc and http and std only.
pub fn len_nonzero(&self) -> NonZero<usize>
grpc and http and std only.Gets the length of the list as a NonZeroUsize.
pub fn capacity(&self) -> NonZero<usize>
Available on crate features grpc and http and std only.
pub fn capacity(&self) -> NonZero<usize>
grpc and http and std only.Get the capacity of the list.
pub fn last(&self) -> &T
Available on crate features grpc and http and std only.
pub fn last(&self) -> &T
grpc and http and std only.Get the last element. Never fails.
pub fn last_mut(&mut self) -> &mut T
Available on crate features grpc and http and std only.
pub fn last_mut(&mut self) -> &mut T
grpc and http and std only.Get the last element mutably.
pub fn contains(&self, x: &T) -> boolwhere
T: PartialEq,
Available on crate features grpc and http and std only.
pub fn contains(&self, x: &T) -> boolwhere
T: PartialEq,
grpc and http and std only.Check whether an element is contained in the list.
use rama_utils::collections::NonEmptyVec;
let mut l = NonEmptyVec::from((42, vec![36, 58]));
assert!(l.contains(&42));
assert!(!l.contains(&101));pub fn get(&self, index: usize) -> Option<&T>
Available on crate features grpc and http and std only.
pub fn get(&self, index: usize) -> Option<&T>
grpc and http and std only.Get an element by index.
pub fn get_mut(&mut self, index: usize) -> Option<&mut T>
Available on crate features grpc and http and std only.
pub fn get_mut(&mut self, index: usize) -> Option<&mut T>
grpc and http and std only.Get an element by index, mutably.
pub fn truncate(&mut self, len: NonZero<usize>)
Available on crate features grpc and http and std only.
pub fn truncate(&mut self, len: NonZero<usize>)
grpc and http and std only.Truncate the list to a certain size. Must be greater than 0.
pub fn iter(&self) -> NonEmptyVecIter<'_, T> ⓘ
Available on crate features grpc and http and std only.
pub fn iter(&self) -> NonEmptyVecIter<'_, T> ⓘ
grpc and http and std only.use rama_utils::collections::NonEmptyVec;
let mut l = NonEmptyVec::from((42, vec![36, 58]));
let mut l_iter = l.iter();
assert_eq!(l_iter.len(), 3);
assert_eq!(l_iter.next(), Some(&42));
assert_eq!(l_iter.next(), Some(&36));
assert_eq!(l_iter.next(), Some(&58));
assert_eq!(l_iter.next(), None);pub fn iter_mut(&mut self) -> impl DoubleEndedIterator
Available on crate features grpc and http and std only.
pub fn iter_mut(&mut self) -> impl DoubleEndedIterator
grpc and http and std only.use rama_utils::collections::NonEmptyVec;
let mut l = NonEmptyVec::new(42);
l.push(36);
l.push(58);
for i in l.iter_mut() {
*i *= 10;
}
let mut l_iter = l.iter();
assert_eq!(l_iter.next(), Some(&420));
assert_eq!(l_iter.next(), Some(&360));
assert_eq!(l_iter.next(), Some(&580));
assert_eq!(l_iter.next(), None);pub fn from_slice(slice: &[T]) -> Option<NonEmptyVec<T>>where
T: Clone,
Available on crate features grpc and http and std only.
pub fn from_slice(slice: &[T]) -> Option<NonEmptyVec<T>>where
T: Clone,
grpc and http and std only.Often we have a Vec (or slice &[T]) but want to ensure that it is NonEmptyVec before
proceeding with a computation. Using from_slice will give us a proof
that we have a NonEmptyVec in the Some branch, otherwise it allows
the caller to handle the None case.
§Example Use
use rama_utils::collections::NonEmptyVec;
let non_empty_vec = NonEmptyVec::from_slice(&[1, 2, 3, 4, 5]);
assert_eq!(non_empty_vec, Some(NonEmptyVec::from((1, vec![2, 3, 4, 5]))));
let empty_vec: Option<NonEmptyVec<&u32>> = NonEmptyVec::from_slice(&[]);
assert!(empty_vec.is_none());pub fn from_vec(vec: Vec<T>) -> Option<NonEmptyVec<T>>
Available on crate features grpc and http and std only.
pub fn from_vec(vec: Vec<T>) -> Option<NonEmptyVec<T>>
grpc and http and std only.Often we have a Vec (or slice &[T]) but want to ensure that it is NonEmptyVec before
proceeding with a computation. Using from_vec will give us a proof
that we have a NonEmptyVec 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 NonEmptyVec::from_slice.
§Example Use
use rama_utils::collections::NonEmptyVec;
let non_empty_vec = NonEmptyVec::from_vec(vec![1, 2, 3, 4, 5]);
assert_eq!(non_empty_vec, Some(NonEmptyVec::from((1, vec![2, 3, 4, 5]))));
let empty_vec: Option<NonEmptyVec<&u32>> = NonEmptyVec::from_vec(vec![]);
assert!(empty_vec.is_none());pub fn split_first(&self) -> (&T, &[T])
Available on crate features grpc and http and std only.
pub fn split_first(&self) -> (&T, &[T])
grpc and http and std only.Deconstruct a NonEmptyVec into its head and tail.
This operation never fails since we are guaranteed
to have a head element.
§Example Use
use rama_utils::collections::NonEmptyVec;
let mut non_empty = NonEmptyVec::from((1, vec![2, 3, 4, 5]));
// Guaranteed to have the head and we also get the tail.
assert_eq!(non_empty.split_first(), (&1, &[2, 3, 4, 5][..]));
let non_empty = NonEmptyVec::new(1);
// Guaranteed to have the head element.
assert_eq!(non_empty.split_first(), (&1, &[][..]));pub fn split(&self) -> (&T, &[T], Option<&T>)
Available on crate features grpc and http and std only.
pub fn split(&self) -> (&T, &[T], Option<&T>)
grpc and http and std only.Deconstruct a NonEmptyVec into its first, last, and
middle elements, in that order.
If there is only one element then last is None.
§Example Use
use rama_utils::collections::NonEmptyVec;
let mut non_empty = NonEmptyVec::from((1, vec![2, 3, 4, 5]));
// When there are two or more elements, the last element is represented
// as a `Some`. Elements preceding it, except for the first, are returned
// in the middle.
assert_eq!(non_empty.split(), (&1, &[2, 3, 4][..], Some(&5)));
let non_empty = NonEmptyVec::new(1);
// The last element is `None` when there's only one element.
assert_eq!(non_empty.split(), (&1, &[][..], None));pub fn append(&mut self, other: &mut Vec<T>)
Available on crate features grpc and http and std only.
pub fn append(&mut self, other: &mut Vec<T>)
grpc and http and std only.Append a Vec to the tail of the NonEmptyVec.
§Example Use
use rama_utils::collections::NonEmptyVec;
let mut non_empty = NonEmptyVec::new(1);
let mut vec = vec![2, 3, 4, 5];
non_empty.append(&mut vec);
let mut expected = NonEmptyVec::from((1, vec![2, 3, 4, 5]));
assert_eq!(non_empty, expected);pub fn map<U, F>(self, f: F) -> NonEmptyVec<U>where
F: FnMut(T) -> U,
Available on crate features grpc and http and std only.
pub fn map<U, F>(self, f: F) -> NonEmptyVec<U>where
F: FnMut(T) -> U,
grpc and http and std only.A structure preserving map. This is useful for when
we wish to keep the NonEmptyVec structure guaranteeing
that there is at least one element. Otherwise, we can
use non_empty_vec.iter().map(f).
§Examples
use rama_utils::collections::NonEmptyVec;
let non_empty = NonEmptyVec::from((1, vec![2, 3, 4, 5]));
let squares = non_empty.map(|i| i * i);
let expected = NonEmptyVec::from((1, vec![4, 9, 16, 25]));
assert_eq!(squares, expected);pub fn try_map<E, U, F>(self, f: F) -> Result<NonEmptyVec<U>, E>
Available on crate features grpc and http and std only.
pub fn try_map<E, U, F>(self, f: F) -> Result<NonEmptyVec<U>, E>
grpc and http and std only.A structure preserving, fallible mapping function.
pub fn flat_map<U, F>(self, f: F) -> NonEmptyVec<U>where
F: FnMut(T) -> NonEmptyVec<U>,
Available on crate features grpc and http and std only.
pub fn flat_map<U, F>(self, f: F) -> NonEmptyVec<U>where
F: FnMut(T) -> NonEmptyVec<U>,
grpc and http and std only.When we have a function that goes from some T to a NonEmptyVec<U>,
we may want to apply it to a NonEmptyVec<T> but keep the structure flat.
This is where flat_map shines.
§Examples
use rama_utils::collections::NonEmptyVec;
let non_empty = NonEmptyVec::from((1, vec![2, 3, 4, 5]));
let windows = non_empty.flat_map(|i| {
let mut next = NonEmptyVec::new(i + 5);
next.push(i + 6);
next
});
let expected = NonEmptyVec::from((6, vec![7, 7, 8, 8, 9, 9, 10, 10, 11]));
assert_eq!(windows, expected);pub fn flatten(full: NonEmptyVec<NonEmptyVec<T>>) -> NonEmptyVec<T>
Available on crate features grpc and http and std only.
pub fn flatten(full: NonEmptyVec<NonEmptyVec<T>>) -> NonEmptyVec<T>
grpc and http and std only.Flatten nested NonEmptyVecs into a single one.
§Examples
use rama_utils::collections::NonEmptyVec;
let non_empty = NonEmptyVec::from((
NonEmptyVec::from((1, vec![2, 3])),
vec![NonEmptyVec::from((4, vec![5]))],
));
let expected = NonEmptyVec::from((1, vec![2, 3, 4, 5]));
assert_eq!(NonEmptyVec::flatten(non_empty), expected);pub fn binary_search(&self, x: &T) -> Result<usize, usize>where
T: Ord,
Available on crate features grpc and http and std only.
pub fn binary_search(&self, x: &T) -> Result<usize, usize>where
T: Ord,
grpc and http and std only.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.
§Examples
use rama_utils::collections::NonEmptyVec;
let non_empty = NonEmptyVec::from((0, vec![1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]));
assert_eq!(non_empty.binary_search(&0), Ok(0));
assert_eq!(non_empty.binary_search(&13), Ok(9));
assert_eq!(non_empty.binary_search(&4), Err(7));
assert_eq!(non_empty.binary_search(&100), Err(13));
let r = non_empty.binary_search(&1);
assert!(match r { Ok(1..=4) => true, _ => false, });If you want to insert an item to a sorted non-empty vector, while maintaining sort order:
use rama_utils::collections::NonEmptyVec;
let mut non_empty = NonEmptyVec::from((0, vec![1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]));
let num = 42;
let idx = non_empty.binary_search(&num).unwrap_or_else(|x| x);
non_empty.insert(idx, num);
assert_eq!(non_empty, NonEmptyVec::from((0, vec![1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 42, 55])));pub fn binary_search_by<'a, F>(&'a self, f: F) -> Result<usize, usize>
Available on crate features grpc and http and std only.
pub fn binary_search_by<'a, F>(&'a self, f: F) -> Result<usize, usize>
grpc and http and std only.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.
§Examples
Looks up a series of four elements. The first is found, with a uniquely determined
position; the second and third are not found; the fourth could match any position in [1,4].
use rama_utils::collections::NonEmptyVec;
let non_empty = NonEmptyVec::from((0, vec![1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]));
let seek = 0;
assert_eq!(non_empty.binary_search_by(|probe| probe.cmp(&seek)), Ok(0));
let seek = 13;
assert_eq!(non_empty.binary_search_by(|probe| probe.cmp(&seek)), Ok(9));
let seek = 4;
assert_eq!(non_empty.binary_search_by(|probe| probe.cmp(&seek)), Err(7));
let seek = 100;
assert_eq!(non_empty.binary_search_by(|probe| probe.cmp(&seek)), Err(13));
let seek = 1;
let r = non_empty.binary_search_by(|probe| probe.cmp(&seek));
assert!(match r { Ok(1..=4) => true, _ => false, });pub fn binary_search_by_key<'a, B, F>(
&'a self,
b: &B,
f: F,
) -> Result<usize, usize>
Available on crate features grpc and http and std only.
pub fn binary_search_by_key<'a, B, F>( &'a self, b: &B, f: F, ) -> Result<usize, usize>
grpc and http and std only.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.
§Examples
Looks up a series of four elements in a non-empty vector of pairs sorted by their second elements. The first is found, with a uniquely determined position; the second and third are not found; the fourth could match any position in [1, 4].
use rama_utils::collections::NonEmptyVec;
let non_empty = NonEmptyVec::from((
(0, 0),
vec![(2, 1), (4, 1), (5, 1), (3, 1),
(1, 2), (2, 3), (4, 5), (5, 8), (3, 13),
(1, 21), (2, 34), (4, 55)]
));
assert_eq!(non_empty.binary_search_by_key(&0, |&(a,b)| b), Ok(0));
assert_eq!(non_empty.binary_search_by_key(&13, |&(a,b)| b), Ok(9));
assert_eq!(non_empty.binary_search_by_key(&4, |&(a,b)| b), Err(7));
assert_eq!(non_empty.binary_search_by_key(&100, |&(a,b)| b), Err(13));
let r = non_empty.binary_search_by_key(&1, |&(a,b)| b);
assert!(match r { Ok(1..=4) => true, _ => false, });pub fn maximum(&self) -> &Twhere
T: Ord,
Available on crate features grpc and http and std only.
pub fn maximum(&self) -> &Twhere
T: Ord,
grpc and http and std only.Returns the maximum element in the non-empty vector.
This will return the first item in the vector if the tail is empty.
§Examples
use rama_utils::collections::NonEmptyVec;
let non_empty = NonEmptyVec::new(42);
assert_eq!(non_empty.maximum(), &42);
let non_empty = NonEmptyVec::from((1, vec![-34, 42, 76, 4, 5]));
assert_eq!(non_empty.maximum(), &76);pub fn minimum(&self) -> &Twhere
T: Ord,
Available on crate features grpc and http and std only.
pub fn minimum(&self) -> &Twhere
T: Ord,
grpc and http and std only.Returns the minimum element in the non-empty vector.
This will return the first item in the vector if the tail is empty.
§Examples
use rama_utils::collections::NonEmptyVec;
let non_empty = NonEmptyVec::new(42);
assert_eq!(non_empty.minimum(), &42);
let non_empty = NonEmptyVec::from((1, vec![-34, 42, 76, 4, 5]));
assert_eq!(non_empty.minimum(), &-34);pub fn maximum_by<F>(&self, compare: F) -> &T
Available on crate features grpc and http and std only.
pub fn maximum_by<F>(&self, compare: F) -> &T
grpc and http and std only.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.
§Examples
use rama_utils::collections::NonEmptyVec;
let non_empty = NonEmptyVec::new((0, 42));
assert_eq!(non_empty.maximum_by(|(k, _), (l, _)| k.cmp(l)), &(0, 42));
let non_empty = NonEmptyVec::from(((2, 1), vec![(2, -34), (4, 42), (0, 76), (1, 4), (3, 5)]));
assert_eq!(non_empty.maximum_by(|(k, _), (l, _)| k.cmp(l)), &(4, 42));pub fn minimum_by<F>(&self, compare: F) -> &T
Available on crate features grpc and http and std only.
pub fn minimum_by<F>(&self, compare: F) -> &T
grpc and http and std only.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.
use rama_utils::collections::NonEmptyVec;
let non_empty = NonEmptyVec::new((0, 42));
assert_eq!(non_empty.minimum_by(|(k, _), (l, _)| k.cmp(l)), &(0, 42));
let non_empty = NonEmptyVec::from(((2, 1), vec![(2, -34), (4, 42), (0, 76), (1, 4), (3, 5)]));
assert_eq!(non_empty.minimum_by(|(k, _), (l, _)| k.cmp(l)), &(0, 76));pub fn maximum_by_key<U, F>(&self, f: F) -> &T
Available on crate features grpc and http and std only.
pub fn maximum_by_key<U, F>(&self, f: F) -> &T
grpc and http and std only.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.
§Examples
use rama_utils::collections::NonEmptyVec;
let non_empty = NonEmptyVec::new((0, 42));
assert_eq!(non_empty.maximum_by_key(|(k, _)| *k), &(0, 42));
let non_empty = NonEmptyVec::from(((2, 1), vec![(2, -34), (4, 42), (0, 76), (1, 4), (3, 5)]));
assert_eq!(non_empty.maximum_by_key(|(k, _)| *k), &(4, 42));
assert_eq!(non_empty.maximum_by_key(|(k, _)| -k), &(0, 76));pub fn minimum_by_key<U, F>(&self, f: F) -> &T
Available on crate features grpc and http and std only.
pub fn minimum_by_key<U, F>(&self, f: F) -> &T
grpc and http and std only.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.
§Examples
use rama_utils::collections::NonEmptyVec;
let non_empty = NonEmptyVec::new((0, 42));
assert_eq!(non_empty.minimum_by_key(|(k, _)| *k), &(0, 42));
let non_empty = NonEmptyVec::from(((2, 1), vec![(2, -34), (4, 42), (0, 76), (1, 4), (3, 5)]));
assert_eq!(non_empty.minimum_by_key(|(k, _)| *k), &(0, 76));
assert_eq!(non_empty.minimum_by_key(|(k, _)| -k), &(4, 42));pub fn sort(&mut self)where
T: Ord,
Available on crate features grpc and http and std only.
pub fn sort(&mut self)where
T: Ord,
grpc and http and std only.Sorts the NonEmptyVec.
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.
§Examples
use rama_utils::collections::non_empty_vec;
let mut non_empty = non_empty_vec![-5, 4, 1, -3, 2];
non_empty.sort();
assert!(non_empty == non_empty_vec![-5, -3, 1, 2, 4]);pub fn sort_by<F>(&mut self, compare: F)
Available on crate features grpc and http and std only.
pub fn sort_by<F>(&mut self, compare: F)
grpc and http and std only.Sorts the NonEmptyVec 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.
§Examples
use rama_utils::collections::non_empty_vec;
let mut non_empty = non_empty_vec![-5, 4, 1, -3, 2];
non_empty.sort_by(|a, b| a.cmp(b));
assert!(non_empty == non_empty_vec![-5, -3, 1, 2, 4]);pub fn sort_by_key<K, F>(&mut self, f: F)
Available on crate features grpc and http and std only.
pub fn sort_by_key<K, F>(&mut self, f: F)
grpc and http and std only.Sorts the NonEmptyVec with a key extraction function.
§Examples
use rama_utils::collections::non_empty_vec;
let mut non_empty = non_empty_vec!["bbb", "a", "cccc"];
non_empty.sort_by_key(|s| s.len());
assert!(non_empty == non_empty_vec!["a", "bbb", "cccc"]);pub fn sort_by_cached_key<K, F>(&mut self, f: F)
Available on crate features grpc and http and std only.
pub fn sort_by_cached_key<K, F>(&mut self, f: F)
grpc and http and std only.Sorts the NonEmptyVec 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.
§Examples
use rama_utils::collections::non_empty_vec;
let mut non_empty = non_empty_vec!["bbb", "a", "cccc"];
non_empty.sort_by_cached_key(|s| s.len());
assert!(non_empty == non_empty_vec!["a", "bbb", "cccc"]);Trait Implementations§
§impl<T> Clone for NonEmptyVec<T>where
T: Clone,
impl<T> Clone for NonEmptyVec<T>where
T: Clone,
§fn clone(&self) -> NonEmptyVec<T>
fn clone(&self) -> NonEmptyVec<T>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more§impl<T> Debug for NonEmptyVec<T>where
T: Debug,
impl<T> Debug for NonEmptyVec<T>where
T: Debug,
§impl<T> Default for NonEmptyVec<T>where
T: Default,
impl<T> Default for NonEmptyVec<T>where
T: Default,
§fn default() -> NonEmptyVec<T>
fn default() -> NonEmptyVec<T>
§impl<'de, T> Deserialize<'de> for NonEmptyVec<T>where
T: Deserialize<'de>,
impl<'de, T> Deserialize<'de> for NonEmptyVec<T>where
T: Deserialize<'de>,
§fn deserialize<__D>(
__deserializer: __D,
) -> Result<NonEmptyVec<T>, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<NonEmptyVec<T>, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
§impl<R> DnsAddressResolver for NonEmptyVec<R>where
R: DnsAddressResolver,
impl<R> DnsAddressResolver for NonEmptyVec<R>where
R: DnsAddressResolver,
§type Error = OpaqueError
type Error = OpaqueError
DnsAddressResolver§fn lookup_ipv4(
&self,
domain: Domain,
) -> impl Stream<Item = Result<Ipv4Addr, <NonEmptyVec<R> as DnsAddressResolver>::Error>> + Send
fn lookup_ipv4( &self, domain: Domain, ) -> impl Stream<Item = Result<Ipv4Addr, <NonEmptyVec<R> as DnsAddressResolver>::Error>> + Send
§async fn lookup_ipv4_first(
&self,
domain: Domain,
) -> Option<Result<Ipv4Addr, <NonEmptyVec<R> as DnsAddressResolver>::Error>>
async fn lookup_ipv4_first( &self, domain: Domain, ) -> Option<Result<Ipv4Addr, <NonEmptyVec<R> as DnsAddressResolver>::Error>>
§async fn lookup_ipv4_rand(
&self,
domain: Domain,
) -> Option<Result<Ipv4Addr, <NonEmptyVec<R> as DnsAddressResolver>::Error>>
async fn lookup_ipv4_rand( &self, domain: Domain, ) -> Option<Result<Ipv4Addr, <NonEmptyVec<R> as DnsAddressResolver>::Error>>
§fn lookup_ipv6(
&self,
domain: Domain,
) -> impl Stream<Item = Result<Ipv6Addr, <NonEmptyVec<R> as DnsAddressResolver>::Error>> + Send
fn lookup_ipv6( &self, domain: Domain, ) -> impl Stream<Item = Result<Ipv6Addr, <NonEmptyVec<R> as DnsAddressResolver>::Error>> + Send
§async fn lookup_ipv6_first(
&self,
domain: Domain,
) -> Option<Result<Ipv6Addr, <NonEmptyVec<R> as DnsAddressResolver>::Error>>
async fn lookup_ipv6_first( &self, domain: Domain, ) -> Option<Result<Ipv6Addr, <NonEmptyVec<R> as DnsAddressResolver>::Error>>
§async fn lookup_ipv6_rand(
&self,
domain: Domain,
) -> Option<Result<Ipv6Addr, <NonEmptyVec<R> as DnsAddressResolver>::Error>>
async fn lookup_ipv6_rand( &self, domain: Domain, ) -> Option<Result<Ipv6Addr, <NonEmptyVec<R> as DnsAddressResolver>::Error>>
§fn into_box_dns_address_resolver(self) -> BoxDnsAddressResolver
fn into_box_dns_address_resolver(self) -> BoxDnsAddressResolver
§impl<R> DnsResolver for NonEmptyVec<R>where
R: DnsResolver,
impl<R> DnsResolver for NonEmptyVec<R>where
R: DnsResolver,
fn into_box_dns_resolver(self) -> BoxDnsResolverwhere
Self: Sized,
§impl<R> DnsTxtResolver for NonEmptyVec<R>where
R: DnsTxtResolver,
impl<R> DnsTxtResolver for NonEmptyVec<R>where
R: DnsTxtResolver,
§type Error = OpaqueError
type Error = OpaqueError
DnsTxtResolver§fn lookup_txt(
&self,
domain: Domain,
) -> impl Stream<Item = Result<Bytes, <NonEmptyVec<R> as DnsTxtResolver>::Error>> + Send
fn lookup_txt( &self, domain: Domain, ) -> impl Stream<Item = Result<Bytes, <NonEmptyVec<R> as DnsTxtResolver>::Error>> + Send
§fn into_box_dns_txt_resolver(self) -> BoxDnsTxtResolver
fn into_box_dns_txt_resolver(self) -> BoxDnsTxtResolver
impl<T> Eq for NonEmptyVec<T>where
T: Eq,
§impl<A> Extend<A> for NonEmptyVec<A>
impl<A> Extend<A> for NonEmptyVec<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<T> From<(T, Vec<T>)> for NonEmptyVec<T>
impl<T> From<(T, Vec<T>)> for NonEmptyVec<T>
§fn from(_: (T, Vec<T>)) -> NonEmptyVec<T>
fn from(_: (T, Vec<T>)) -> NonEmptyVec<T>
Turns a pair of an element and a Vec into a NonEmptyVec.
§impl<T> From<NonEmptyVec<T>> for Vec<T>
impl<T> From<NonEmptyVec<T>> for Vec<T>
§fn from(non_empty_vec: NonEmptyVec<T>) -> Vec<T>
fn from(non_empty_vec: NonEmptyVec<T>) -> Vec<T>
Turns a non-empty list into a Vec.
§impl<T> From<NonEmptyVec<T>> for (T, Vec<T>)
impl<T> From<NonEmptyVec<T>> for (T, Vec<T>)
§fn from(non_empty_vec: NonEmptyVec<T>) -> (T, Vec<T>)
fn from(non_empty_vec: NonEmptyVec<T>) -> (T, Vec<T>)
Turns a non-empty list into a Vec.
§impl<T> Hash for NonEmptyVec<T>where
T: Hash,
impl<T> Hash for NonEmptyVec<T>where
T: Hash,
§impl<T> Index<usize> for NonEmptyVec<T>
impl<T> Index<usize> for NonEmptyVec<T>
§impl<T> IndexMut<usize> for NonEmptyVec<T>
impl<T> IndexMut<usize> for NonEmptyVec<T>
§impl<T> IntoIterator for NonEmptyVec<T>
impl<T> IntoIterator for NonEmptyVec<T>
§type IntoIter = Chain<Once<T>, IntoIter<<NonEmptyVec<T> as IntoIterator>::Item>>
type IntoIter = Chain<Once<T>, IntoIter<<NonEmptyVec<T> as IntoIterator>::Item>>
§fn into_iter(self) -> <NonEmptyVec<T> as IntoIterator>::IntoIter
fn into_iter(self) -> <NonEmptyVec<T> as IntoIterator>::IntoIter
§impl<'a, T> IntoIterator for &'a NonEmptyVec<T>
impl<'a, T> IntoIterator for &'a NonEmptyVec<T>
§impl<T> Ord for NonEmptyVec<T>where
T: Ord,
impl<T> Ord for NonEmptyVec<T>where
T: Ord,
§fn cmp(&self, other: &NonEmptyVec<T>) -> Ordering
fn cmp(&self, other: &NonEmptyVec<T>) -> Ordering
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
§impl<T> PartialEq for NonEmptyVec<T>where
T: PartialEq,
impl<T> PartialEq for NonEmptyVec<T>where
T: PartialEq,
§impl<T> PartialOrd for NonEmptyVec<T>where
T: PartialOrd,
impl<T> PartialOrd for NonEmptyVec<T>where
T: PartialOrd,
§impl<T> Serialize for NonEmptyVec<T>where
T: Serialize,
impl<T> Serialize for NonEmptyVec<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<T> StructuralPartialEq for NonEmptyVec<T>where
T: PartialEq,
§impl<T> TryFrom<Vec<T>> for NonEmptyVec<T>
impl<T> TryFrom<Vec<T>> for NonEmptyVec<T>
§type Error = NonEmptyVecEmptyError
type Error = NonEmptyVecEmptyError
§fn try_from(
vec: Vec<T>,
) -> Result<NonEmptyVec<T>, <NonEmptyVec<T> as TryFrom<Vec<T>>>::Error>
fn try_from( vec: Vec<T>, ) -> Result<NonEmptyVec<T>, <NonEmptyVec<T> as TryFrom<Vec<T>>>::Error>
Auto Trait Implementations§
impl<T> Freeze for NonEmptyVec<T>where
T: Freeze,
impl<T> RefUnwindSafe for NonEmptyVec<T>where
T: RefUnwindSafe,
impl<T> Send for NonEmptyVec<T>where
T: Send,
impl<T> Sync for NonEmptyVec<T>where
T: Sync,
impl<T> Unpin for NonEmptyVec<T>where
T: Unpin,
impl<T> UnsafeUnpin for NonEmptyVec<T>where
T: UnsafeUnpin,
impl<T> UnwindSafe for NonEmptyVec<T>where
T: UnwindSafe,
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
§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<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
§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
§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<R> HappyEyeballAddressResolverExt for Rwhere
R: DnsAddressResolver,
impl<R> HappyEyeballAddressResolverExt for Rwhere
R: DnsAddressResolver,
§fn happy_eyeballs_resolver(
&self,
host: impl Into<Host>,
) -> HappyEyeballAddressResolver<'_, R>
fn happy_eyeballs_resolver( &self, host: impl Into<Host>, ) -> HappyEyeballAddressResolver<'_, R>
§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> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a rama_grpc::Request§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§impl<T, U> RamaTryFrom<T> for Uwhere
U: TryFrom<T>,
impl<T, U> RamaTryFrom<T> for Uwhere
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
fn rama_try_from(value: T) -> Result<U, <U as RamaTryFrom<T>>::Error>
§impl<T, U, CrateMarker> RamaTryInto<U, CrateMarker> for Twhere
U: RamaTryFrom<T, CrateMarker>,
impl<T, U, CrateMarker> RamaTryInto<U, CrateMarker> for Twhere
U: RamaTryFrom<T, CrateMarker>,
type Error = <U as RamaTryFrom<T, CrateMarker>>::Error
fn rama_try_into(self) -> Result<U, <U as RamaTryFrom<T, CrateMarker>>::Error>
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
§impl<V, F> ValueFormatter<&V> for F
impl<V, F> ValueFormatter<&V> for F
§fn format_value(writer: impl ValueWriter, value: &&V)
fn format_value(writer: impl ValueWriter, value: &&V)
value to writer§impl<V, F> ValueFormatter<Arc<V>> for F
impl<V, F> ValueFormatter<Arc<V>> for F
§fn format_value(writer: impl ValueWriter, value: &Arc<V>)
fn format_value(writer: impl ValueWriter, value: &Arc<V>)
value to writer§impl<V, F> ValueFormatter<Box<V>> for F
impl<V, F> ValueFormatter<Box<V>> for F
§fn format_value(writer: impl ValueWriter, value: &Box<V>)
fn format_value(writer: impl ValueWriter, value: &Box<V>)
value to writer§impl<V, F> ValueFormatter<Cow<'_, V>> for F
impl<V, F> ValueFormatter<Cow<'_, V>> for F
§fn format_value(writer: impl ValueWriter, value: &Cow<'_, V>)
fn format_value(writer: impl ValueWriter, value: &Cow<'_, V>)
value to writer§impl<V, F> ValueFormatter<Option<V>> for Fwhere
F: ValueFormatter<V> + ?Sized,
impl<V, F> ValueFormatter<Option<V>> for Fwhere
F: ValueFormatter<V> + ?Sized,
§fn format_value(writer: impl ValueWriter, value: &Option<V>)
fn format_value(writer: impl ValueWriter, value: &Option<V>)
value to writer