Struct Nul

Source
#[repr(C)]
pub struct Nul<A>(/* private fields */);
Expand description

Generic unsized null-terminated array

&Nul<A> is a thin pointer, so it can be readily used with FFI.

§Examples

One can safely take views of null-terminated slices with TryFrom::try_from:

extern "C" {
    fn c_f(path: *const u8) -> i32;
}

fn f(path: &[u8]) -> Result<i32, ()> {
    <&Nul<u8> as ::std::convert::TryFrom<_>>::try_from(path)
        .map(|path| unsafe { c_f(path.as_ptr()) })
}

Implementations§

Source§

impl<A> Nul<A>

Source

pub const fn as_ptr(&self) -> *const A

Return a pointer to the start of the array.

Source

pub fn as_mut_ptr(&mut self) -> *mut A

Return a mutable pointer to the start of the array.

Source

pub const fn iter(&self) -> Iter<'_, A>

Iterate over array elements.

Source

pub fn iter_mut(&mut self) -> IterMut<'_, A>

Iterate over array elements, mutably.

Source

pub const unsafe fn new_unchecked(p: *const A) -> &'static Self

Create a reference to a null-terminated array, given a pointer to its start.

The caller must make sure the argument does, in fact, point to a null-terminated array, and the returned reference not live longer than the array it refers to. These requirements are not checked.

Source

pub unsafe fn new_unchecked_mut(p: *mut A) -> &'static mut Self

Create a mutable reference to a null-terminated array, given a pointer to its start.

The caller must make sure the argument does, in fact, point to a null-terminated array, and the returned reference not live longer than the array it refers to. These requirements are not checked.

Source

pub fn len(&self) -> usize

Return array length. O(n)

Source

pub fn get(&self, i: usize) -> Option<&A>

Get element at given position. O(n) to check bounds

Source

pub fn get_mut(&mut self, i: usize) -> Option<&mut A>

Get element at given position, mutably. O(n) to check bounds

Source

pub fn split_at(&self, i: usize) -> (&[A], &Self)

Split array at given position.

§Panics

Panics if index out of bounds.

Source

pub fn split_at_mut(&mut self, i: usize) -> (&mut [A], &mut Self)

Split array at given position, mutably.

§Panics

Panics if index out of bounds.

Source

pub fn try_split_at(&self, i: usize) -> Option<(&[A], &Self)>

Split array at given position; return None if index out of bounds.

Source

pub fn try_split_at_mut(&mut self, i: usize) -> Option<(&mut [A], &mut Self)>

Split array at given position, mutably; return None if index out of bounds.

Trait Implementations§

Source§

impl<A> AsRef<Nul<A>> for Nul<A>

Source§

fn as_ref(&self) -> &Self

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

impl<A: Debug> Debug for Nul<A>

Source§

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

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

impl Display for Nul<char>

Source§

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

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

impl<'a, A> From<Iter<'a, A>> for &'a Nul<A>

Source§

fn from(it: Iter<'a, A>) -> Self

Converts to this type from the input type.
Source§

impl<'a, A> From<IterMut<'a, A>> for &'a mut Nul<A>

Source§

fn from(it: IterMut<'a, A>) -> Self

Converts to this type from the input type.
Source§

impl<A: Hash> Hash for Nul<A>

Source§

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

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

impl<A, I> Index<I> for Nul<A>
where [A]: Index<I>,

Source§

type Output = <[A] as Index<I>>::Output

The returned type after indexing.
Source§

fn index(&self, i: I) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<A, I> IndexMut<I> for Nul<A>
where [A]: IndexMut<I>,

Source§

fn index_mut(&mut self, i: I) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<'a, A> IntoIterator for &'a Nul<A>

Source§

type Item = &'a A

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, A>

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

fn into_iter(self) -> Iter<'a, A>

Creates an iterator from a value. Read more
Source§

impl<'a, A> IntoIterator for &'a mut Nul<A>

Source§

type Item = &'a mut A

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, A>

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

fn into_iter(self) -> IterMut<'a, A>

Creates an iterator from a value. Read more
Source§

impl<A: Ord> Ord for Nul<A>

Source§

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

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

impl<A: PartialEq> PartialEq for Nul<A>

Source§

fn eq(&self, other: &Self) -> 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<A: PartialOrd> PartialOrd for Nul<A>

Source§

fn partial_cmp(&self, other: &Self) -> 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<'a, A> TryFrom<&'a [A]> for &'a Nul<A>

Source§

type Error = ()

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

fn try_from(xs: &'a [A]) -> Result<Self, ()>

Performs the conversion.
Source§

impl<'a> TryFrom<&'a Nul<u8>> for &'a NulStr

Source§

type Error = Utf8Error

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

fn try_from(s: &'a Nul<u8>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'a, A> TryFrom<&'a mut [A]> for &'a mut Nul<A>

Source§

type Error = ()

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

fn try_from(xs: &'a mut [A]) -> Result<Self, ()>

Performs the conversion.
Source§

impl<'a> TryFrom<&'a mut Nul<u8>> for &'a mut NulStr

Source§

type Error = Utf8Error

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

fn try_from(s: &'a mut Nul<u8>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<A: Eq> Eq for Nul<A>

Auto Trait Implementations§

§

impl<A> !Freeze for Nul<A>

§

impl<A> !RefUnwindSafe for Nul<A>

§

impl<A> Send for Nul<A>
where A: Send,

§

impl<A> !Sized for Nul<A>

§

impl<A> Sync for Nul<A>
where A: Sync,

§

impl<A> !Unpin for Nul<A>

§

impl<A> !UnwindSafe for Nul<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