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>

source

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.

source

pub fn new(slice: [T; N]) -> Result<Self, [T; N]>

Attempts to create a new InlineSSlice from a fixed-size array.

Errors

If the provided array does not contain a sentinel value, it is returned as an error.

Methods from Deref<Target = SSlice<T>>§

source

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.

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 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 fn as_std_cstr(&self) -> &CStr

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

source

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, const N: usize> AsMut<[T]> for InlineSSlice<T, N>
where T: Sentinel,

source§

fn as_mut(&mut self) -> &mut [T]

Converts this type into a mutable reference of the (usually inferred) input type.
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, const N: usize> AsRef<[T]> for InlineSSlice<T, N>
where T: Sentinel,

source§

fn as_ref(&self) -> &[T]

Converts this type into a shared 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: Clone + Sentinel, const N: usize> Clone for InlineSSlice<T, N>

source§

fn clone(&self) -> InlineSSlice<T, N>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: Debug + Sentinel, const N: usize> Debug for InlineSSlice<T, N>

source§

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

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

impl<T: Sentinel, const N: usize> Deref for InlineSSlice<T, N>

§

type Target = SSlice<T>

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl<T: Sentinel, const N: usize> DerefMut for InlineSSlice<T, N>

source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
source§

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

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method 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, const M: usize> PartialEq<InlineSSlice<U, M>> for InlineSSlice<T, N>
where T: PartialEq<U> + Sentinel, U: Sentinel,

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method 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 [T]
where T: PartialEq<U> + Sentinel, U: Sentinel,

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method 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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method 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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T: Copy + Sentinel, const N: usize> Copy for InlineSSlice<T, N>

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

impl<T, const N: usize> UnwindSafe for InlineSSlice<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
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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

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>,

§

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.