[][src]Struct sgx_trts::c_str::CString

pub struct CString { /* fields omitted */ }

A type representing an owned C-compatible string

Methods

impl CString[src]

pub fn new<T: Into<Vec<u8>>>(t: T) -> Result<CString, NulError>[src]

Creates a new C-compatible string from a container of bytes.

This function will consume the provided data and use the underlying bytes to construct a new string, ensuring that there is a trailing 0 byte. This trailing 0 byte will be appended by this function; the provided data should not contain any 0 bytes in it.

pub unsafe fn from_vec_unchecked(v: Vec<u8>) -> CString[src]

Creates a C-compatible string by consuming a byte vector, without checking for interior 0 bytes.

This method is equivalent to [new] except that no runtime assertion is made that v contains no 0 bytes, and it requires an actual byte vector, not anything that can be converted to one with Into.

pub unsafe fn from_raw(ptr: *mut c_char) -> CString[src]

Retakes ownership of a CString that was transferred to C via [into_raw].

Additionally, the length of the string will be recalculated from the pointer.

Safety

This should only ever be called with a pointer that was earlier obtained by calling [into_raw] on a CString. Other usage (e.g. trying to take ownership of a string that was allocated by foreign code) is likely to lead to undefined behavior or allocator corruption.

Note: If you need to borrow a string that was allocated by foreign code, use CStr. If you need to take ownership of a string that was allocated by foreign code, you will need to make your own provisions for freeing it appropriately, likely with the foreign code's API to do that.

pub fn into_raw(self) -> *mut c_char[src]

Consumes the CString and transfers ownership of the string to a C caller.

The pointer which this function returns must be returned to Rust and reconstituted using [from_raw] to be properly deallocated. Specifically, one should not use the standard C free() function to deallocate this string.

Failure to call [from_raw] will lead to a memory leak.

pub fn into_string(self) -> Result<String, IntoStringError>[src]

Converts the CString into a String if it contains valid UTF-8 data.

On failure, ownership of the original CString is returned.

pub fn into_bytes(self) -> Vec<u8>[src]

Consumes the CString and returns the underlying byte buffer.

The returned buffer does not contain the trailing nul terminator, and it is guaranteed to not have any interior nul bytes.

pub fn into_bytes_with_nul(self) -> Vec<u8>[src]

Equivalent to the [into_bytes] function except that the returned vector includes the trailing nul terminator.

pub fn as_bytes(&self) -> &[u8][src]

Returns the contents of this CString as a slice of bytes.

The returned slice does not contain the trailing nul terminator, and it is guaranteed to not have any interior nul bytes. If you need the nul terminator, use [as_bytes_with_nul] instead.

pub fn as_bytes_with_nul(&self) -> &[u8][src]

Equivalent to the [as_bytes] function except that the returned slice includes the trailing nul terminator.

pub fn as_c_str(&self) -> &CStr[src]

Extracts a CStr slice containing the entire string.

pub fn into_boxed_c_str(self) -> Box<CStr>[src]

Converts this CString into a boxed CStr.

Methods from Deref<Target = CStr>

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

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.

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

Converts this C string to a byte slice.

The returned slice will not contain the trailing nul terminator that this C string has.

Note: This method is currently implemented as a constant-time cast, but it is planned to alter its definition in the future to perform the length calculation whenever this method is called.

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

Converts this C string to a byte slice containing the trailing 0 byte.

This function is the equivalent of to_bytes except that it will retain the trailing nul terminator instead of chopping it off.

Note: This method is currently implemented as a 0-cost cast, but it is planned to alter its definition in the future to perform the length calculation whenever this method is called.

pub fn to_str(&self) -> Result<&str, Utf8Error>[src]

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.

Note: This method is currently implemented to check for validity after a constant-time cast, but it is planned to alter its definition in the future to perform the length calculation in addition to the UTF-8 check whenever this method is called.

pub fn to_string_lossy(&self) -> Cow<str>[src]

Converts a CStr into a Cow<str>.

If the contents of the CStr are valid UTF-8 data, this function will return a Cow::Borrowed([&str]) with the corresponding [&str] slice. Otherwise, it will replace any invalid UTF-8 sequences with U+FFFD REPLACEMENT CHARACTER and return a Cow::Owned(String) with the result.

Note: This method is currently implemented to check for validity after a constant-time cast, but it is planned to alter its definition in the future to perform the length calculation in addition to the UTF-8 check whenever this method is called.

Trait Implementations

impl From<CString> for Vec<u8>[src]

fn from(s: CString) -> Vec<u8>[src]

Converts a CString into a Vec<u8>.

The conversion consumes the CString, and removes the terminating NUL byte.

impl<'a> From<Cow<'a, CStr>> for CString[src]

impl From<Box<CStr>> for CString[src]

fn from(s: Box<CStr>) -> CString[src]

Converts a Box<CStr> into a CString without copying or allocating.

impl From<CString> for Box<CStr>[src]

fn from(s: CString) -> Box<CStr>[src]

Converts a CString into a Box<CStr> without copying or allocating.

impl<'a> From<CString> for Cow<'a, CStr>[src]

impl<'a> From<&'a CString> for Cow<'a, CStr>[src]

impl From<CString> for Arc<CStr>[src]

fn from(s: CString) -> Arc<CStr>[src]

Converts a CString into a Arc<CStr> without copying or allocating.

impl From<CString> for Rc<CStr>[src]

fn from(s: CString) -> Rc<CStr>[src]

Converts a CString into a Rc<CStr> without copying or allocating.

impl<'_> From<&'_ CStr> for CString[src]

impl PartialEq<CString> for CString[src]

impl Eq for CString[src]

impl Ord for CString[src]

impl PartialOrd<CString> for CString[src]

impl Hash for CString[src]

impl Deref for CString[src]

type Target = CStr

The resulting type after dereferencing.

impl Drop for CString[src]

impl Index<RangeFull> for CString[src]

type Output = CStr

The returned type after indexing.

impl Debug for CString[src]

impl AsRef<CStr> for CString[src]

impl Clone for CString[src]

impl Default for CString[src]

fn default() -> CString[src]

Creates an empty CString.

impl Borrow<CStr> for CString[src]

Auto Trait Implementations

impl Unpin for CString

impl Send for CString

impl Sync for CString

Blanket Implementations

impl<T> From<T> for T[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.