Struct nslice::MinSlice

source ·
pub struct MinSlice<T, const N: usize> {
    pub head: [T; N],
    pub tail: [T],
}
Expand description

A reference to a region of memory which is known to contain N or more elements of type T.

Much like [T] itself, it is not possible to construct an owned MinSlice. MinSlice is merely a way of reinterpreting an existing slice (&[T] or &mut [T]), and it is exactly the same size as a slice: one pointer and one usize.

§Example

let slice: &[u8] = b"Hello, world";
let minslice = MinSlice::<u8, 12>::from_slice(slice).unwrap();
let value: u8 = minslice.head[6];
assert_eq!(value, b' ')

Fields§

§head: [T; N]

The bounded region of memory. Exactly N Ts.

§tail: [T]

Zero or more remaining Ts after the N in the bounded region.

Implementations§

source§

impl<T, const N: usize> MinSlice<T, N>

source

pub fn from_slice(slice: &[T]) -> Option<&MinSlice<T, N>>

Produce a &MinSlice from a slice of Ts. Returns None if there are not enough elements in slice.

source

pub fn from_mut(slice: &mut [T]) -> Option<&mut MinSlice<T, N>>

Produce a &mut MinSlice from a mutable slice of Ts. Returns None if there are not enough elements in slice.

source§

impl<T, const N: usize> MinSlice<T, N>

source

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

Returns a slice over the memory referred to by the MinSlice. Its length is guaranteed to be no less than N.

source

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

Returns a mutable slice over the memory referred to by the MinSlice. Its length is guaranteed to be no less than N.

source

pub fn resize<const M: usize>(&self) -> Option<&MinSlice<T, M>>

Resize the MinSlice, returning a new Some(&MinSlice<T, M>) if there are at least M Ts in self.

source

pub fn resize_mut<const M: usize>(&mut self) -> Option<&mut MinSlice<T, M>>

Resize the MinSlice, returning a new Some(&mut MinSlice<T, M>) if there are at least M Ts in self.

source

pub fn get(&self, i: usize) -> Option<&T>

Get a reference to a value from the MinSlice by index.

Return Some(&T) for the T at index i if i < (N + self.tail.len), or None otherwise.

source

pub fn get_mut(&mut self, i: usize) -> Option<&mut T>

Get a mutable reference to a value from the MinSlice by index.

Return Some(&mut T) for the T at index i if i < (N + self.tail.len), or None otherwise.

source

pub unsafe fn from_slice_unchecked(slice: &[T]) -> &MinSlice<T, N>

Produce a &MinSlice from a slice of Ts without checking its length.

§Safety

The caller is responsible for upholding the length invariant slice.len() >= N, in addition to all normal slice invariants.

source

pub unsafe fn from_mut_unchecked(slice: &mut [T]) -> &mut MinSlice<T, N>

Produce a &mut MinSlice from a slice of Ts without checking its length.

§Safety

The caller is responsible for upholding the length invariant slice.len() >= N, in addition to all normal slice invariants.

Trait Implementations§

source§

impl<'a, T, const N: usize> Into<&'a ExactSlice<T, N>> for &'a MinSlice<T, N>

source§

fn into(self) -> &'a ExactSlice<T, N>

Converts this type into the (usually inferred) input type.
source§

impl<'a, T, const N: usize> Into<&'a MinSlice<T, N>> for &'a ExactSlice<T, N>

source§

fn into(self) -> &'a MinSlice<T, N>

Converts this type into the (usually inferred) input type.
source§

impl<'a, T, const N: usize> Into<&'a mut ExactSlice<T, N>> for &'a mut MinSlice<T, N>

source§

fn into(self) -> &'a mut ExactSlice<T, N>

Converts this type into the (usually inferred) input type.
source§

impl<'a, T, const N: usize> Into<&'a mut MinSlice<T, N>> for &'a mut ExactSlice<T, N>

source§

fn into(self) -> &'a mut MinSlice<T, N>

Converts this type into the (usually inferred) input type.

Auto Trait Implementations§

§

impl<T, const N: usize> Freeze for MinSlice<T, N>
where T: Freeze,

§

impl<T, const N: usize> RefUnwindSafe for MinSlice<T, N>
where T: RefUnwindSafe,

§

impl<T, const N: usize> Send for MinSlice<T, N>
where T: Send,

§

impl<T, const N: usize> !Sized for MinSlice<T, N>

§

impl<T, const N: usize> Sync for MinSlice<T, N>
where T: Sync,

§

impl<T, const N: usize> Unpin for MinSlice<T, N>
where T: Unpin,

§

impl<T, const N: usize> UnwindSafe for MinSlice<T, N>
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