Function ffi_support::rust_str_from_c [−][src]
pub unsafe fn rust_str_from_c<'a>(c_string: *const c_char) -> &'a str
Please use FfiStr::as_str instead
Convert a null-terminated C string to a rust str
. This does not take ownership of the string,
and you should be careful about the lifetime of the resulting string. Note that strings
containing invalid UTF-8 are replaced with the empty string (for many cases, you will want to
use rust_string_from_c
instead, which will do a lossy conversion).
If you actually need an owned rust String
, you’re encouraged to use rust_string_from_c
,
which, as mentioned, also behaves better in the face of invalid UTF-8.
Safety
This is unsafe because we read from a raw pointer, which may or may not be valid.
We also assume c_string
is a null terminated string, and have no way of knowing if that’s
actually true. If it’s not, we’ll read arbitrary memory from the heap until we see a ‘\0’, which
can result in a enormous number of problems.
Panics
Panics if it’s argument is null, see opt_rust_str_from_c
for a variant that returns None in
this case instead.
Note: This means it’s forbidden to call this outside of a call_with_result
(or something else
that uses std::panic::catch_unwind
), as it is UB to panic across the FFI boundary.