]> git.lizzy.rs Git - rust.git/blobdiff - src/liballoc/boxed.rs
Auto merge of #28651 - dotdash:exhaustive_match, r=eddyb
[rust.git] / src / liballoc / boxed.rs
index 8d357eb49a9f389049529270ad0a78acc5c6720e..1529187da064c05550974edc92755fdb91ead193 100644 (file)
@@ -57,6 +57,7 @@
 use raw_vec::RawVec;
 
 use core::any::Any;
+use core::borrow;
 use core::cmp::Ordering;
 use core::fmt;
 use core::hash::{self, Hash};
@@ -65,7 +66,7 @@
 use core::ops::{CoerceUnsized, Deref, DerefMut};
 use core::ops::{Placer, Boxed, Place, InPlace, BoxPlace};
 use core::ptr::{self, Unique};
-use core::raw::{TraitObject};
+use core::raw::TraitObject;
 
 /// A value that represents the heap. This is the default place that the `box`
 /// keyword allocates into when no place is supplied.
 /// use std::boxed::HEAP;
 ///
 /// fn main() {
-///     let foo = box(HEAP) 5;
+///     let foo: Box<i32> = in HEAP { 5 };
 ///     let foo = box 5;
 /// }
 /// ```
-#[lang = "exchange_heap"]
 #[unstable(feature = "box_heap",
-           reason = "may be renamed; uncertain about custom allocator design")]
+           reason = "may be renamed; uncertain about custom allocator design",
+           issue = "27779")]
 pub const HEAP: ExchangeHeapSingleton =
     ExchangeHeapSingleton { _force_singleton: () };
 
 /// This the singleton type used solely for `boxed::HEAP`.
 #[unstable(feature = "box_heap",
-           reason = "may be renamed; uncertain about custom allocator design")]
+           reason = "may be renamed; uncertain about custom allocator design",
+           issue = "27779")]
 #[derive(Copy, Clone)]
-pub struct ExchangeHeapSingleton { _force_singleton: () }
+pub struct ExchangeHeapSingleton {
+    _force_singleton: (),
+}
 
 /// A pointer type for heap allocation.
 ///
@@ -121,8 +125,10 @@ pub struct ExchangeHeapSingleton { _force_singleton: () }
 /// the fact that the `align_of` intrinsic currently requires the
 /// input type to be Sized (which I do not think is strictly
 /// necessary).
