#![deny(missing_docs)]
#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(feature = "std")]
use std::alloc::{handle_alloc_error, alloc, alloc_zeroed, Layout};
#[cfg(feature = "allocator_api")]
extern crate allocator_api;
#[cfg(feature = "std")]
extern crate core;
#[cfg(feature = "std")]
use core::ptr;
#[cfg(feature = "allocator_api")]
mod allocator_box;
#[cfg(feature = "allocator_api")]
pub use allocator_box::*;
pub trait BoxExt {
type Inner;
fn new_with<F: FnOnce() -> Self::Inner>(f: F) -> Self;
fn new_zeroed() -> Self
where
Self: Sized,
Self::Inner: Zero;
fn try_new(x: Self::Inner) -> Option<Self>
where
Self: Sized;
fn try_new_with<F: FnOnce() -> Self::Inner>(f: F) -> Option<Self>
where
Self: Sized;
fn try_new_zeroed() -> Option<Self>
where
Self: Sized,
Self::Inner: Zero;
}
#[cfg(feature = "std")]
unsafe fn try_new_box<T>(zeroed: bool) -> Result<Box<T>, Layout> {
let layout = Layout::new::<T>();
let raw = if layout.size() == 0 {
ptr::NonNull::<T>::dangling().as_ptr()
} else if zeroed {
alloc_zeroed(layout) as *mut T
} else {
alloc(layout) as *mut T
};
if !raw.is_null() {
Ok(Box::from_raw(raw))
} else {
Err(layout)
}
}
#[cfg(feature = "std")]
unsafe fn new_box<T>(zeroed: bool) -> Box<T> {
try_new_box::<T>(zeroed).unwrap_or_else(|l| handle_alloc_error(l))
}
#[cfg(feature = "std")]
impl<T> BoxExt for Box<T> {
type Inner = T;
#[inline]
fn new_with<F: FnOnce() -> T>(f: F) -> Box<T> {
unsafe {
let mut b = new_box::<T>(false);
ptr::write(b.as_mut(), f());
b
}
}
#[inline]
fn new_zeroed() -> Box<T>
where
T: Zero,
{
unsafe { new_box(true) }
}
#[inline]
fn try_new(x: T) -> Option<Self> {
unsafe {
let mut b = try_new_box::<T>(false).ok()?;
ptr::write(b.as_mut(), x);
Some(b)
}
}
#[inline]
fn try_new_with<F: FnOnce() -> Self::Inner>(f: F) -> Option<Self> {
unsafe {
let mut b = try_new_box::<T>(false).ok()?;
ptr::write(b.as_mut(), f());
Some(b)
}
}
#[inline]
fn try_new_zeroed() -> Option<Self>
where
Self::Inner: Zero,
{
unsafe { try_new_box::<T>(true).ok() }
}
}
pub unsafe trait Zero: Sized {}
macro_rules! zero_num_impl {
($($t:ty)+) => { $(unsafe impl Zero for $t {})+ }
}
zero_num_impl! {
u8 u16 u32 u64 usize
i8 i16 i32 i64 isize
f32 f64
}
unsafe impl<T> Zero for *mut T {}
unsafe impl<T> Zero for *const T {}
macro_rules! zero_array_impl {
($($n:expr)+) => {$(
unsafe impl<T: Zero> Zero for [T; $n] {}
)+};
}
zero_array_impl! {
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
160 192 200 224 256 384 512 768 1024 2048 4096 8192 16384 32768
}
#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))]
zero_array_impl! {
65536 131072 262144 524288 1048576 2097152 4194304 8388608
16777216 33554432 67108864 134217728 268435456 536870912
1073741824 2147483648
}
#[cfg(target_pointer_width = "64")]
zero_array_impl! {
4294967296
}
macro_rules! zero_tuple_impl {
($t:ident $($u:ident)+) => {
zero_tuple_impl!(($t) $($u)+);
};
(($($t:ident)+) $u:ident $($v:ident)*) => {
zero_tuple_impl!(($($t)+));
zero_tuple_impl!(($($t)+ $u) $($v)*);
};
(($($t:ident)+)) => {
unsafe impl<$($t: Zero),+> Zero for ($($t,)+) {}
};
}
zero_tuple_impl! {
A B C D E F G H I J K L
}