1 use core::{borrow, fmt, hash, mem, ptr}; 2 use loom::alloc; 3 4 pub struct Box<T: ?Sized> { 5 ptr: *mut T, 6 } 7 8 impl<T> Box<T> { new(value: T) -> Self9 pub fn new(value: T) -> Self { 10 let layout = alloc::Layout::new::<T>(); 11 let ptr = unsafe { alloc::alloc(layout) } as *mut T; 12 unsafe { ptr::write(ptr, value) }; 13 Self { ptr } 14 } 15 } 16 17 impl<T: ?Sized> Box<T> { 18 #[inline] into_raw(b: Box<T>) -> *mut T19 pub fn into_raw(b: Box<T>) -> *mut T { 20 let ptr = b.ptr; 21 mem::forget(b); 22 ptr 23 } 24 from_raw(ptr: *mut T) -> Box<T>25 pub const unsafe fn from_raw(ptr: *mut T) -> Box<T> { 26 Self { ptr } 27 } 28 } 29 30 impl<T: ?Sized> Drop for Box<T> { drop(&mut self)31 fn drop(&mut self) { 32 unsafe { 33 let size = mem::size_of_val(&*self.ptr); 34 let align = mem::align_of_val(&*self.ptr); 35 let layout = alloc::Layout::from_size_align(size, align).unwrap(); 36 ptr::drop_in_place(self.ptr); 37 alloc::dealloc(self.ptr as *mut u8, layout); 38 } 39 } 40 } 41 42 unsafe impl<T: Send> Send for Box<T> {} 43 unsafe impl<T: Sync> Sync for Box<T> {} 44 45 impl<T: ?Sized> core::ops::Deref for Box<T> { 46 type Target = T; 47 deref(&self) -> &T48 fn deref(&self) -> &T { 49 unsafe { &*self.ptr } 50 } 51 } 52 53 impl<T: ?Sized> core::ops::DerefMut for Box<T> { deref_mut(&mut self) -> &mut T54 fn deref_mut(&mut self) -> &mut T { 55 unsafe { &mut *self.ptr } 56 } 57 } 58 59 impl<T: ?Sized> borrow::Borrow<T> for Box<T> { borrow(&self) -> &T60 fn borrow(&self) -> &T { 61 &**self 62 } 63 } 64 65 impl<T: ?Sized> borrow::BorrowMut<T> for Box<T> { borrow_mut(&mut self) -> &mut T66 fn borrow_mut(&mut self) -> &mut T { 67 &mut **self 68 } 69 } 70 71 impl<T: ?Sized> AsRef<T> for Box<T> { as_ref(&self) -> &T72 fn as_ref(&self) -> &T { 73 &**self 74 } 75 } 76 77 impl<T: ?Sized> AsMut<T> for Box<T> { as_mut(&mut self) -> &mut T78 fn as_mut(&mut self) -> &mut T { 79 &mut **self 80 } 81 } 82 83 impl<T: fmt::Display + ?Sized> fmt::Display for Box<T> { fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result84 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 85 fmt::Display::fmt(&**self, f) 86 } 87 } 88 89 impl<T: fmt::Debug + ?Sized> fmt::Debug for Box<T> { fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result90 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 91 fmt::Debug::fmt(&**self, f) 92 } 93 } 94 95 impl<T: Clone> Clone for Box<T> { 96 #[inline] clone(&self) -> Box<T>97 fn clone(&self) -> Box<T> { 98 Self::new(self.as_ref().clone()) 99 } 100 } 101 102 impl<T: ?Sized + PartialEq> PartialEq for Box<T> { 103 #[inline] eq(&self, other: &Box<T>) -> bool104 fn eq(&self, other: &Box<T>) -> bool { 105 PartialEq::eq(&**self, &**other) 106 } 107 108 #[allow(clippy::partialeq_ne_impl)] 109 #[inline] ne(&self, other: &Box<T>) -> bool110 fn ne(&self, other: &Box<T>) -> bool { 111 PartialEq::ne(&**self, &**other) 112 } 113 } 114 115 impl<T: ?Sized + Eq> Eq for Box<T> {} 116 117 impl<T: ?Sized + PartialOrd> PartialOrd for Box<T> { 118 #[inline] partial_cmp(&self, other: &Box<T>) -> Option<core::cmp::Ordering>119 fn partial_cmp(&self, other: &Box<T>) -> Option<core::cmp::Ordering> { 120 PartialOrd::partial_cmp(&**self, &**other) 121 } 122 #[inline] lt(&self, other: &Box<T>) -> bool123 fn lt(&self, other: &Box<T>) -> bool { 124 PartialOrd::lt(&**self, &**other) 125 } 126 #[inline] le(&self, other: &Box<T>) -> bool127 fn le(&self, other: &Box<T>) -> bool { 128 PartialOrd::le(&**self, &**other) 129 } 130 #[inline] ge(&self, other: &Box<T>) -> bool131 fn ge(&self, other: &Box<T>) -> bool { 132 PartialOrd::ge(&**self, &**other) 133 } 134 #[inline] gt(&self, other: &Box<T>) -> bool135 fn gt(&self, other: &Box<T>) -> bool { 136 PartialOrd::gt(&**self, &**other) 137 } 138 } 139 140 impl<T: ?Sized + Ord> Ord for Box<T> { 141 #[inline] cmp(&self, other: &Box<T>) -> core::cmp::Ordering142 fn cmp(&self, other: &Box<T>) -> core::cmp::Ordering { 143 Ord::cmp(&**self, &**other) 144 } 145 } 146 147 impl<T: ?Sized + hash::Hash> hash::Hash for Box<T> { hash<H: hash::Hasher>(&self, state: &mut H)148 fn hash<H: hash::Hasher>(&self, state: &mut H) { 149 (**self).hash(state); 150 } 151 } 152