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 new(head: T) -> NonEmptyVec<T>
pub const fn new(head: T) -> NonEmptyVec<T>
Create a singleton collection from one element.
Sourcepub const fn singleton(head: T) -> NonEmptyVec<T>
pub const fn singleton(head: T) -> NonEmptyVec<T>
Alias for NonEmptyVec::new, matching the nonempty crate naming.
Sourcepub fn from_vec(vec: Vec<T>) -> Option<NonEmptyVec<T>>
pub fn from_vec(vec: Vec<T>) -> Option<NonEmptyVec<T>>
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 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 (const: unstable) · 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<'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<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<T> Eq for NonEmptyVec<T>where
T: Eq,
Source§impl<T> From<NonEmptyVec<T>> for Vec<T>
impl<T> From<NonEmptyVec<T>> for Vec<T>
Source§fn from(value: NonEmptyVec<T>) -> Vec<T>
fn from(value: NonEmptyVec<T>) -> Vec<T>
Source§impl<T> PartialEq for NonEmptyVec<T>where
T: PartialEq,
impl<T> PartialEq for NonEmptyVec<T>where
T: PartialEq,
Source§impl<T> Serialize for NonEmptyVec<T>where
T: Serialize,
impl<T> Serialize for NonEmptyVec<T>where
T: Serialize,
Source§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,
Source§impl<T> TryFrom<Vec<T>> for NonEmptyVec<T>
impl<T> TryFrom<Vec<T>> for NonEmptyVec<T>
Source§type Error = EmptyCollectionError
type Error = EmptyCollectionError
Source§fn try_from(
value: Vec<T>,
) -> Result<NonEmptyVec<T>, <NonEmptyVec<T> as TryFrom<Vec<T>>>::Error>
fn try_from( value: Vec<T>, ) -> Result<NonEmptyVec<T>, <NonEmptyVec<T> as TryFrom<Vec<T>>>::Error>
Auto Trait Implementations§
impl<T> Forget for NonEmptyVec<T>
impl<T> Freeze for NonEmptyVec<T>
impl<T> Move 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
impl<T> CacheValue for 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<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
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.impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§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