Struct SSlice

Source
pub struct SSlice<T>
where T: Sentinel,
{ /* private fields */ }
Expand description

A sentinel-terminated slice.

Implementations§

Source§

impl<T: Sentinel> SSlice<T>

Source

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).

Source

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).

Source

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");
Source

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");
Source

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]);
Source

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]);
Source

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.

Source

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.

Source

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);
Source

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");
Source

pub unsafe fn get_unchecked<Idx>(&self, index: Idx) -> &Idx::Output
where Idx: SliceIndex<T>,

Indexes into this SSlice<T> instance without checking the bounds.

§Safety

index must be in bounds.

Source

pub unsafe fn get_unchecked_mut<Idx>(&mut self, index: Idx) -> &mut Idx::Output
where Idx: SliceIndex<T>,

Indexes into this SSlice<T> instance without checking the bounds.

§Safety

index must be in bounds.

Source

pub fn len(&self) -> usize

Returns the length of the SSlice<T>. This is the number of elements referenced by that instance, not including the terminating sentinel character.

§Examples
use sentinel::SSlice;

let sslice = SSlice::<u8>::from_slice(b"Hello\0World").unwrap();
assert_eq!(sslice.len(), 5);
Source

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());
Source

pub fn first(&self) -> Option<&T>

Returns the first element of the slice, or None if it is empty.

§Examples
use sentinel::SSlice;

assert_eq!(SSlice::<u8>::from_slice(b"123\0").unwrap().first(), Some(&b'1'));
assert_eq!(SSlice::<u8>::from_slice(b"\0").unwrap().first(), None);
Source

pub fn first_mut(&mut self) -> Option<&mut T>

Returns the first element of the slice, or None if it is empty.

§Examples
use sentinel::SSlice;

let mut array = [1, 2, 3, 0];
let mut sslice = SSlice::<u8>::from_slice_mut(&mut array).unwrap();

*sslice.first_mut().unwrap() = 0;

assert_eq!(sslice.first_mut(), None);
Source

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");
Source

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());
Source

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).

Source

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).

Source

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

Returns a slice referencing every element of this SSlice<T>, not including the terminating sentinel character.

Source

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.

Source

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

Returns a slice referencing every element of this SSlice<T>, including the terminating sentinel character.

Source

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.

Source

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 Ts.

Source

pub const unsafe fn cast_to_slice_of_sslices<'a>( slice: &[*const T], ) -> &[&'a Self]

Casts a slice of *const Ts 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>

Source

pub const fn from_std_cstr(cstr: &CStr) -> &Self

Creates a new SSlice<T> from the provided standard core::ffi::CStr.

Source

pub fn as_std_cstr(&self) -> &CStr

Turns this SSlice<T> into a standard core::ffi::CStr.

Source

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_chars.

Source

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.

Trait Implementations§

Source§

impl<T, const N: usize> AsMut<SSlice<T>> for InlineSSlice<T, N>
where T: Sentinel,

Source§

fn as_mut(&mut self) -> &mut SSlice<T>

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl<T: Sentinel, A: Allocator> AsMut<SSlice<T>> for SBox<T, A>

Source§

fn as_mut(&mut self) -> &mut SSlice<T>

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl<T, const N: usize> AsRef<SSlice<T>> for InlineSSlice<T, N>
where T: Sentinel,

Source§

fn as_ref(&self) -> &SSlice<T>

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<T: Sentinel, A: Allocator> AsRef<SSlice<T>> for SBox<T, A>

Source§

fn as_ref(&self) -> &SSlice<T>

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<T: Sentinel, A: Allocator> Borrow<SSlice<T>> for SBox<T, A>

Source§

fn borrow(&self) -> &SSlice<T>

Immutably borrows from an owned value. Read more
Source§

impl<T: Sentinel, A: Allocator> BorrowMut<SSlice<T>> for SBox<T, A>

Source§

fn borrow_mut(&mut self) -> &mut SSlice<T>

Mutably borrows from an owned value. Read more
Source§

