Struct sentinel::SBox

source ·
pub struct SBox<T: Sentinel, A: Allocator = Global> { /* private fields */ }
Expand description

An allocated SSlice<T> instance.

Implementations§

source§

impl<T: Sentinel, A: Allocator> SBox<T, A>

source

pub fn from_sslice_in( slice: &SSlice<T>, allocator: A ) -> Result<Self, AllocError>
where T: Clone,

Clones the content of slice into a SBox<T>.

source

pub unsafe fn from_slice_unchecked_in( slice: &[T], allocator: A ) -> Result<Self, AllocError>
where T: Clone,

Creates a new SBox<T> from the provided slice.

Safety

slice must end with a sentinel value. Apart from this one, it must contain no sentinel values.

source

pub fn from_slice_in(slice: &[T], allocator: A) -> Result<Self, AllocError>
where T: Clone,

Creates a new SBox<T, A> from the provided slice.

If the slice contains a sentinel value, the procuded SBox<T> will only clone the values up to that point. If the slice contains no sentinel value, a default sentinel value will be used instead (see [DefaultSentinel]).

source

pub fn into_raw_parts(b: SBox<T, A>) -> (*mut T, A)

Turns the provided SBox<T> into its raw representation.

The returned pointer can be used to re-construct a box using from_raw_parts.

source

pub unsafe fn from_raw_parts(data: *mut T, allocator: A) -> Self

Constructs a new SBox<T> using the provided data pointer and allocator.

Safety

The provided pointer must reference the first element of a SSlice<T> instance allocated by allocator.

source§

impl<T: Sentinel> SBox<T, Global>

source

pub fn from_sslice(slice: &SSlice<T>) -> Result<Self, AllocError>
where T: Clone,

Clones the content of slice into a SBox<T>.

source

pub fn from_slice(slice: &[T]) -> Result<Self, AllocError>
where T: Clone,

Clones the content of slice into a SBox<T>.

If the provided slice contains a sentinel value, the produced box will contain values up to that point. Otherwise, a default sentinel value will be used (see [DefaultSentinel]).

source

pub unsafe fn from_box_unchecked(b: Box<[T]>) -> Self

Creates a SBox<T> from the provided allocated box.

Safety

The last element of the slice must be a sentinel, and other elements must not.

source

pub fn from_box(b: Box<[T]>) -> Option<Self>

Creates an SBox<T> from the provided allocated box.

If the box does not end with a sentinel value, or if it contains a sentinel value in non-ending position, None is returned.

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: 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: 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, A> Clone for SBox<T, A>
where T: Clone + Sentinel, A: Allocator + Clone,

source§

fn clone(&self) -> Self

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: Sentinel, A: Allocator> Deref for SBox<T, A>

§

type Target = SSlice<T>

The resulting type after dereferencing.
source§

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

Dereferences the value.
source§

impl<T: Sentinel, A: Allocator> DerefMut for SBox<T, A>

source§

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

Mutably dereferences the value.
source§

impl<T: Sentinel, A: Allocator> Drop for SBox<T, A>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<T: Sentinel> FromIterator<T> for SBox<T, Global>

source§

fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self

Creates a new SBox<T> from the values returned by the provided iterator. If a sentinel value is returned, it is ignored.

source§

impl<T, A> Ord for SBox<T, A>
where A: Allocator, T: Ord + Sentinel,

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

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

impl<T, T2, A> PartialEq<SBox<T, A>> for [T2]
where T: Sentinel, A: Allocator, T2: PartialEq<T>,

source§

fn eq(&self, other: &SBox<T, A>) -> 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, T2, A, const N: usize> PartialEq<SBox<T, A>> for [T2; N]
where T: Sentinel, A: Allocator, T2: PartialEq<T>,

source§

fn eq(&self, other: &SBox<T, A>) -> 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, 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

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, A> PartialEq<U> for SBox<T, A>
where A: Allocator, T: Sentinel, SSlice<T>: PartialEq<U>,

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, T2, A> PartialOrd<SBox<T, A>> for [T2]
where T: Sentinel, A: Allocator, T2: PartialOrd<T>,

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

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

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

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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<T, T2, A, const N: usize> PartialOrd<SBox<T, A>> for [T2; N]
where T: Sentinel, A: Allocator, T2: PartialOrd<T>,

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

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

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

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

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

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

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

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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

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

source§

fn partial_cmp(&self, other: &U) -> 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

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

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

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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<T, A> Eq for SBox<T, A>
where A: Allocator, T: Eq + Sentinel,

Auto Trait Implementations§

§

impl<T, A> RefUnwindSafe for SBox<T, A>

§

impl<T, A = Global> !Send for SBox<T, A>

§

impl<T, A = Global> !Sync for SBox<T, A>

§

impl<T, A> Unpin for SBox<T, A>
where A: Unpin,

§

impl<T, A> UnwindSafe for SBox<T, A>

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.