pub struct NonEmptySlice<'a, T> { /* private fields */ }Expand description
A borrowed slice guaranteed to contain at least one element.
The borrowed counterpart to NonEmptyVec: parse once via
NonEmptySlice::from_slice, then NonEmptySlice::first is
infallible. Avoids cloning large payloads (e.g. LLM choices arrays)
while still removing validate-then-index (is_empty + [0]) sites.
use vtcode_commons::validation::NonEmptySlice;
let values = vec![10, 20];
let parsed = NonEmptySlice::from_slice(&values).expect("non-empty");
assert_eq!(parsed.first(), &10);
assert!(NonEmptySlice::<i32>::from_slice(&[]).is_none());Implementations§
Source§impl<'a, T> NonEmptySlice<'a, T>
impl<'a, T> NonEmptySlice<'a, T>
Sourcepub fn from_slice(slice: &'a [T]) -> Option<NonEmptySlice<'a, T>>
pub fn from_slice(slice: &'a [T]) -> Option<NonEmptySlice<'a, T>>
Parse a slice into a non-empty view, returning None when empty.
This is the single boundary where the invariant is established.
Sourcepub const fn first(&self) -> &'a T
pub const fn first(&self) -> &'a T
Infallible access to the first element; no Option to re-check.
Sourcepub const fn is_empty(&self) -> bool
pub const fn is_empty(&self) -> bool
Always returns false; provided for generic len/is_empty pairing.
Trait Implementations§
Source§impl<'a, T> Clone for NonEmptySlice<'a, T>where
T: Clone,
impl<'a, T> Clone for NonEmptySlice<'a, T>where
T: Clone,
Source§fn clone(&self) -> NonEmptySlice<'a, T>
fn clone(&self) -> NonEmptySlice<'a, T>
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreimpl<'a, T> Copy for NonEmptySlice<'a, T>where
T: Copy,
Source§impl<'a, T> Debug for NonEmptySlice<'a, T>where
T: Debug,
impl<'a, T> Debug for NonEmptySlice<'a, T>where
T: Debug,
impl<'a, T> Eq for NonEmptySlice<'a, T>where
T: Eq,
Source§impl<'a, T> PartialEq for NonEmptySlice<'a, T>where
T: PartialEq,
impl<'a, T> PartialEq for NonEmptySlice<'a, T>where
T: PartialEq,
impl<'a, T> StructuralPartialEq for NonEmptySlice<'a, T>where
T: PartialEq,
Auto Trait Implementations§
impl<'a, T> Forget for NonEmptySlice<'a, T>
impl<'a, T> Freeze for NonEmptySlice<'a, T>
impl<'a, T> Move for NonEmptySlice<'a, T>
impl<'a, T> RefUnwindSafe for NonEmptySlice<'a, T>
impl<'a, T> Send for NonEmptySlice<'a, T>
impl<'a, T> Sync for NonEmptySlice<'a, T>
impl<'a, T> Unpin for NonEmptySlice<'a, T>
impl<'a, T> UnsafeUnpin for NonEmptySlice<'a, T>
impl<'a, T> UnwindSafe for NonEmptySlice<'a, 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
Mutably borrows from an owned value. Read more
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,
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>
Convert
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>
Convert
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)
Convert
&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)
Convert
&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
Compare self to
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> ⓘ
Converts
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> ⓘ
Converts
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PtyHandle for Twhere
T: Send,
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<T> ResetDiscriminant<T> for Twhere
T: Copy,
impl<T> ResetDiscriminant<T> for Twhere
T: Copy,
Source§fn discriminant(&self) -> T
fn discriminant(&self) -> T
Value based on which equality for the reset will be determined.