pub struct NonEmptyVec<T> {
pub head: T,
pub tail: Vec<T>,
}Expand description
A vector guaranteed to contain at least one element.
Follows the “Parse, don’t validate” pattern: the non-empty invariant
is established once via NonEmptyVec::from_vec or TryFrom<Vec<T>>,
and NonEmptyVec::first is then infallible (returns &T, not Option<&T>).
The shape mirrors the nonempty crate discussed in Eli Bendersky’s
“Rusty thoughts on Parse, don’t validate”: head holds the first element,
tail holds the rest, so no indexing or unwrap is needed downstream.
use vtcode_commons::validation::NonEmptyVec;
let parsed = NonEmptyVec::from_vec(vec!["a", "b"]).expect("non-empty");
assert_eq!(parsed.first(), &"a");
assert!(NonEmptyVec::<String>::from_vec(Vec::new()).is_none());Fields§
§head: TFirst element; always present.
tail: Vec<T>Remaining elements; possibly empty.
Implementations§
Source§impl<T> NonEmptyVec<T>
impl<T> NonEmptyVec<T>
Sourcepub const fn singleton(head: T) -> Self
pub const fn singleton(head: T) -> Self
Alias for NonEmptyVec::new, matching the nonempty crate naming.
Sourcepub fn from_vec(vec: Vec<T>) -> Option<Self>
pub fn from_vec(vec: Vec<T>) -> Option<Self>
Parse a Vec into a non-empty collection, returning None when empty.
This is the single boundary where the invariant is established.
Order is preserved: head is the original vec[0].
Trait Implementations§
Source§impl<T: Clone> Clone for NonEmptyVec<T>
impl<T: Clone> Clone for NonEmptyVec<T>
Source§impl<T: Debug> Debug for NonEmptyVec<T>
impl<T: Debug> Debug for NonEmptyVec<T>
Source§impl<'de, T> Deserialize<'de> for NonEmptyVec<T>where
T: Deserialize<'de>,
impl<'de, T> Deserialize<'de> for NonEmptyVec<T>where
T: Deserialize<'de>,
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
impl<T: Eq> Eq for NonEmptyVec<T>
Source§impl<T> From<NonEmptyVec<T>> for Vec<T>
impl<T> From<NonEmptyVec<T>> for Vec<T>
Source§fn from(value: NonEmptyVec<T>) -> Self
fn from(value: NonEmptyVec<T>) -> Self
Source§impl<T: PartialEq> PartialEq for NonEmptyVec<T>
impl<T: PartialEq> PartialEq for NonEmptyVec<T>
Source§impl<T: Serialize> Serialize for NonEmptyVec<T>
impl<T: Serialize> Serialize for NonEmptyVec<T>
impl<T: PartialEq> StructuralPartialEq for NonEmptyVec<T>
Auto Trait Implementations§
impl<T> Freeze for NonEmptyVec<T>
impl<T> RefUnwindSafe for NonEmptyVec<T>
impl<T> Send for NonEmptyVec<T>
impl<T> Sync for NonEmptyVec<T>
impl<T> Unpin for NonEmptyVec<T>
impl<T> UnsafeUnpin for NonEmptyVec<T>
impl<T> UnwindSafe for NonEmptyVec<T>
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,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
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.