1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
use std::{
alloc::{GlobalAlloc, Layout},
os::raw::c_char,
};
pub(crate) fn c_str(rust_str: &str) -> *mut c_char {
unsafe {
// Note: Use +1 and alloc_zeroed() to get the null terminator
let layout = Layout::array::<u8>(rust_str.len() + 1).unwrap();
let buffer = std::alloc::System.alloc_zeroed(layout);
assert!(!buffer.is_null(), "Allocation failed");
std::ptr::copy_nonoverlapping(
rust_str.as_ptr(),
buffer,
rust_str.len(),
);
buffer.cast()
}
}