-#[unstable(feature = "placement_in", reason = "placement box design is still being worked out.")]
-pub struct IntermediateBox<T: ?Sized>{
+#[unstable(feature = "placement_in",
+           reason = "placement box design is still being worked out.",
+           issue = "27779")]
+pub struct IntermediateBox<T: ?Sized> {
     ptr: *mut u8,
     size: usize,
     align: usize,
@@ -148,9 +154,7 @@ fn make_place<T>() -> IntermediateBox<T> {
     let p = if size == 0 {
         heap::EMPTY as *mut u8
     } else {
-        let p = unsafe {
-            heap::allocate(size, align)
-        };
+        let p = unsafe { heap::allocate(size, align) };
         if p.is_null() {
             panic!("Box make_place allocation failure.");
         }
@@ -161,18 +165,24 @@ fn make_place<T>() -> IntermediateBox<T> {
 }
 
 impl<T> BoxPlace<T> for IntermediateBox<T> {
-    fn make_place() -> IntermediateBox<T> { make_place() }
+    fn make_place() -> IntermediateBox<T> {
+        make_place()
+    }
 }
 
 impl<T> InPlace<T> for IntermediateBox<T> {
     type Owner = Box<T>;
-    unsafe fn finalize(self) -> Box<T> { finalize(self) }
+    unsafe fn finalize(self) -> Box<T> {
+        finalize(self)
+    }
 }
 
 impl<T> Boxed for Box<T> {
     type Data = T;
     type Place = IntermediateBox<T>;
-    unsafe fn finalize(b: IntermediateBox<T>) -> Box<T> { finalize(b) }
+    unsafe fn finalize(b: IntermediateBox<T>) -> Box<T> {
+        finalize(b)
+    }
 }
 
 impl<T> Placer<T> for ExchangeHeapSingleton {
@@ -186,9 +196,7 @@ fn make_place(self) -> IntermediateBox<T> {
 impl<T: ?Sized> Drop for IntermediateBox<T> {
     fn drop(&mut self) {
         if self.size > 0 {
-            unsafe {
-                heap::deallocate(self.ptr, self.size, self.align)
-            }
+            unsafe { heap::deallocate(self.ptr, self.size, self.align) }
         }
     }
 }
@@ -221,10 +229,8 @@ impl<T : ?Sized> Box<T> {
     /// Function is unsafe, because improper use of this function may
     /// lead to memory problems like double-free, for example if the
     /// function is called twice on the same raw pointer.
-    #[unstable(feature = "box_raw",
-               reason = "may be renamed or moved out of Box scope")]
+    #[stable(feature = "box_raw", since = "1.4.0")]
     #[inline]
-    // NB: may want to be called from_ptr, see comments on CStr::from_ptr
     pub unsafe fn from_raw(raw: *mut T) -> Self {
         mem::transmute(raw)
     }
@@ -238,16 +244,14 @@ pub unsafe fn from_raw(raw: *mut T) -> Self {
     /// `Box` does not specify, how memory is allocated.
     ///
     /// # Examples
-    /// ```
-    /// #![feature(box_raw)]
     ///
+    /// ```
     /// let seventeen = Box::new(17u32);
     /// let raw = Box::into_raw(seventeen);
     /// let boxed_again = unsafe { Box::from_raw(raw) };
     /// ```
-    #[unstable(feature = "box_raw", reason = "may be renamed")]
+    #[stable(feature = "box_raw", since = "1.4.0")]
     #[inline]
-    // NB: may want to be called into_ptr, see comments on CStr::from_ptr
     pub fn into_raw(b: Box<T>) -> *mut T {
         unsafe { mem::transmute(b) }
     }
@@ -256,13 +260,17 @@ pub fn into_raw(b: Box<T>) -> *mut T {
 #[stable(feature = "rust1", since = "1.0.0")]
 impl<T: Default> Default for Box<T> {
     #[stable(feature = "rust1", since = "1.0.0")]
-    fn default() -> Box<T> { box Default::default() }
+    fn default() -> Box<T> {
+        box Default::default()
+    }
 }
 
 #[stable(feature = "rust1", since = "1.0.0")]
 impl<T> Default for Box<[T]> {
     #[stable(feature = "rust1", since = "1.0.0")]
-    fn default() -> Box<[T]> { Box::<[T; 0]>::new([]) }
+    fn default() -> Box<[T]> {
+        Box::<[T; 0]>::new([])
+    }
 }
 
 #[stable(feature = "rust1", since = "1.0.0")]
@@ -275,15 +283,16 @@ impl<T: Clone> Clone for Box<T> {
     /// let x = Box::new(5);
     /// let y = x.clone();
     /// ```
+    #[rustfmt_skip]
     #[inline]
-    fn clone(&self) -> Box<T> { box {(**self).clone()} }
+    fn clone(&self) -> Box<T> {
+        box { (**self).clone() }
+    }
     /// Copies `source`'s contents into `self` without creating a new allocation.
     ///
     /// # Examples
     ///
     /// ```
-    /// #![feature(box_raw)]
-    ///
     /// let x = Box::new(5);
     /// let mut y = Box::new(10);
     ///
@@ -313,9 +322,13 @@ fn clone(&self) -> Self {
 #[stable(feature = "rust1", since = "1.0.0")]
 impl<T: ?Sized + PartialEq> PartialEq for Box<T> {
     #[inline]
-    fn eq(&self, other: &Box<T>) -> bool { PartialEq::eq(&**self, &**other) }
+    fn eq(&self, other: &Box<T>) -> bool {
+        PartialEq::eq(&**self, &**other)
+    }
     #[inline]
-    fn ne(&self, other: &Box<T>) -> bool { PartialEq::ne(&**self, &**other) }
+    fn ne(&self, other: &Box<T>) -> bool {
+        PartialEq::ne(&**self, &**other)
+    }
 }
 #[stable(feature = "rust1", since = "1.0.0")]
 impl<T: ?Sized + PartialOrd> PartialOrd for Box<T> {
@@ -324,13 +337,21 @@ fn partial_cmp(&self, other: &Box<T>) -> Option<Ordering> {
         PartialOrd::partial_cmp(&**self, &**other)
     }
     #[inline]
-    fn lt(&self, other: &Box<T>) -> bool { PartialOrd::lt(&**self, &**other) }
+    fn lt(&self, other: &Box<T>) -> bool {
+        PartialOrd::lt(&**self, &**other)
+    }
     #[inline]
-    fn le(&self, other: &Box<T>) -> bool { PartialOrd::le(&**self, &**other) }
+    fn le(&self, other: &Box<T>) -> bool {
+        PartialOrd::le(&**self, &**other)
+    }
     #[inline]
-    fn ge(&self, other: &Box<T>) -> bool { PartialOrd::ge(&**self, &**other) }
+    fn ge(&self, other: &Box<T>) -> bool {
+        PartialOrd::ge(&**self, &**other)
+    }
     #[inline]
-    fn gt(&self, other: &Box<T>) -> bool { PartialOrd::gt(&**self, &**other) }
+    fn gt(&self, other: &Box<T>) -> bool {
+        PartialOrd::gt(&**self, &**other)
+    }
 }
 #[stable(feature = "rust1", since = "1.0.0")]
 impl<T: ?Sized + Ord> Ord for Box<T> {
@@ -358,8 +379,7 @@ pub fn downcast<T: Any>(self) -> Result<Box<T>, Box<Any>> {
             unsafe {
                 // Get the raw representation of the trait object
                 let raw = Box::into_raw(self);
-                let to: TraitObject =
-                    mem::transmute::<*mut Any, TraitObject>(raw);
+                let to: TraitObject = mem::transmute::<*mut Any, TraitObject>(raw);
 
                 // Extract the data pointer
                 Ok(Box::from_raw(to.data as *mut T))
@@ -410,23 +430,33 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
 impl<T: ?Sized> Deref for Box<T> {
     type Target = T;
 
-    fn deref(&self) -> &T { &**self }
+    fn deref(&self) -> &T {
+        &**self
+    }
 }
 
 #[stable(feature = "rust1", since = "1.0.0")]
 impl<T: ?Sized> DerefMut for Box<T> {
-    fn deref_mut(&mut self) -> &mut T { &mut **self }
+    fn deref_mut(&mut self) -> &mut T {
+        &mut **self
+    }
 }
 
 #[stable(feature = "rust1", since = "1.0.0")]
 impl<I: Iterator + ?Sized> Iterator for Box<I> {
     type Item = I::Item;
-    fn next(&mut self) -> Option<I::Item> { (**self).next() }
-    fn size_hint(&self) -> (usize, Option<usize>) { (**self).size_hint() }
+    fn next(&mut self) -> Option<I::Item> {
+        (**self).next()
+    }
+    fn size_hint(&self) -> (usize, Option<usize>) {
+        (**self).size_hint()
+    }
 }
 #[stable(feature = "rust1", since = "1.0.0")]
 impl<I: DoubleEndedIterator + ?Sized> DoubleEndedIterator for Box<I> {
-    fn next_back(&mut self) -> Option<I::Item> { (**self).next_back() }
+    fn next_back(&mut self) -> Option<I::Item> {
+        (**self).next_back()
+    }
 }
 #[stable(feature = "rust1", since = "1.0.0")]
 impl<I: ExactSizeIterator + ?Sized> ExactSizeIterator for Box<I> {}
@@ -470,7 +500,7 @@ impl<I: ExactSizeIterator + ?Sized> ExactSizeIterator for Box<I> {}
 /// }
 /// ```
 #[rustc_paren_sugar]
-#[unstable(feature = "fnbox", reason = "Newly introduced")]
+#[unstable(feature = "fnbox", reason = "Newly introduced", issue = "0")]
 pub trait FnBox<A> {
     type Output;
 
@@ -508,10 +538,7 @@ impl<T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<Box<U>> for Box<T> {}
 #[stable(feature = "box_slice_clone", since = "1.3.0")]
 impl<T: Clone> Clone for Box<[T]> {
     fn clone(&self) -> Self {
-        let mut new = BoxBuilder {
-            data: RawVec::with_capacity(self.len()),
-            len: 0
-        };
+        let mut new = BoxBuilder { data: RawVec::with_capacity(self.len()), len: 0 };
 
         let mut target = new.data.ptr();
 
@@ -556,3 +583,14 @@ fn drop(&mut self) {
     }
 }
 
+impl<T: ?Sized> borrow::Borrow<T> for Box<T> {
+    fn borrow(&self) -> &T {
+        &**self
+    }
+}
+
+impl<T: ?Sized> borrow::BorrowMut<T> for Box<T> {
+    fn borrow_mut(&mut self) -> &mut T {
+        &mut **self
+    }
+}