NEVec

Struct NEVec 

Source
pub struct NEVec<T>(/* private fields */);
Expand description

Non-empty vector type.

Implementations§

Source§

impl<T> NEVec<T>

Source

pub fn new(head: T, tail: Vec<T>) -> Self

Creates a new NEVec, ensuring at least one element is present.

Source

pub fn singleton(value: T) -> Self

Creates a new singleton NEVec. Semantically equivalent to:

NEVec::new(value, Vec::new());
Source

pub fn head(&self) -> &T

Returns the first element. This operation is safe as the invariant guarantees at least one element is present.

Source

pub fn init(&self) -> Iter<'_, T>

Returns all elements except the last one. This may be empty if the NEVec is a singleton.

Source

pub fn tail(&self) -> Iter<'_, T>

Returns all elements except the first one. This may be empty if the NEVec is a singleton.

Source

pub fn last(&self) -> &T

Returns the last element. This operation is safe as the invariant guarantees at least one element is present.

Source

pub fn from_vec(vec: Vec<T>) -> Result<Self, NonEmptyError>

Attempts to create a NEVec from a Vec, returning None if the Vec is empty.

assert!(NEVec::from_vec(vec![42]).is_ok());
assert!(NEVec::from_vec(Vec::<u32>::new()).is_err());
Source

pub fn from_deque(deque: VecDeque<T>) -> Result<Self, NonEmptyError>

Attempts to create a NEVec from a VecDeque, returning None if the VecDeque is empty.

assert!(NEVec::from_deque(VecDeque::from(vec![42])).is_ok());
assert!(NEVec::from_deque(VecDeque::<u32>::new()).is_err());
Source

pub fn len(&self) -> usize

Returns the length of this NEVec.

Source

pub fn is_empty(&self) -> bool

A NEVec is always non-empty.

Source

pub fn as_slice(&mut self) -> &[T]

Returns this NEVec as a slice.

Source

pub fn push_front(&mut self, value: T)

Pushes an element to the front of the NEVec.

Source

pub fn push_back(&mut self, value: T)

Pushes an element to the back of the NEVec.

Source

pub fn pop_front(&mut self) -> Result<T, NonEmptyError>

Tries to remove the first element.

Source

pub fn pop_back(&mut self) -> Result<T, NonEmptyError>

Tries to remove the last element.

Source

pub fn split_first(&self) -> (&T, Iter<'_, T>)

Splits the NEVec into the first element and the rest. This operation is guaranteed to succeed because the invariant guarantees at least one element is present.

Source

pub fn split_last(&self) -> (Iter<'_, T>, &T)

Splits the NEVec into all elements except the last one and the last element. This operation is guaranteed to succeed because the invariant guarantees at least one element is present.

Source

pub fn take_split_first(self) -> (T, IntoIter<T>)

Like NEVec::split_first, but consumes the NEVec.

Source

pub fn take_split_last(self) -> (IntoIter<T>, T)

Like NEVec::split_last, but consumes the NEVec.

Source

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

Returns an iterator over the elements of the NEVec.

Source

pub fn extend<I: IntoIterator<Item = T>>(&mut self, other: I)

Extends the NEVec with the elements from another collection.

Trait Implementations§

Source§

impl<T: Clone> Clone for NEVec<T>

Source§

fn clone(&self) -> NEVec<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> Debug for NEVec<T>

Source§

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

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

impl<T> From<(T, Vec<T>)> for NEVec<T>

Source§

fn from(value: (T, Vec<T>)) -> Self

Converts to this type from the input type.
Source§

impl<T> From<(T, VecDeque<T>)> for NEVec<T>

Source§

fn from(value: (T, VecDeque<T>)) -> Self

Converts to this type from the input type.
Source§

impl<T> From<NEVec<T>> for Vec<T>

Source§

fn from(ne: NEVec<T>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<NEVec<T>> for VecDeque<T>

Source§

fn from(ne: NEVec<T>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<T> for NEVec<T>

Source§

fn from(value: T) -> Self

Converts to this type from the input type.
Source§

impl<T: Hash> Hash for NEVec<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<T> Index<usize> for NEVec<T>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

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

impl<'a, T> IntoIterator for &'a NEVec<T>

Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = 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<'a, T> IntoIterator for &'a mut NEVec<T>

Source§

type Item = &'a mut T

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'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<T> IntoIterator for NEVec<T>

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<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<T: PartialEq> PartialEq for NEVec<T>

Source§

fn eq(&self, other: &NEVec<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> TryFrom<Vec<T>> for NEVec<T>

Source§

type Error = NonEmptyError

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

fn try_from(vec: Vec<T>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<T: Eq> Eq for NEVec<T>

Source§

impl<T> StructuralPartialEq for NEVec<T>

Auto Trait Implementations§

§

impl<T> Freeze for NEVec<T>

§

impl<T> RefUnwindSafe for NEVec<T>
where T: RefUnwindSafe,

§

impl<T> Send for NEVec<T>
where T: Send,

§

impl<T> Sync for NEVec<T>
where T: Sync,

§

impl<T> Unpin for NEVec<T>
where T: Unpin,

§

impl<T> UnwindSafe for NEVec<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<T> From<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
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.