use crate::sys;
use std::ffi::{CStr, CString};
use std::marker::PhantomData;
use std::os::raw::c_char;
pub(crate) type NotThreadSafe = PhantomData<*const ()>;
pub(crate) fn cstring(s: &str) -> CString {
CString::new(s).unwrap_or_else(|_| {
CString::new(s.replace('\0', "")).expect("NUL-free after strip")
})
}
pub(crate) unsafe fn take_string(ptr: *mut c_char) -> String {
if ptr.is_null() {
return String::new();
}
let out = CStr::from_ptr(ptr).to_string_lossy().into_owned();
sys::affineui_string_free(ptr);
out
}
pub(crate) fn ensure_abi() {
use std::sync::Once;
static ONCE: Once = Once::new();
ONCE.call_once(|| {
let got = unsafe { sys::affineui_c_abi_version() };
assert_eq!(
got,
sys::AFFINEUI_C_ABI_VERSION,
"AffineUI C ABI mismatch: affineui_c reports version {got}, this \
crate expects {}. Rebuild the native library or update the crate.",
sys::AFFINEUI_C_ABI_VERSION
);
});
}