safex11/src/ffi.rs
2023-08-20 04:50:59 -05:00

15 lines
391 B
Rust

use std::ffi::{CStr, CString};
use std::os::raw::c_char;
use crate::Result;
pub unsafe fn string_from_c_char_star(ptr: *mut c_char) -> Result<String> {
let c_str = CStr::from_ptr(ptr);
let s = c_str.to_str()?;
Ok(s.to_owned())
}
pub fn c_char_star_from_string(s: impl AsRef<str>) -> Result<*mut c_char> {
let c_str = CString::new(s.as_ref().as_bytes())?;
Ok(c_str.into_raw())
}