Expand description
Utilities for safely moving UTF-8 strings across the FFI boundary.
Interoperability between Rust and C/C++/Python often requires raw pointers to null terminated strings. This module provides convenience helpers that:
- Convert raw
*const c_char
pointers to RustString
,&str
, byte slices, orustr::Ustr
values. - Perform the inverse conversion when Rust needs to hand ownership of a string to foreign code.
The majority of these functions are marked unsafe
because they accept raw pointers and rely
on the caller to uphold basic invariants (pointer validity, lifetime, UTF-8 correctness). Each
function documents the specific safety requirements.
Functions§
- cstr_
as_ ⚠str - Convert a C string pointer into a string slice.
- cstr_
drop ⚠ - Drops the C string memory at the pointer.
- cstr_
to_ ⚠bytes - Convert a C string pointer into bytes.
- cstr_
to_ ⚠ustr - Convert a C string pointer into an owned
String
. - optional_
cstr_ ⚠to_ str - Convert a C string pointer into an
Option<&str>
. - optional_
cstr_ ⚠to_ ustr - Convert a C string pointer into an owned
Option<Ustr>
. - pystr_
to_ ⚠string - Returns an owned string from a valid Python object pointer.
- str_
to_ cstr - Create a C string pointer to newly allocated memory from a
&str
.