safex11/src/ffi.rs

16 lines
391 B
Rust
Raw Normal View History

2020-06-28 19:50:29 +00:00
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> {
2023-08-20 09:50:59 +00:00
let c_str = CStr::from_ptr(ptr);
let s = c_str.to_str()?;
Ok(s.to_owned())
2020-06-28 19:50:29 +00:00
}
pub fn c_char_star_from_string(s: impl AsRef<str>) -> Result<*mut c_char> {
2023-08-20 09:50:59 +00:00
let c_str = CString::new(s.as_ref().as_bytes())?;
Ok(c_str.into_raw())
2020-06-28 19:50:29 +00:00
}