use std::{mem::size_of, slice, str};
use nix::libc::c_char;
#[inline]
pub fn align_to(num: usize, align_to: usize) -> usize {
let agn = align_to - 1;
(num + agn) & !agn
}
pub fn byte_slice_from_c_str(c_str: &[c_char]) -> &[u8] {
unsafe { slice::from_raw_parts(c_str as *const _ as *const u8, c_str.len()) }
}
pub fn str_from_c_str(slc: &[c_char]) -> Option<&str> {
let slc = byte_slice_from_c_str(slc);
str_from_byte_slice(slc)
}
pub fn str_from_byte_slice(slc: &[u8]) -> Option<&str> {
slc.iter()
.position(|c| *c == b'\0')
.and_then(|i| str::from_utf8(&slc[..i]).ok())
}
pub fn mut_slice_from_c_str(c_str: &mut [c_char]) -> &mut [u8] {
unsafe { slice::from_raw_parts_mut(c_str as *mut _ as *mut u8, c_str.len()) }
}
pub fn slice_from_c_struct<T>(strct: &T) -> &[u8] {
unsafe { slice::from_raw_parts(strct as *const _ as *const u8, size_of::<T>()) }
}
pub fn c_struct_from_slice<T>(slice: &[u8]) -> Option<&T> {
unsafe { (slice as *const _ as *const T).as_ref() }
}