Struct sentinel::InlineSSlice
source · pub struct InlineSSlice<T: Sentinel, const N: usize>(/* private fields */);
Expand description
A sentinel-terminated slice that’s backed by a fixed-size array.
Implementations§
source§impl<T: Sentinel, const N: usize> InlineSSlice<T, N>
impl<T: Sentinel, const N: usize> InlineSSlice<T, N>
sourcepub const unsafe fn new_unchecked(slice: [T; N]) -> Self
pub const unsafe fn new_unchecked(slice: [T; N]) -> Self
Creates a new InlineSSlice
from a fixed-size array.
Safety
slice
must contain a sentinel value somewhere in it.
Methods from Deref<Target = SSlice<T>>§
sourcepub fn as_ptr(&self) -> *const T
pub fn as_ptr(&self) -> *const T
Returns a pointer to the first element that is part of the slice.
Notes
This pointer is always valid and will reference an initialized instance of T
. Note,
however, that this value cannot be modified (even if it supports interior mutability). Or,
rather, if it is a sentinel, it must remain a sentinel.
sourcepub fn as_mut_ptr(&mut self) -> *mut T
pub fn as_mut_ptr(&mut self) -> *mut T
Returns a pointer to the first element that is part of the slice.
Notes
This pointer is always valid and will reference an initialized instance of T
. Note,
however, that this value cannot be modified. Or, rather, if it is a sentinel, it must
remain a sentinel.
sourcepub fn iter(&self) -> &Iter<T> ⓘ
pub fn iter(&self) -> &Iter<T> ⓘ
Returns an iterator over the elements of the slice.
Examples
let s = sentinel::cstr!("Hello!");
let mut iter = s.iter();
assert_eq!(iter.next(), Some(&b'H'));
assert_eq!(iter.next(), Some(&b'e'));
assert_eq!(iter.next(), Some(&b'l'));
assert_eq!(iter.next(), Some(&b'l'));
assert_eq!(iter.next(), Some(&b'o'));
assert_eq!(iter.next(), Some(&b'!'));
assert_eq!(iter.next(), None);
sourcepub fn iter_mut(&mut self) -> &mut Iter<T> ⓘ
pub fn iter_mut(&mut self) -> &mut Iter<T> ⓘ
Returns an iterator over the elements of the slice.
Examples
let mut array = *b"abc\0";
let mut sslice = sentinel::SSlice::<u8>::from_slice_mut(&mut array).unwrap();
let mut iter = sslice.iter_mut();
*iter.next().unwrap() = b'1';
*iter.next().unwrap() = b'2';
*iter.next().unwrap() = b'3';
assert_eq!(sslice, b"123");
sourcepub unsafe fn get_unchecked<Idx>(&self, index: Idx) -> &Idx::Outputwhere
Idx: SliceIndex<T>,
pub unsafe fn get_unchecked<Idx>(&self, index: Idx) -> &Idx::Outputwhere
Idx: SliceIndex<T>,
sourcepub unsafe fn get_unchecked_mut<Idx>(&mut self, index: Idx) -> &mut Idx::Outputwhere
Idx: SliceIndex<T>,
pub unsafe fn get_unchecked_mut<Idx>(&mut self, index: Idx) -> &mut Idx::Outputwhere
Idx: SliceIndex<T>,
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns whether the slice is currently empty.
Examples
use sentinel::SSlice;
assert!(!SSlice::<u8>::from_slice(b"123\0").unwrap().is_empty());
assert!(SSlice::<u8>::from_slice(b"\0").unwrap().is_empty());
sourcepub fn split_first(&self) -> Option<(&T, &Self)>
pub fn split_first(&self) -> Option<(&T, &Self)>
Returns a pointer to the first element of the slice, and a slice to the remaining elements.
None
is returned if the slice is empty.
Examples
use sentinel::SSlice;
let sslice = SSlice::<u8>::from_slice(b"1234\0").unwrap();
let (first, remainder) = sslice.split_first().unwrap();
assert_eq!(*first, '1' as u8);
assert_eq!(remainder, b"234");
sourcepub fn split_first_mut(&mut self) -> Option<(&mut T, &mut Self)>
pub fn split_first_mut(&mut self) -> Option<(&mut T, &mut Self)>
Returns a pointer to the first element of the slice, and a slice to the remaining elements.
None
is returned if the slice is empty.
Examples
use sentinel::SSlice;
let mut array = *b"1234\0";
let mut sslice = SSlice::<u8>::from_slice_mut(&mut array).unwrap();
let (first, remainder) = sslice.split_first_mut().unwrap();
assert_eq!(*first, '1' as u8);
assert_eq!(remainder, b"234");
*first = 0;
assert!(sslice.is_empty());
sourcepub unsafe fn raw_first(&self) -> &T
pub unsafe fn raw_first(&self) -> &T
Returns a shared reference to the first element of the slice, or a sentinel value if the slice is empty.
Safety
If the returned value is a sentinel, it must not be modified (or rather, it must remain a sentinel).
sourcepub unsafe fn raw_first_mut(&mut self) -> &mut T
pub unsafe fn raw_first_mut(&mut self) -> &mut T
Returns an exclusive reference to the first element of the slice, or a sentinel value if the slice is empty.
Safety
If the returned value is a sentinel, it must not be modified (or rather, it must remain a sentinel).
sourcepub fn as_slice(&self) -> &[T]
pub fn as_slice(&self) -> &[T]
Returns a slice referencing every element of this SSlice<T>
, not including the
terminating sentinel character.
sourcepub fn as_slice_mut(&mut self) -> &mut [T]
pub fn as_slice_mut(&mut self) -> &mut [T]
Returns a slice referencing every element of this SSlice<T>
, not including the
terminating sentinel character.
sourcepub fn as_slice_with_sentinel(&self) -> &[T]
pub fn as_slice_with_sentinel(&self) -> &[T]
Returns a slice referencing every element of this SSlice<T>
, including the
terminating sentinel character.
sourcepub fn as_slice_with_sentinel_mut(&mut self) -> &mut [T]
pub fn as_slice_with_sentinel_mut(&mut self) -> &mut [T]
Returns a slice referencing every element of this SSlice<T>
, including the
terminating sentinel character.
sourcepub fn as_std_cstr(&self) -> &CStr
pub fn as_std_cstr(&self) -> &CStr
Turns this SSlice<T>
into a standard core::ffi::CStr
.
sourcepub fn display(&self) -> &Display
pub fn display(&self) -> &Display
An implementation of fmt::Display
and fmt::Debug
for the CStr
type.
When an invalid character is found, the REPLACEMENT_CHARACTER
is displayed instead.
Trait Implementations§
source§impl<T: Clone + Sentinel, const N: usize> Clone for InlineSSlice<T, N>
impl<T: Clone + Sentinel, const N: usize> Clone for InlineSSlice<T, N>
source§fn clone(&self) -> InlineSSlice<T, N>
fn clone(&self) -> InlineSSlice<T, N>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<T, U, const N: usize, const M: usize> PartialEq<InlineSSlice<U, M>> for InlineSSlice<T, N>
impl<T, U, const N: usize, const M: usize> PartialEq<InlineSSlice<U, M>> for InlineSSlice<T, N>
source§fn eq(&self, other: &InlineSSlice<U, M>) -> bool
fn eq(&self, other: &InlineSSlice<U, M>) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl<T, U, const N: usize> PartialEq<InlineSSlice<U, N>> for [T]
impl<T, U, const N: usize> PartialEq<InlineSSlice<U, N>> for [T]
source§fn eq(&self, other: &InlineSSlice<U, N>) -> bool
fn eq(&self, other: &InlineSSlice<U, N>) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl<T, U, const N: usize> PartialEq<InlineSSlice<U, N>> for SSlice<T>
impl<T, U, const N: usize> PartialEq<InlineSSlice<U, N>> for SSlice<T>
source§fn eq(&self, other: &InlineSSlice<U, N>) -> bool
fn eq(&self, other: &InlineSSlice<U, N>) -> bool
self
and other
values to be equal, and is used
by ==
.