use std::ffi::{CStr, CString};
use std::os::raw::c_char;
use crate::error::{ErrorCode, Result, ZvecError};
#[inline]
pub(crate) fn slice_as_bytes<T: Copy>(s: &[T]) -> &[u8] {
unsafe { core::slice::from_raw_parts(s.as_ptr() as *const u8, core::mem::size_of_val(s)) }
}
pub(crate) fn cstring(s: &str) -> Result<CString> {
CString::new(s).map_err(|e| {
ZvecError::with_message(
ErrorCode::InvalidArgument,
format!("string contains NUL byte at position {}", e.nul_position()),
)
})
}
pub(crate) unsafe fn cstr_to_string(ptr: *const c_char) -> Option<String> {
if ptr.is_null() {
None
} else {
Some(CStr::from_ptr(ptr).to_string_lossy().into_owned())
}
}
pub(crate) unsafe fn cstr_as_str<'a>(ptr: *const c_char) -> Option<&'a str> {
if ptr.is_null() {
None
} else {
CStr::from_ptr(ptr).to_str().ok()
}
}