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
T
s.
tail: [T]
Zero or more remaining T
s after the N
in the bounded region.
Implementations§
source§impl<T, const N: usize> MinSlice<T, N>
impl<T, const N: usize> MinSlice<T, N>
sourcepub fn from_slice(slice: &[T]) -> Option<&MinSlice<T, N>>
pub fn from_slice(slice: &[T]) -> Option<&MinSlice<T, N>>
Produce a &MinSlice
from a slice of T
s.
Returns None
if there are not enough elements in slice
.
source§impl<T, const N: usize> MinSlice<T, N>
impl<T, const N: usize> MinSlice<T, N>
sourcepub fn as_slice(&self) -> &[T]
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
.
sourcepub fn as_mut_slice(&mut self) -> &mut [T]
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
.
sourcepub fn resize<const M: usize>(&self) -> Option<&MinSlice<T, M>>
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
T
s in self
.
sourcepub fn resize_mut<const M: usize>(&mut self) -> Option<&mut MinSlice<T, M>>
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
T
s in self
.
sourcepub fn get(&self, i: usize) -> Option<&T>
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.
sourcepub fn get_mut(&mut self, i: usize) -> Option<&mut T>
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.
sourcepub unsafe fn from_slice_unchecked(slice: &[T]) -> &MinSlice<T, N>
pub unsafe fn from_slice_unchecked(slice: &[T]) -> &MinSlice<T, N>
Produce a &MinSlice
from a slice of T
s without checking its length.
§Safety
The caller is responsible for upholding the length invariant
slice.len() >= N
, in addition to all normal slice invariants.
sourcepub unsafe fn from_mut_unchecked(slice: &mut [T]) -> &mut MinSlice<T, N>
pub unsafe fn from_mut_unchecked(slice: &mut [T]) -> &mut MinSlice<T, N>
Produce a &mut MinSlice
from a slice of T
s without checking its length.
§Safety
The caller is responsible for upholding the length invariant
slice.len() >= N
, in addition to all normal slice invariants.