impl<T: Debug + Sentinel> Debug for SSlice<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Sentinel> Drop for SSlice<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T: Sentinel + Hash> Hash for SSlice<T>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<'a, T: Sentinel> IntoIterator for &'a SSlice<T>

Source§

type IntoIter = &'a Iter<T>

Which kind of iterator are we turning this into?
Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, T: Sentinel> IntoIterator for &'a mut SSlice<T>

Source§

type IntoIter = &'a mut Iter<T>

Which kind of iterator are we turning this into?
Source§

type Item = &'a mut T

The type of the elements being iterated over.
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T: Ord + Sentinel> Ord for SSlice<T>

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T1, T2> PartialEq<[T2]> for SSlice<T1>
where T1: PartialEq<T2> + Sentinel,

Source§

fn eq(&self, other: &[T2]) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T1, T2, const N: usize> PartialEq<[T2; N]> for SSlice<T1>
where T1: PartialEq<T2> + Sentinel,

Source§

fn eq(&self, other: &[T2; N]) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T, U, const N: usize> PartialEq<InlineSSlice<U, N>> for SSlice<T>
where T: PartialEq<U> + Sentinel, U: Sentinel,

Source§

fn eq(&self, other: &InlineSSlice<U, N>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T, T2, A> PartialEq<SBox<T, A>> for SSlice<T2>
where T: Sentinel, T2: Sentinel + PartialEq<T>, A: Allocator,

Source§

fn eq(&self, other: &SBox<T, A>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T1, T2> PartialEq<SSlice<T2>> for [T1]
where T1: PartialEq<T2>, T2: Sentinel,

Source§

fn eq(&self, other: &SSlice<T2>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T1, T2, const N: usize> PartialEq<SSlice<T2>> for [T1; N]
where T1: PartialEq<T2>, T2: Sentinel,

Source§

fn eq(&self, other: &SSlice<T2>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T1, T2> PartialEq<SSlice<T2>> for SSlice<T1>
where T1: PartialEq<T2> + Sentinel, T2: Sentinel,

Source§

fn eq(&self, other: &SSlice<T2>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T, U, const N: usize> PartialEq<SSlice<U>> for InlineSSlice<T, N>
where T: PartialEq<U> + Sentinel, U: Sentinel,

Source§

fn eq(&self, other: &SSlice<U>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T1, T2> PartialOrd<[T2]> for SSlice<T1>
where T1: PartialOrd<T2> + Sentinel,

Source§

fn partial_cmp(&self, other: &[T2]) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T1, T2, const N: usize> PartialOrd<[T2; N]> for SSlice<T1>
where T1: PartialOrd<T2> + Sentinel,

Source§

fn partial_cmp(&self, other: &[T2; N]) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T, T2, A> PartialOrd<SBox<T, A>> for SSlice<T2>
where T: Sentinel, T2: Sentinel + PartialOrd<T>, A: Allocator,

Source§

fn partial_cmp(&self, other: &SBox<T, A>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T1, T2> PartialOrd<SSlice<T2>> for [T1]
where T1: PartialOrd<T2>, T2: Sentinel,

Source§

fn partial_cmp(&self, other: &SSlice<T2>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T1, T2, const N: usize> PartialOrd<SSlice<T2>> for [T1; N]
where T1: PartialOrd<T2>, T2: Sentinel,

Source§

fn partial_cmp(&self, other: &SSlice<T2>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T1, T2> PartialOrd<SSlice<T2>> for SSlice<T1>
where T1: PartialOrd<T2> + Sentinel, T2: Sentinel,

Source§

fn partial_cmp(&self, other: &SSlice<T2>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T: Eq + Sentinel> Eq for SSlice<T>

Source§

impl<T: RefUnwindSafe + Sentinel> RefUnwindSafe for SSlice<T>

Source§

impl<T: Send + Sentinel> Send for SSlice<T>

Source§

impl<T: Sync + Sentinel> Sync for SSlice<T>

Source§

impl<T: Unpin + Sentinel> Unpin for SSlice<T>

Source§

impl<T: UnwindSafe + Sentinel> UnwindSafe for SSlice<T>

Auto Trait Implementations§

§

impl<T> Freeze for SSlice<T>

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
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.