Skip to main content

c_str_utf8

Function c_str_utf8 

Source
pub unsafe fn c_str_utf8<'a>(ptr: *const c_char) -> Result<&'a str, FfiError>
Expand description

Read a non-null C string argument as &str, classifying a non-UTF-8 one.

Separate from with_c_str! because an entry point with an out-parameter has to reject a null pointer before it starts producing a result — its failure path writes through that pointer — so it does its own null check and needs only the conversion. Everywhere else, with_c_str! does both.

§Safety

ptr must be non-null and point to a NUL-terminated string that stays valid for 'a. The lifetime is unbounded, as with std::ffi::CStr::from_ptr: nothing here can check how long the caller’s buffer lives.

let path = CString::new("document.pdf").unwrap();
let read = unsafe { unparser_shared::ffi::c_str_utf8(path.as_ptr()) };
assert_eq!(read.unwrap(), "document.pdf");