1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
pub mod context; pub mod module; pub mod value; use std::borrow::Cow; use std::ffi::{CStr, CString}; use std::os::raw::c_char; pub(crate) fn to_c_str(string: &str) -> Cow<CStr> { let string = match string { "" => "\0", _ => string, }; match string.chars().rev().find(|ch| *ch == '\x00') { Some(_) => Cow::from(unsafe { CStr::from_ptr(string.as_ptr() as *const i8) }), None => Cow::from(CString::new(string).unwrap()), } } pub(crate) fn from_c_str(string: *const c_char) -> Cow<'static, str> { let c_str = unsafe { CStr::from_ptr(string) }; Cow::from(c_str.to_str().unwrap()) }