| 1 |
//! Implement Fallible Box |
| 2 |
use super::TryClone; |
| 3 |
use crate::TryReserveError; |
| 4 |
use alloc::alloc::Layout; |
| 5 |
use alloc::boxed::Box; |
| 6 |
use core::borrow::Borrow; |
| 7 |
use core::ops::Deref; |
| 8 |
use core::ptr::NonNull; |
| 9 |
|
| 10 |
/// trait to implement Fallible Box |
| 11 |
pub trait FallibleBox<T> { |
| 12 |
/// try creating a new box, returning a Result<Box<T>, |
| 13 |
/// TryReserveError> if allocation failed |
| 14 |
fn try_new(t: T) -> Result<Self, TryReserveError> |
| 15 |
where |
| 16 |
Self: Sized; |
| 17 |
} |
| 18 |
/// TryBox is a thin wrapper around alloc::boxed::Box to provide support for |
| 19 |
/// fallible allocation. |
| 20 |
/// |
| 21 |
/// See the crate documentation for more. |
| 22 |
pub struct TryBox<T> { |
| 23 |
inner: Box<T>, |
| 24 |
} |
| 25 |
|
| 26 |
impl<T> TryBox<T> { |
| 27 |
#[inline] |
| 28 |
pub fn try_new(t: T) -> Result<Self, TryReserveError> { |
| 29 |
Ok(Self { |
| 30 |
inner: <Box<T> as FallibleBox<T>>::try_new(t)?, |
| 31 |
}) |
| 32 |
} |
| 33 |
|
| 34 |
#[inline(always)] |
| 35 |
pub fn into_raw(b: TryBox<T>) -> *mut T { |
| 36 |
Box::into_raw(b.inner) |
| 37 |
} |
| 38 |
|
| 39 |
/// # Safety |
| 40 |
/// |
| 41 |
/// See std::boxed::from_raw |
| 42 |
#[inline(always)] |
| 43 |
pub unsafe fn from_raw(raw: *mut T) -> Self { |
| 44 |
Self { |
| 45 |
inner: Box::from_raw(raw), |
| 46 |
} |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
impl<T: TryClone> TryClone for TryBox<T> { |
| 51 |
fn try_clone(&self) -> Result<Self, TryReserveError> { |
| 52 |
let clone: T = (*self.inner).try_clone()?; |
| 53 |
Self::try_new(clone) |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
impl<T> Deref for TryBox<T> { |
| 58 |
type Target = T; |
| 59 |
|
| 60 |
#[inline(always)] |
| 61 |
fn deref(&self) -> &T { |
| 62 |
self.inner.deref() |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
fn alloc(layout: Layout) -> Result<NonNull<u8>, TryReserveError> { |
| 67 |
#[cfg(all(feature = "unstable", not(feature = "rust_1_57")))] // requires allocator_api |
| 68 |
{ |
| 69 |
use alloc::collections::TryReserveErrorKind; |
| 70 |
use core::alloc::Allocator; |
| 71 |
alloc::alloc::Global |
| 72 |
.allocate(layout) |
| 73 |
.map_err(|_e| { |
| 74 |
TryReserveErrorKind::AllocError { |
| 75 |
layout, |
| 76 |
non_exhaustive: (), |
| 77 |
} |
| 78 |
.into() |
| 79 |
}) |
| 80 |
.map(|v| v.cast()) |
| 81 |
} |
| 82 |
#[cfg(any(not(feature = "unstable"), feature = "rust_1_57"))] |
| 83 |
{ |
| 84 |
match layout.size() { |
| 85 |
0 => { |
| 86 |
// Required for alloc safety |
| 87 |
// See https://doc.rust-lang.org/stable/std/alloc/trait.GlobalAlloc.html#safety-1 |
| 88 |
Ok(NonNull::dangling()) |
| 89 |
} |
| 90 |
1..=core::usize::MAX => { |
| 91 |
let ptr = unsafe { alloc::alloc::alloc(layout) }; |
| 92 |
core::ptr::NonNull::new(ptr).ok_or(TryReserveError::AllocError { layout }) |
| 93 |
} |
| 94 |
_ => unreachable!("size must be non-negative"), |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
impl<T> FallibleBox<T> for Box<T> { |
| 100 |
fn try_new(t: T) -> Result<Self, TryReserveError> { |
| 101 |
let layout = Layout::for_value(&t); |
| 102 |
let ptr = alloc(layout)?.as_ptr() as *mut T; |
| 103 |
unsafe { |
| 104 |
core::ptr::write(ptr, t); |
| 105 |
Ok(Box::from_raw(ptr)) |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
impl<T: TryClone> TryClone for Box<T> { |
| 111 |
#[inline] |
| 112 |
fn try_clone(&self) -> Result<Self, TryReserveError> { |
| 113 |
<Self as FallibleBox<T>>::try_new(Borrow::<T>::borrow(self).try_clone()?) |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
#[cfg(test)] |
| 118 |
mod tests { |
| 119 |
use super::*; |
| 120 |
#[test] |
| 121 |
fn boxed() { |
| 122 |
let mut v = <Box<_> as FallibleBox<_>>::try_new(5).unwrap(); |
| 123 |
assert_eq!(*v, 5); |
| 124 |
*v = 3; |
| 125 |
assert_eq!(*v, 3); |
| 126 |
} |
| 127 |
// #[test] |
| 128 |
// fn big_alloc() { |
| 129 |
// let layout = Layout::from_size_align(1_000_000_000_000, 8).unwrap(); |
| 130 |
// let ptr = unsafe { alloc::alloc::alloc(layout) }; |
| 131 |
// assert!(ptr.is_null()); |
| 132 |
// } |
| 133 |
|
| 134 |
#[test] |
| 135 |
fn trybox_zst() { |
| 136 |
let b = <Box<_> as FallibleBox<_>>::try_new(()).expect("ok"); |
| 137 |
assert_eq!(b, Box::new(())); |
| 138 |
} |
| 139 |
|
| 140 |
struct NonCopyType; |
| 141 |
|
| 142 |
#[test] |
| 143 |
fn trybox_deref() { |
| 144 |
let try_box: TryBox<NonCopyType> = TryBox::try_new(NonCopyType {}).unwrap(); |
| 145 |
let _derefed: &NonCopyType = try_box.deref(); |
| 146 |
} |
| 147 |
|
| 148 |
#[test] |
| 149 |
fn trybox_as_deref() { |
| 150 |
let try_box_option: Option<TryBox<NonCopyType>> = |
| 151 |
Some(TryBox::try_new(NonCopyType).unwrap()); |
| 152 |
let _ref_option: Option<&NonCopyType> = try_box_option.as_deref(); |
| 153 |
} |
| 154 |
} |