#![warn(missing_docs)]
#![allow(clippy::not_unsafe_ptr_arg_deref)]
use std::ffi::{c_char, CStr};
#[tracing::instrument(skip_all)]
pub fn c_char_to_str(c_char: *const c_char) -> String {
if c_char.is_null() {
return "".to_string();
}
unsafe {
match CStr::from_ptr(c_char).to_str() {
Ok(s) => s.to_string(),
Err(_) => "".to_string(),
}
}
}
#[inline(always)]
#[tracing::instrument(skip_all)]
pub fn safe_wrapper<'a, T>(obj: *mut T) -> Option<&'a mut T> {
if obj.is_null() {
None
} else if (obj as usize) % std::mem::align_of::<T>() != 0 {
eprintln!("Pointer is not properly aligned");
None
} else {
unsafe { Some(&mut *obj ) }
}
}