Skip to main content

CStrNonNull

Struct CStrNonNull 

Source
pub struct CStrNonNull<'s> { /* private fields */ }
Expand description

Option<CStrNonNull> is ABI compatible with *const c_char.

If you want to treat null() as "", use CStrPtr instead.

Implementations§

Source§

impl<'s> CStrNonNull<'s>

Source

pub unsafe fn from_ptr_unchecked_unbounded(ptr: *const c_char) -> Self

Convert a raw C-string into a CStrPtr. Note that the lifetime of the returned reference is unbounded!

§Safety
  • ptr cannot be null
  • ptr must point to a \0-terminated C string
  • The underlying C-string cannot change for the duration of the lifetime 's.
  • The lifetime 's is unbounded by this fn. Very easy to accidentally extend. Be careful!
Source

pub fn from_bytes_with_nul( bytes: &'s [u8], ) -> Result<Self, FromBytesWithNulError>

Convert a raw slice of bytes into a CStrNonNull. bytes should end with \0, but contain no interior \0s otherwise.

Source

pub unsafe fn from_bytes_with_nul_unchecked(bytes: &'s [u8]) -> Self

Convert a raw slice of bytes to a CStrNonNull. The resulting string will be terminated at the first \0 in bytes.

§Safety
  • bytes must contain at least one \0.
Source

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

Treat self as a raw C string.

Source

pub fn as_non_null(&self) -> NonNull<c_char>

Treat self as a NonNull C string.

Source

pub fn is_empty(&self) -> bool

Checks if self is empty (either null(), or the first character is \0.)

Source

pub fn to_bytes(&self) -> &'s [u8]

Convert self to a &[u8] slice, excluding the terminal \0.

O(n) to find the terminal \0.

Source

pub fn to_bytes_with_nul(&self) -> &'s [u8]

Convert self to a &[u8] slice, including the terminal \0.

O(n) to find the terminal \0.

Source

pub fn to_cstr(&self) -> &'s CStr

Convert self to a std::ffi::CStr.

O(n) to find the terminal \0.

Source

pub fn to_str(&self) -> Result<&'s str, Utf8Error>

Convert self to a &str.

O(n) to find the terminal \0 and validate UTF8.

Source

pub fn to_string_lossy(&self) -> Cow<'s, str>

Convert self to a Cow<str>.

O(n) to find the terminal \0 and validate, and to convert UTF8ish data to UTF8 if necesssary.

Trait Implementations§

Source§

impl AsCStr for CStrNonNull<'_>

Source§

fn as_cstr(&self) -> *const c_char

Returns a \0-terminated C string
Source§

impl<'s> Clone for CStrNonNull<'s>

Source§

fn clone(&self) -> CStrNonNull<'s>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<'s> Copy for CStrNonNull<'s>

Source§

impl Debug for CStrNonNull<'_>

Source§

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

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

impl Default for CStrNonNull<'_>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'s> From<&'s CStr> for CStrNonNull<'s>

Source§

fn from(s: &'s CStr) -> Self

Converts to this type from the input type.
Source§

impl<'s> From<CStrNonNull<'s>> for &'s CStr

Source§

fn from(s: CStrNonNull<'s>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<'s> !Send for CStrNonNull<'s>

§

impl<'s> !Sync for CStrNonNull<'s>

§

impl<'s> Freeze for CStrNonNull<'s>

§

impl<'s> RefUnwindSafe for CStrNonNull<'s>

§

impl<'s> Unpin for CStrNonNull<'s>

§

impl<'s> UnsafeUnpin for CStrNonNull<'s>

§

impl<'s> UnwindSafe for CStrNonNull<'s>

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> AsOptCStr for T
where T: AsCStr,

Source§

fn as_opt_cstr(&self) -> *const i8

Returns a \0-terminated C string, or null().
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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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,

Source§

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

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

impl<T> TryIntoAsCStr for T
where T: AsCStr,

Source§

type Target = T

The temporary type that can be treated as a C-string.
Source§

fn try_into(self) -> Result<<T as TryIntoAsCStr>::Target, NulError>

Attempt to convert to Self::Target. May fail if self contains \0s.
Source§

impl<T> TryIntoAsOptCStr for T
where T: AsOptCStr,

Source§

type Target = T

The temporary type that can be treated as an Optional C-string.
Source§

fn try_into(self) -> Result<<T as TryIntoAsOptCStr>::Target, NulError>

Attempt to convert to Self::Target. May fail if self contains \0s.