#![cfg_attr(not(doctest), no_std)]
extern crate alloc;
use alloc::alloc::{alloc as alloc_raw, handle_alloc_error, Layout};
use alloc::boxed::Box;
use core::ptr;
pub use default_boxed_derive::DefaultBoxed;
pub unsafe trait DefaultBoxed {
fn default_boxed() -> Box<Self>
where
Self: Sized,
{
let layout = Layout::new::<Self>();
unsafe {
if layout.size() == 0 {
return Box::from_raw(ptr::NonNull::<Self>::dangling().as_ptr());
}
let raw = alloc_raw(layout) as *mut Self;
if raw.is_null() {
handle_alloc_error(layout)
} else {
Self::default_in_place(raw);
Box::from_raw(raw)
}
}
}
fn default_boxed_array<const N: usize>() -> Box<[Self; N]>
where
Self: Sized,
{
let layout = Layout::new::<[Self; N]>();
unsafe {
if layout.size() == 0 {
return Box::from_raw(ptr::NonNull::<[Self; N]>::dangling().as_ptr());
}
let raw = alloc_raw(layout) as *mut Self;
if raw.is_null() {
handle_alloc_error(layout)
} else {
for i in 0..N as isize {
Self::default_in_place(raw.offset(i));
}
Box::from_raw(raw as *mut [Self; N])
}
}
}
unsafe fn default_in_place(ptr: *mut Self);
}
unsafe impl<T: Default> DefaultBoxed for T {
unsafe fn default_in_place(ptr: *mut Self) {
ptr::write(ptr, Default::default());
}
}