Cstring from_raw
and into_raw
safety precisions by poliorcetics · Pull Request #72963 · rust-lang/rust (original) (raw)
This issue had two points:
- The one about
from_raw
has been addressed (I hope). - The other one, about
into_raw
, has only been partially fixed.
About into_raw
: the idea was to:
steer users away from using the pattern of CString::{into_raw,from_raw} when interfacing with C APIs that may change the effective length of the string by writing interior NULs or erasing the final NUL
I tried making a Vec<c_char>
like suggested but my current solution feels very unsafe and hacky to me (most notably the type cast), I included it here to make it available for discussion:
fn main() { use std::os::raw::c_char;
let v = String::from("abc")
.bytes()
// From u8 to i8,
// I feel like it will be a problem for values of u8 > 255
.map(|c| c as c_char)
.collect::<Vec<_>>();
dbg!(v);
}