pub struct SSlice<T>where
T: Sentinel,{ /* private fields */ }
Expand description
A sentinel-terminated slice.
Implementations§
Source§impl<T: Sentinel> SSlice<T>
impl<T: Sentinel> SSlice<T>
Sourcepub const unsafe fn from_ptr<'a>(ptr: *const T) -> &'a Self
pub const unsafe fn from_ptr<'a>(ptr: *const T) -> &'a Self
Creates a new SSlice<T>
instance from the provided pointer.
§Safety
The elements referenced by the provided pointer, until the first sentinel value, must be part of the same allocated object. They must be properly initialized and safe for reads.
This invariant must remain until the end of the lifetime 'a
(at least).
Sourcepub unsafe fn from_mut_ptr<'a>(ptr: *mut T) -> &'a mut Self
pub unsafe fn from_mut_ptr<'a>(ptr: *mut T) -> &'a mut Self
Creates a new SSlice<T>
instance from the provided pointer.
§Safety
The elements referenced by the provided pointer, until the first sentinel value, must be part of the same allocated object. They must be properly initialized and safe for reads and writes.
This invariant must remain until the end of the lifetime 'a
(at least).
Sourcepub fn from_slice_split(slice: &[T]) -> Option<(&Self, &[T])>
pub fn from_slice_split(slice: &[T]) -> Option<(&Self, &[T])>
Creates a SSlice<T>
instance from the provided slice.
If the slice contains a sentinel character, the function retuns a SSlice<T>
referencing the elements stored in T
up to (and including) the first sentinel
character. The remaining of the slice is returned as well.
Otherwise, the function returns None
§Examples
use sentinel::SSlice;
let input = b"abc\0def";
let (a, b) = SSlice::<u8>::from_slice_split(input).unwrap();
assert_eq!(a, b"abc");
assert_eq!(b, b"def");
Sourcepub fn from_slice(slice: &[T]) -> Option<&Self>
pub fn from_slice(slice: &[T]) -> Option<&Self>
Creates a SSlice<T>
instance from the provided slice.
If the slice contains a sentinel character, the function returns SSlice<T>
referencing the elements stored in T
up to (and including) the first sentinel
character. Otherwise, the function returns None
.
§Examples
use sentinel::SSlice;
let sslice = SSlice::<u8>::from_slice(b"abc\0def").unwrap();
assert_eq!(sslice, b"abc");
Sourcepub fn from_slice_split_mut(slice: &mut [T]) -> Option<(&mut Self, &mut [T])>
pub fn from_slice_split_mut(slice: &mut [T]) -> Option<(&mut Self, &mut [T])>
Creates a SSlice<T>
instance from the provided slice.
If the slice contains a sentinel character, the function retuns a SSlice<T>
referencing the elements stored in T
up to (and including) the first sentinel
character. The remaining of the slice is returned as well.
Otherwise, the function returns None
§Examples
use sentinel::SSlice;
let mut slice = [1, 2, 3, 0, 4, 5, 6];
let (sslice, remainder) = SSlice::<u8>::from_slice_split_mut(&mut slice).unwrap();
assert_eq!(sslice, &[1, 2, 3]);
assert_eq!(remainder, [4, 5, 6]);
Sourcepub fn from_slice_mut(slice: &mut [T]) -> Option<&mut Self>
pub fn from_slice_mut(slice: &mut [T]) -> Option<&mut Self>
Creates a SSlice<T>
instance from the provided slice.
If the slice contains a sentinel character, the function returns SSlice<T>
referencing the elements stored in T
up to (and including) the first sentinel
character. Otherwise, the function returns None
.
§Examples
use sentinel::SSlice;
let mut slice = [1, 2, 3, 0, 4, 5, 6];
let sslice = SSlice::<u8>::from_slice_mut(&mut slice).unwrap();
assert_eq!(sslice, &[1, 2, 3]);
Sourcepub const fn as_ptr(&self) -> *const T
pub const 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 const unsafe fn raw_first(&self) -> &T
pub const 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 const fn cast_to_slice_of_ptrs<'a>(slice: &'a [&Self]) -> &'a [*const T]
pub const fn cast_to_slice_of_ptrs<'a>(slice: &'a [&Self]) -> &'a [*const T]
Casts a slice of SSlice<T>
values into a slice of *const T
s.
Sourcepub const unsafe fn cast_to_slice_of_sslices<'a>(
slice: &[*const T],
) -> &[&'a Self]
pub const unsafe fn cast_to_slice_of_sslices<'a>( slice: &[*const T], ) -> &[&'a Self]
Casts a slice of *const T
s into a slice of SSlice<T>
values.
§Safety
It must be safe to call SSlice::from_ptr
for every pointer in the slice (for the lifetime
'a
).
Source§impl SSlice<u8>
impl SSlice<u8>
Sourcepub const fn from_std_cstr(cstr: &CStr) -> &Self
pub const fn from_std_cstr(cstr: &CStr) -> &Self
Creates a new SSlice<T>
from the provided standard core::ffi::CStr
.
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 const fn cast_to_slice_of_cstrs<'a>(
slice: &'a [&Self],
) -> &'a [*const c_char]
pub const fn cast_to_slice_of_cstrs<'a>( slice: &'a [&Self], ) -> &'a [*const c_char]
Casts a slice of CStr
values into a slice of *const c_char
s.
Sourcepub const fn display(&self) -> &Display
pub const 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.