Struct nonempty_collections::slice::NESlice
source · pub struct NESlice<'a, T> {
pub head: &'a T,
pub tail: &'a [T],
}Expand description
A non-empty slice. Like crate::NEVec, but guaranteed to have borrowed
contents.
NESlice::from_slice is the simplest way to construct this from borrowed data.
Unfortunately there is no macro for this, but if you want one, just use
nev! and handle the ownership manually. Also consider
crate::NEVec::as_nonempty_slice.
Fields§
§head: &'a TThe element of the non-empty slice. Always exists.
tail: &'a [T]The remaining elements of the non-empty slice, perhaps empty.
Implementations§
source§impl<'a, T> NESlice<'a, T>
impl<'a, T> NESlice<'a, T>
sourcepub fn new(head: &'a T, tail: &'a [T]) -> Self
pub fn new(head: &'a T, tail: &'a [T]) -> Self
Create a new non-empty slice with an initial element.
sourcepub fn from_slice(slice: &'a [T]) -> Option<Self>
pub fn from_slice(slice: &'a [T]) -> Option<Self>
Using from_slice gives a proof that the input slice is non-empty in
the Some branch.
sourcepub fn len(&self) -> NonZeroUsize
pub fn len(&self) -> NonZeroUsize
Get the length of the slice.
sourcepub fn nonempty_chunks(&'a self, chunk_size: NonZeroUsize) -> NEChunks<'a, T>
pub fn nonempty_chunks(&'a self, chunk_size: NonZeroUsize) -> NEChunks<'a, T>
Returns a non-empty iterator over chunk_size elements of the NESlice
at a time, starting at the beginning of the NESlice.
use nonempty_collections::*;
use std::num::NonZeroUsize;
let v = nev![1,2,3,4,5,6];
let s = v.as_nonempty_slice();
let n = NonZeroUsize::new(2).unwrap();
let r = s.nonempty_chunks(n).collect::<NEVec<_>>();
let a = nev![1,2];
let b = nev![3,4];
let c = nev![5,6];
assert_eq!(r, nev![a.as_nonempty_slice(), b.as_nonempty_slice(), c.as_nonempty_slice()]);Trait Implementations§
source§impl<'a, T> IntoNonEmptyIterator for NESlice<'a, T>
impl<'a, T> IntoNonEmptyIterator for NESlice<'a, T>
§type IntoIter = Iter<'a, T>
type IntoIter = Iter<'a, T>
Which kind of
NonEmptyIterator are we turning this into?source§fn into_nonempty_iter(self) -> Self::IntoIter
fn into_nonempty_iter(self) -> Self::IntoIter
Creates a
NonEmptyIterator from a value.source§impl<'a, T: Ord> Ord for NESlice<'a, T>
impl<'a, T: Ord> Ord for NESlice<'a, T>
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
source§impl<'a, T: PartialEq> PartialEq for NESlice<'a, T>
impl<'a, T: PartialEq> PartialEq for NESlice<'a, T>
source§impl<'a, T: PartialOrd> PartialOrd for NESlice<'a, T>
impl<'a, T: PartialOrd> PartialOrd for NESlice<'a, T>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for
self and other) and is used by the <=
operator. Read moreimpl<'a, T: Eq> Eq for NESlice<'a, T>
impl<'a, T> StructuralPartialEq for NESlice<'a, T>
Auto Trait Implementations§
impl<'a, T> Freeze for NESlice<'a, T>
impl<'a, T> RefUnwindSafe for NESlice<'a, T>where
T: RefUnwindSafe,
impl<'a, T> Send for NESlice<'a, T>where
T: Sync,
impl<'a, T> Sync for NESlice<'a, T>where
T: Sync,
impl<'a, T> Unpin for NESlice<'a, T>
impl<'a, T> UnwindSafe for NESlice<'a, T>where
T: RefUnwindSafe,
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