pub struct NonEmptyVec<T> { /* private fields */ }Expand description
A non-empty vector guaranteed to contain at least one element.
This type provides type-level guarantees that operations like head(),
max(), and min() will always succeed without returning Option.
§Example
use stillwater::NonEmptyVec;
let nev = NonEmptyVec::new(1, vec![2, 3, 4]);
assert_eq!(nev.head(), &1);
assert_eq!(nev.tail(), &[2, 3, 4]);
assert_eq!(nev.len(), 4);Implementations§
Source§impl<T> NonEmptyVec<T>
impl<T> NonEmptyVec<T>
Sourcepub fn new(head: T, tail: Vec<T>) -> NonEmptyVec<T>
pub fn new(head: T, tail: Vec<T>) -> NonEmptyVec<T>
Create a new non-empty vector with a head element and tail.
§Example
use stillwater::NonEmptyVec;
let nev = NonEmptyVec::new(1, vec![2, 3]);
assert_eq!(nev.len(), 3);Sourcepub fn singleton(value: T) -> NonEmptyVec<T>
pub fn singleton(value: T) -> NonEmptyVec<T>
Create a non-empty vector from a single element.
§Example
use stillwater::NonEmptyVec;
let nev = NonEmptyVec::singleton(42);
assert_eq!(nev.len(), 1);
assert_eq!(nev.head(), &42);Sourcepub fn from_vec(vec: Vec<T>) -> Option<NonEmptyVec<T>>
pub fn from_vec(vec: Vec<T>) -> Option<NonEmptyVec<T>>
Try to create a non-empty vector from a Vec.
Returns None if the vector is empty.
§Example
use stillwater::NonEmptyVec;
let nev = NonEmptyVec::from_vec(vec![1, 2, 3]).unwrap();
assert_eq!(nev.len(), 3);
let empty = NonEmptyVec::from_vec(Vec::<i32>::new());
assert!(empty.is_none());Sourcepub fn from_vec_unchecked(vec: Vec<T>) -> NonEmptyVec<T>
pub fn from_vec_unchecked(vec: Vec<T>) -> NonEmptyVec<T>
Create a non-empty vector from a Vec without checking.
§Panics
Panics if the vector is empty.
§Example
use stillwater::NonEmptyVec;
let nev = NonEmptyVec::from_vec_unchecked(vec![1, 2, 3]);
assert_eq!(nev.len(), 3);use stillwater::NonEmptyVec;
let nev = NonEmptyVec::from_vec_unchecked(Vec::<i32>::new()); // panicsSourcepub fn head(&self) -> &T
pub fn head(&self) -> &T
Get the first element (always succeeds).
§Example
use stillwater::NonEmptyVec;
let nev = NonEmptyVec::new(1, vec![2, 3]);
assert_eq!(nev.head(), &1);Sourcepub fn tail(&self) -> &[T]
pub fn tail(&self) -> &[T]
Get the tail (all elements except the first).
§Example
use stillwater::NonEmptyVec;
let nev = NonEmptyVec::new(1, vec![2, 3]);
assert_eq!(nev.tail(), &[2, 3]);Sourcepub fn last(&self) -> &T
pub fn last(&self) -> &T
Get the last element (always succeeds).
§Example
use stillwater::NonEmptyVec;
let nev = NonEmptyVec::new(1, vec![2, 3]);
assert_eq!(nev.last(), &3);
let single = NonEmptyVec::singleton(42);
assert_eq!(single.last(), &42);Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Get the number of elements.
Always >= 1.
§Example
use stillwater::NonEmptyVec;
let nev = NonEmptyVec::new(1, vec![2, 3]);
assert_eq!(nev.len(), 3);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Check if the vector is empty.
Always returns false since a NonEmptyVec is guaranteed to have at least one element.
This method exists to satisfy clippy’s len_without_is_empty lint.
§Example
use stillwater::NonEmptyVec;
let nev = NonEmptyVec::singleton(42);
assert!(!nev.is_empty());Sourcepub fn push(&mut self, value: T)
pub fn push(&mut self, value: T)
Push an element to the end.
§Example
use stillwater::NonEmptyVec;
let mut nev = NonEmptyVec::singleton(1);
nev.push(2);
nev.push(3);
assert_eq!(nev.len(), 3);Sourcepub fn pop(&mut self) -> Option<T>
pub fn pop(&mut self) -> Option<T>
Pop an element from the end.
Returns None if there’s only one element (the head).
§Example
use stillwater::NonEmptyVec;
let mut nev = NonEmptyVec::new(1, vec![2, 3]);
assert_eq!(nev.pop(), Some(3));
assert_eq!(nev.pop(), Some(2));
assert_eq!(nev.pop(), None); // Can't remove headSourcepub fn map<U, F>(self, f: F) -> NonEmptyVec<U>where
F: FnMut(T) -> U,
pub fn map<U, F>(self, f: F) -> NonEmptyVec<U>where
F: FnMut(T) -> U,
Map a function over all elements.
§Example
use stillwater::NonEmptyVec;
let nev = NonEmptyVec::new(1, vec![2, 3]);
let doubled = nev.map(|x| x * 2);
assert_eq!(doubled.head(), &2);
assert_eq!(doubled.tail(), &[4, 6]);Sourcepub fn filter<F>(self, predicate: F) -> Vec<T>
pub fn filter<F>(self, predicate: F) -> Vec<T>
Filter elements (may return empty Vec).
Since filtering might remove all elements, this returns Vec<T>.
§Example
use stillwater::NonEmptyVec;
let nev = NonEmptyVec::new(1, vec![2, 3, 4]);
let evens = nev.filter(|x| x % 2 == 0);
assert_eq!(evens, vec![2, 4]);
let none = NonEmptyVec::singleton(1).filter(|x| x % 2 == 0);
assert_eq!(none, vec![]);Trait Implementations§
Source§impl<T> Clone for NonEmptyVec<T>where
T: Clone,
impl<T> Clone for NonEmptyVec<T>where
T: Clone,
Source§fn clone(&self) -> NonEmptyVec<T>
fn clone(&self) -> NonEmptyVec<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T> Debug for NonEmptyVec<T>where
T: Debug,
impl<T> Debug for NonEmptyVec<T>where
T: Debug,
Source§impl<T> Index<usize> for NonEmptyVec<T>
impl<T> Index<usize> for NonEmptyVec<T>
Source§impl<T> IntoIterator for NonEmptyVec<T>
impl<T> IntoIterator for NonEmptyVec<T>
Source§impl<T> PartialEq for NonEmptyVec<T>where
T: PartialEq,
impl<T> PartialEq for NonEmptyVec<T>where
T: PartialEq,
Source§impl<T> Semigroup for NonEmptyVec<T>
impl<T> Semigroup for NonEmptyVec<T>
Source§fn combine(self, other: NonEmptyVec<T>) -> NonEmptyVec<T>
fn combine(self, other: NonEmptyVec<T>) -> NonEmptyVec<T>
impl<T> Eq for NonEmptyVec<T>where
T: Eq,
impl<T> StructuralPartialEq for NonEmptyVec<T>
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> UnwindSafe for NonEmptyVec<T>where
T: UnwindSafe,
Blanket Implementations§
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,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.