hylarana_common::strings

Struct PSTR

Source
pub struct PSTR { /* private fields */ }
Expand description

A type representing an owned, C-compatible, nul-terminated string with no nul bytes in the middle.

This type serves the purpose of being able to safely generate a C-compatible string from a Rust byte slice or vector. An instance of this type is a static guarantee that the underlying bytes contain no interior 0 bytes (“nul characters”) and that the final byte is 0 (“nul terminator”).

CString is to &CStr as String is to &str: the former in each pair are owned strings; the latter are borrowed references.

Implementations§

Source§

impl PSTR

Source

pub fn to_string(&self) -> Result<String, StringError>

Yields a &str slice if the CStr contains valid UTF-8.

If the contents of the CStr are valid UTF-8 data, this function will return the corresponding &str slice. Otherwise, it will return an error with details of where UTF-8 validation failed.

Source

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

Returns the inner pointer to this C string.

The returned pointer will be valid for as long as self is, and points to a contiguous region of memory terminated with a 0 byte to represent the end of the string.

The type of the returned pointer is *const c_char, and whether it’s an alias for *const i8 or *const u8 is platform-specific.

§WARNING

The returned pointer is read-only; writing to it (including passing it to C code that writes to it) causes undefined behavior.

Source

pub fn strcpy(src: &str, dest: *mut c_char)

Copy the string pointed to by src to dest.

The behavior of this function is essentially the same as the behavior of strcpy in the C standard library.

Trait Implementations§

Source§

impl Drop for PSTR

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl From<&str> for PSTR

Source§

fn from(value: &str) -> Self

Converts to this type from the input type.
Source§

impl From<*const i8> for PSTR

Source§

fn from(ptr: *const c_char) -> Self

Converts to this type from the input type.
Source§

impl From<String> for PSTR

Source§

fn from(value: String) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl Freeze for PSTR

§

impl RefUnwindSafe for PSTR

§

impl !Send for PSTR

§

impl !Sync for PSTR

§

impl Unpin for PSTR

§

impl UnwindSafe for PSTR

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.