about summary refs log tree commit diff
path: root/src/liballoc
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2019-11-27 00:32:30 +0000
committerbors <bors@rust-lang.org>2019-11-27 00:32:30 +0000
commit809e180a76ce97340bf4354ff357bc59e3ca40b2 (patch)
tree7c8e0aaf9f4345352e07818cb99d9b99d78ad81e /src/liballoc
parenta7d791b4503a86c1b16b4393ba7998ef8b561a27 (diff)
parent8547ea32db4a6f2c5b27827b30f372859de22fbb (diff)
downloadrust-809e180a76ce97340bf4354ff357bc59e3ca40b2.tar.gz
rust-809e180a76ce97340bf4354ff357bc59e3ca40b2.zip
Auto merge of #66794 - tmandry:rollup-99qrpr0, r=tmandry
Rollup of 14 pull requests

Successful merges:

 - #66128 (alloc: Add new_zeroed() versions like new_uninit().)
 - #66661 (Add riscv64gc-unknown-linux-gnu target)
 - #66663 (Miri: print leak report even without tracing)
 - #66711 (Add hardware floating point features to aarch64-pc-windows-msvc)
 - #66713 (introduce a target to build the kernel of the unikernel HermitCore)
 - #66717 (tidy: Accommodate rustfmt's preferred layout of stability attributes)
 - #66719 (Store pointer width as u32 on Config)
 - #66720 (Move ErrorReported to rustc_errors)
 - #66737 (Error codes cleanup)
 - #66754 (Various tweaks to diagnostic output)
 - #66763 (Minor edit for documentation-tests.md that increases clarity)
 - #66779 (follow the same function order in the trait)
 - #66786 (Add wildcard test for const_if_match)
 - #66788 (Allow `Unreachable` terminators through `min_const_fn` checks)

Failed merges:

r? @ghost
Diffstat (limited to 'src/liballoc')
-rw-r--r--src/liballoc/boxed.rs27
-rw-r--r--src/liballoc/rc.rs29
-rw-r--r--src/liballoc/sync.rs29
3 files changed, 85 insertions, 0 deletions
diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs
index 567b8ea7224..51ad3a04e87 100644
--- a/src/liballoc/boxed.rs
+++ b/src/liballoc/boxed.rs
@@ -152,6 +152,33 @@ impl<T> Box<T> {
         Box(ptr.cast().into())
     }
 
+    /// Constructs a new `Box` with uninitialized contents, with the memory
+    /// being filled with `0` bytes.
+    ///
+    /// See [`MaybeUninit::zeroed`][zeroed] for examples of correct and incorrect usage
+    /// of this method.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(new_uninit)]
+    ///
+    /// let zero = Box::<u32>::new_zeroed();
+    /// let zero = unsafe { zero.assume_init() };
+    ///
+    /// assert_eq!(*zero, 0)
+    /// ```
+    ///
+    /// [zeroed]: ../../std/mem/union.MaybeUninit.html#method.zeroed
+    #[unstable(feature = "new_uninit", issue = "63291")]
+    pub fn new_zeroed() -> Box<mem::MaybeUninit<T>> {
+        unsafe {
+            let mut uninit = Self::new_uninit();
+            ptr::write_bytes::<T>(uninit.as_mut_ptr(), 0, 1);
+            uninit
+        }
+    }
+
     /// Constructs a new `Pin<Box<T>>`. If `T` does not implement `Unpin`, then
     /// `x` will be pinned in memory and unable to be moved.
     #[stable(feature = "pin", since = "1.33.0")]
diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs
index a11f9e8c145..ec08965674a 100644
--- a/src/liballoc/rc.rs
+++ b/src/liballoc/rc.rs
@@ -361,6 +361,35 @@ impl<T> Rc<T> {
         }
     }
 
+    /// Constructs a new `Rc` with uninitialized contents, with the memory
+    /// being filled with `0` bytes.
+    ///
+    /// See [`MaybeUninit::zeroed`][zeroed] for examples of correct and
+    /// incorrect usage of this method.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(new_uninit)]
+    ///
+    /// use std::rc::Rc;
+    ///
+    /// let zero = Rc::<u32>::new_zeroed();
+    /// let zero = unsafe { zero.assume_init() };
+    ///
+    /// assert_eq!(*zero, 0)
+    /// ```
+    ///
+    /// [zeroed]: ../../std/mem/union.MaybeUninit.html#method.zeroed
+    #[unstable(feature = "new_uninit", issue = "63291")]
+    pub fn new_zeroed() -> Rc<mem::MaybeUninit<T>> {
+        unsafe {
+            let mut uninit = Self::new_uninit();
+            ptr::write_bytes::<T>(Rc::get_mut_unchecked(&mut uninit).as_mut_ptr(), 0, 1);
+            uninit
+        }
+    }
+
     /// Constructs a new `Pin<Rc<T>>`. If `T` does not implement `Unpin`, then
     /// `value` will be pinned in memory and unable to be moved.
     #[stable(feature = "pin", since = "1.33.0")]
diff --git a/src/liballoc/sync.rs b/src/liballoc/sync.rs
index 4b10f089c29..0deb321d623 100644
--- a/src/liballoc/sync.rs
+++ b/src/liballoc/sync.rs
@@ -341,6 +341,35 @@ impl<T> Arc<T> {
         }
     }
 
+    /// Constructs a new `Arc` with uninitialized contents, with the memory
+    /// being filled with `0` bytes.
+    ///
+    /// See [`MaybeUninit::zeroed`][zeroed] for examples of correct and incorrect usage
+    /// of this method.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// #![feature(new_uninit)]
+    ///
+    /// use std::sync::Arc;
+    ///
+    /// let zero = Arc::<u32>::new_zeroed();
+    /// let zero = unsafe { zero.assume_init() };
+    ///
+    /// assert_eq!(*zero, 0)
+    /// ```
+    ///
+    /// [zeroed]: ../../std/mem/union.MaybeUninit.html#method.zeroed
+    #[unstable(feature = "new_uninit", issue = "63291")]
+    pub fn new_zeroed() -> Arc<mem::MaybeUninit<T>> {
+        unsafe {
+            let mut uninit = Self::new_uninit();
+            ptr::write_bytes::<T>(Arc::get_mut_unchecked(&mut uninit).as_mut_ptr(), 0, 1);
+            uninit
+        }
+    }
+
     /// Constructs a new `Pin<Arc<T>>`. If `T` does not implement `Unpin`, then
     /// `data` will be pinned in memory and unable to be moved.
     #[stable(feature = "pin", since = "1.33.0")]