NonEmptyVec

Struct NonEmptyVec 

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

Source

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

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

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

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()); // panics
Source

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

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

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

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

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

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

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

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

pub fn filter<F>(self, predicate: F) -> Vec<T>
where F: FnMut(&T) -> bool,

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![]);
Source

pub fn into_vec(self) -> Vec<T>

Convert to a regular Vec.

§Example
use stillwater::NonEmptyVec;

let nev = NonEmptyVec::new(1, vec![2, 3]);
let vec = nev.into_vec();
assert_eq!(vec, vec![1, 2, 3]);
Source

pub fn iter(&self) -> impl Iterator<Item = &T>

Iterate over all elements.

§Example
use stillwater::NonEmptyVec;

let nev = NonEmptyVec::new(1, vec![2, 3]);
let sum: i32 = nev.iter().sum();
assert_eq!(sum, 6);

Trait Implementations§

Source§

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

Source§

fn clone(&self) -> NonEmptyVec<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for NonEmptyVec<T>
where T: Debug,

Source§

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

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

impl<T> Index<usize> for NonEmptyVec<T>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &<NonEmptyVec<T> as Index<usize>>::Output

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

impl<T> IntoIterator for NonEmptyVec<T>

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = Chain<Once<T>, IntoIter<T>>

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

fn into_iter(self) -> <NonEmptyVec<T> as IntoIterator>::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T> PartialEq for NonEmptyVec<T>
where T: PartialEq,

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T> Semigroup for NonEmptyVec<T>

Source§

fn combine(self, other: NonEmptyVec<T>) -> NonEmptyVec<T>

Combine this value with another value associatively Read more
Source§

impl<T> Eq for NonEmptyVec<T>
where T: Eq,

Source§

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> 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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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.