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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#[cfg(any(test, feature = "default_allocator"))]
extern crate default_allocator;
use alloc::*;
use core::{any::Any, fmt, marker::Unsize, mem, ops::{CoerceUnsized, Deref, DerefMut}, ptr};
use ::ptr::Unique;
#[fundamental]
pub struct Box<T: ?Sized, A: Alloc = crate::DefaultA> {
pub(crate) ptr: Unique<T>,
pub(crate) alloc: A,
}
impl<T: ?Sized + fmt::Debug, A: Alloc> fmt::Debug for Box<T, A> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Debug::fmt(self.deref(), f) }
}
impl<T, A: Alloc> Box<T, A> {
#[inline]
pub fn new_in(mut a: A, x: T) -> Result<Self, T> {
if 0 == mem::size_of::<T>() { Ok(Unique::empty()) } else { match a.alloc_one() {
Ok(ptr) => unsafe { ptr::write(ptr.as_ptr().as_ptr(), x); Ok(ptr) },
Err(_) => Err(x),
} }.map(|ptr| Box { ptr, alloc: a })
}
}
impl<T, A: Alloc + Default> Box<T, A> {
#[inline]
pub fn new(x: T) -> Result<Self, T> { Self::new_in(A::default(), x) }
}
impl<T: ?Sized, A: Alloc> Box<T, A> {
#[inline]
pub unsafe fn from_raw_in(a: A, ptr: Unique<T>) -> Self { Box { ptr, alloc: a } }
#[inline]
pub unsafe fn into_raw(mut self) -> Unique<T> {
let ptr = self.ptr;
ptr::drop_in_place(&mut self.alloc);
mem::forget(self);
ptr
}
#[inline]
pub unsafe fn alloc_mut(&mut self) -> &mut A { &mut self.alloc }
}
impl<T: ?Sized, A: Alloc + Default> Box<T, A> {
#[inline]
pub unsafe fn from_raw(ptr: Unique<T>) -> Self { Self::from_raw_in(A::default(), ptr) }
}
impl<T: ?Sized, A: Alloc> Deref for Box<T, A> {
type Target = T;
fn deref(&self) -> &T { unsafe { self.ptr.as_ref() } }
}
impl<T: ?Sized, A: Alloc> DerefMut for Box<T, A> {
fn deref_mut(&mut self) -> &mut T { unsafe { self.ptr.as_mut() } }
}
impl<T: ?Sized, A: Alloc> Drop for Box<T, A> {
fn drop(&mut self) { unsafe {
ptr::drop_in_place(self.ptr.as_ptr().as_ptr());
if 0 != mem::size_of_val(self.deref()) {
self.alloc.dealloc(self.ptr.as_ptr().cast(), Layout::for_value(self.ptr.as_ref()));
}
} }
}
impl<S: ?Sized + Unsize<T>, T: ?Sized, A: Alloc> CoerceUnsized<Box<T, A>> for Box<S, A> {}
impl<T: ?Sized, A: Alloc> Unpin for Box<T, A> {}
impl Box<dyn Any> {
#[inline]
pub fn downcast<T: Any>(self) -> Result<Box<T>, Self> {
if self.is::<T>() {
let Box { ptr, alloc } = self;
Ok(Box { alloc, ptr: ptr.as_ptr().cast().into() })
} else { Err(self) }
}
}
#[cfg(test)]
mod tests {
extern crate default_allocator;
use core::ops::Deref;
use core::{mem, ptr};
type Box<T> = super::Box<T, default_allocator::Heap>;
#[test]
fn drop() {
struct T<'a>(&'a mut bool);
impl<'a> Drop for T<'a> {
fn drop(&mut self) { *self.0 = true; }
}
let mut c = false;
{
let _ = Box::new(T(&mut c));
}
assert!(c);
}
#[test]
fn keeps_value() {
let mut b = Box::new(0).unwrap();
assert_eq!(0, *b);
*b += 1;
assert_eq!(1, *b);
}
#[test]
fn zero_size() {
let b = Box::new(()).unwrap();
assert_eq!((), *b);
assert_ne!(ptr::null(), &b as *const _);
let _ = b.deref();
mem::forget(b);
}
#[test]
fn no_drop_into_raw() {
#[derive(Debug)]
struct T<'a>(&'a mut bool);
impl<'a> Drop for T<'a> {
fn drop(&mut self) { *self.0 = true; }
}
let mut c = false;
let _ = unsafe { Box::new(T(&mut c)).unwrap().into_raw() };
assert!(!c);
}
}