use alloc::boxed::Box;
use core::alloc::Layout;
use core::mem::MaybeUninit;
pub mod bitsets;
macro_rules! box_uninit_alloc_impl {
(for $tp:ident {
Box::new(MaybeUninit::$explicit_create:ident),
unsafe { Box::from_raw(std::alloc::$alloc_func:ident) }
}) => {{
let layout = Layout::new::<$tp>();
if layout.size() == 0 {
Box::new(MaybeUninit::$explicit_create())
} else {
let allocated = unsafe { alloc::alloc::alloc(layout) }.cast::<MaybeUninit<$tp>>();
if allocated.is_null() {
alloc::alloc::handle_alloc_error(layout)
} else {
unsafe { Box::from_raw(allocated) }
}
}
}};
}
#[inline]
pub fn box_alloc_uninit<T>() -> Box<MaybeUninit<T>> {
box_uninit_alloc_impl!(for T {
Box::new(MaybeUninit::uninit),
unsafe { Box::from_raw(std::alloc::alloc) }
})
}
#[inline]
pub fn box_alloc_zeroed<T>() -> Box<MaybeUninit<T>> {
box_uninit_alloc_impl!(for T {
Box::new(MaybeUninit::uninit),
unsafe { Box::from_raw(std::alloc::alloc_zeroed) }
})
}
#[inline]
pub unsafe fn box_assume_init<T>(value: Box<MaybeUninit<T>>) -> Box<T> {
let ptr: *mut MaybeUninit<T> = Box::into_raw(value);
unsafe { Box::from_raw(ptr.cast::<T>()) }
}
pub(crate) unsafe trait Zeroable: Sized {
#[inline]
fn zeroed_boxed() -> Box<Self> {
let zeroed = box_alloc_zeroed();
unsafe { box_assume_init(zeroed) }
}
#[inline]
fn zeroed() -> Self {
unsafe { core::mem::zeroed() }
}
}