Trait std::c_str::ToCStrUnstable
[-]
[+]
[src]
pub trait ToCStr: ?Sized { fn to_c_str(&self) -> CString; unsafe fn to_c_str_unchecked(&self) -> CString; fn with_c_str<T, F>(&self, f: F) -> T where F: FnOnce(*const c_char) -> T { ... } unsafe fn with_c_str_unchecked<T, F>(&self, f: F) -> T where F: FnOnce(*const c_char) -> T { ... } }
A generic trait for converting a value to a CString.
Required Methods
fn to_c_str(&self) -> CString
unsafe fn to_c_str_unchecked(&self) -> CString
Unsafe variant of to_c_str()
that doesn't check for nulls.
Provided Methods
fn with_c_str<T, F>(&self, f: F) -> T where F: FnOnce(*const c_char) -> T
Work with a temporary CString constructed from the receiver.
The provided *libc::c_char
will be freed immediately upon return.
Example
extern crate libc; fn main() { let s = "PATH".with_c_str(|path| unsafe { libc::getenv(path) }); }extern crate libc; fn main() { let s = "PATH".with_c_str(|path| unsafe { libc::getenv(path) }); }
Panics
Panics the task if the receiver has an interior null.
unsafe fn with_c_str_unchecked<T, F>(&self, f: F) -> T where F: FnOnce(*const c_char) -> T
Unsafe variant of with_c_str()
that doesn't check for nulls.