Struct sgx_tstd::ffi::CString [−][src]
pub struct CString { /* fields omitted */ }A type representing an owned C-compatible string
Methods
impl CString[src]
impl CStringpub fn new<T>(t: T) -> Result<CString, NulError> where
T: Into<Vec<u8>>, [src]
pub fn new<T>(t: T) -> Result<CString, NulError> where
T: Into<Vec<u8>>, Creates a new C-compatible string from a container of bytes.
This method will consume the provided data and use the underlying bytes to construct a new string, ensuring that there is a trailing 0 byte.
Errors
This function will return an error if the bytes yielded contain an internal 0 byte. The error returned will contain the bytes as well as the position of the nul byte.
pub unsafe fn from_vec_unchecked(v: Vec<u8>) -> CString[src]
pub unsafe fn from_vec_unchecked(v: Vec<u8>) -> CStringCreates a C-compatible string from 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 i8) -> CString[src]
pub unsafe fn from_raw(ptr: *mut i8) -> CStringRetakes ownership of a CString that was transferred to C.
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.
pub fn into_raw(self) -> *mut i8[src]
pub fn into_raw(self) -> *mut i8Transfers ownership of the string to a C caller.
The pointer 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]
pub fn into_string(self) -> Result<String, IntoStringError>Converts the CString into a String if it contains valid Unicode data.
On failure, ownership of the original CString is returned.
pub fn into_bytes(self) -> Vec<u8>[src]
pub fn into_bytes(self) -> Vec<u8>Returns the underlying byte buffer.
The returned buffer does not contain the trailing nul separator and it is guaranteed to not have any interior nul bytes.
pub fn into_bytes_with_nul(self) -> Vec<u8>[src]
pub fn into_bytes_with_nul(self) -> Vec<u8>Equivalent to the into_bytes function except that the returned vector
includes the trailing nul byte.
pub fn as_bytes(&self) -> &[u8][src]
pub fn as_bytes(&self) -> &[u8]Returns the contents of this CString as a slice of bytes.
The returned slice does not contain the trailing nul separator and it is guaranteed to not have any interior nul bytes.
pub fn as_bytes_with_nul(&self) -> &[u8][src]
pub fn as_bytes_with_nul(&self) -> &[u8]Equivalent to the as_bytes function except that the returned slice
includes the trailing nul byte.
pub fn as_c_str(&self) -> &CStr[src]
pub fn as_c_str(&self) -> &CStrExtracts a CStr slice containing the entire string.
ⓘImportant traits for Box<I>pub fn into_boxed_c_str(self) -> Box<CStr>[src]
pub fn into_boxed_c_str(self) -> Box<CStr>Converts this CString into a boxed CStr.
Methods from Deref<Target = CStr>
pub fn as_ptr(&self) -> *const i8[src]
pub fn as_ptr(&self) -> *const i8Returns 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]
pub fn to_bytes(&self) -> &[u8]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]
pub fn to_bytes_with_nul(&self) -> &[u8]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]
pub fn to_str(&self) -> Result<&str, Utf8Error>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]
pub fn to_string_lossy(&self) -> Cow<str>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 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 Drop for CString[src]
impl Drop for CStringimpl Debug for CString[src]
impl Debug for CStringfn fmt(&self, f: &mut Formatter) -> Result<(), Error>[src]
fn fmt(&self, f: &mut Formatter) -> Result<(), Error>Formats the value using the given formatter. Read more
impl Clone for CString[src]
impl Clone for CStringfn clone(&self) -> CString[src]
fn clone(&self) -> CStringReturns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)1.0.0[src]
fn clone_from(&mut self, source: &Self)Performs copy-assignment from source. Read more
impl Hash for CString[src]
impl Hash for CStringfn hash<__H>(&self, state: &mut __H) where
__H: Hasher, [src]
fn hash<__H>(&self, state: &mut __H) where
__H: Hasher, Feeds this value into the given [Hasher]. Read more
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher, 1.3.0[src]
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher, Feeds a slice of this type into the given [Hasher]. Read more
impl PartialEq<CString> for CString[src]
impl PartialEq<CString> for CStringfn eq(&self, other: &CString) -> bool[src]
fn eq(&self, other: &CString) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &CString) -> bool[src]
fn ne(&self, other: &CString) -> boolThis method tests for !=.
impl AsRef<CStr> for CString[src]
impl AsRef<CStr> for CStringimpl Eq for CString[src]
impl Eq for CStringimpl Default for CString[src]
impl Default for CStringimpl Deref for CString[src]
impl Deref for CStringtype Target = CStr
The resulting type after dereferencing.
fn deref(&self) -> &CStr[src]
fn deref(&self) -> &CStrDereferences the value.
impl From<CString> for Box<CStr>[src]
impl From<CString> for Box<CStr>impl From<Box<CStr>> for CString[src]
impl From<Box<CStr>> for CStringimpl From<CString> for Rc<CStr>[src]
impl From<CString> for Rc<CStr>impl From<CString> for Arc<CStr>[src]
impl From<CString> for Arc<CStr>impl<'a> From<&'a CStr> for CString[src]
impl<'a> From<&'a CStr> for CStringimpl From<CString> for Vec<u8>[src]
impl From<CString> for Vec<u8>impl Borrow<CStr> for CString[src]
impl Borrow<CStr> for CStringimpl PartialOrd<CString> for CString[src]
impl PartialOrd<CString> for CStringfn partial_cmp(&self, other: &CString) -> Option<Ordering>[src]
fn partial_cmp(&self, other: &CString) -> Option<Ordering>This method returns an ordering between self and other values if one exists. Read more
fn lt(&self, other: &CString) -> bool[src]
fn lt(&self, other: &CString) -> boolThis method tests less than (for self and other) and is used by the < operator. Read more
fn le(&self, other: &CString) -> bool[src]
fn le(&self, other: &CString) -> boolThis method tests less than or equal to (for self and other) and is used by the <= operator. Read more
fn gt(&self, other: &CString) -> bool[src]
fn gt(&self, other: &CString) -> boolThis method tests greater than (for self and other) and is used by the > operator. Read more
fn ge(&self, other: &CString) -> bool[src]
fn ge(&self, other: &CString) -> boolThis method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
impl Ord for CString[src]
impl Ord for CStringfn cmp(&self, other: &CString) -> Ordering[src]
fn cmp(&self, other: &CString) -> OrderingThis method returns an Ordering between self and other. Read more
fn max(self, other: Self) -> Self1.21.0[src]
fn max(self, other: Self) -> SelfCompares and returns the maximum of two values. Read more
fn min(self, other: Self) -> Self1.21.0[src]
fn min(self, other: Self) -> SelfCompares and returns the minimum of two values. Read more
impl Index<RangeFull> for CString[src]
impl Index<RangeFull> for CString