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
use super::TryClone;
use crate::TryReserveError;
use alloc::alloc::Layout;
use alloc::boxed::Box;
use core::borrow::Borrow;
use core::ptr::NonNull;
pub trait FallibleBox<T> {
fn try_new(t: T) -> Result<Self, TryReserveError>
where
Self: Sized;
}
pub struct TryBox<T> {
inner: Box<T>,
}
impl<T> TryBox<T> {
pub fn try_new(t: T) -> Result<Self, TryReserveError> {
Ok(Self {
inner: Box::try_new(t)?,
})
}
pub fn into_raw(b: TryBox<T>) -> *mut T {
Box::into_raw(b.inner)
}
pub unsafe fn from_raw(raw: *mut T) -> Self {
Self {
inner: Box::from_raw(raw),
}
}
}
impl<T: TryClone> TryClone for TryBox<T> {
fn try_clone(&self) -> Result<Self, TryReserveError> {
let clone: T = (*self.inner).try_clone()?;
Self::try_new(clone)
}
}
fn alloc(layout: Layout) -> Result<NonNull<u8>, TryReserveError> {
#[cfg(feature = "unstable")]
{
use core::alloc::AllocRef as _;
let mut g = alloc::alloc::Global;
g.alloc(layout, alloc::alloc::AllocInit::Uninitialized)
.map_err(|_e| TryReserveError::AllocError {
layout,
non_exhaustive: (),
})
.map(|memory_block| memory_block.ptr)
}
#[cfg(not(feature = "unstable"))]
{
match layout.size() {
0 => {
Ok(NonNull::dangling())
}
1..=core::usize::MAX => {
let ptr = unsafe { alloc::alloc::alloc(layout) };
core::ptr::NonNull::new(ptr).ok_or(TryReserveError::AllocError { layout })
}
_ => unreachable!("size must be non-negative"),
}
}
}
impl<T> FallibleBox<T> for Box<T> {
fn try_new(t: T) -> Result<Self, TryReserveError> {
let layout = Layout::for_value(&t);
let ptr = alloc(layout)?.as_ptr() as *mut T;
unsafe {
core::ptr::write(ptr, t);
Ok(Box::from_raw(ptr))
}
}
}
impl<T: TryClone> TryClone for Box<T> {
fn try_clone(&self) -> Result<Self, TryReserveError> {
Self::try_new(Borrow::<T>::borrow(self).try_clone()?)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn boxed() {
let mut v = Box::try_new(5).unwrap();
assert_eq!(*v, 5);
*v = 3;
assert_eq!(*v, 3);
}
#[test]
fn trybox_zst() {
let b = Box::try_new(()).expect("ok");
assert_eq!(b, Box::new(()));
}
}