]> git.lizzy.rs Git - rust.git/commitdiff
Auto merge of #75033 - Manishearth:rollup-d8afil1, r=Manishearth
authorbors <bors@rust-lang.org>
Sun, 2 Aug 2020 01:04:54 +0000 (01:04 +0000)
committerbors <bors@rust-lang.org>
Sun, 2 Aug 2020 01:04:54 +0000 (01:04 +0000)
Rollup of 5 pull requests

Successful merges:

 - #74602 (Clarify the doc for MaybeUninit::zeroed on incorrect use)
 - #74720 (Clean up E0728 explanation)
 - #74992 (fix rustdoc generic param order)
 - #75015 (Add Vec::spare_capacity_mut)
 - #75022 (Use a slice pattern instead of rchunks_exact(_).next())

Failed merges:

r? @ghost

1  2 
library/alloc/src/vec.rs

index 9eb37bf473fed1727ad9fdafce6d132da9e4f273,159f45ffce1a0ec27eed3a8b4bcc7083e3d9016d..786d1b6ba82f2fd894965c5d3a0b650ee944f53d
@@@ -1521,8 -1523,49 +1521,49 @@@ impl<T> Vec<T> 
      where
          T: 'a, // Technically not needed, but kept to be explicit.
      {
 -        Box::leak(vec.into_boxed_slice())
 +        Box::leak(self.into_boxed_slice())
      }
+     /// Returns the remaining spare capacity of the vector as a slice of
+     /// `MaybeUninit<T>`.
+     ///
+     /// The returned slice can be used to fill the vector with data (e.g. by
+     /// reading from a file) before marking the data as initialized using the
+     /// [`set_len`] method.
+     ///
+     /// [`set_len`]: #method.set_len
+     ///
+     /// # Examples
+     ///
+     /// ```
+     /// #![feature(vec_spare_capacity, maybe_uninit_extra)]
+     ///
+     /// // Allocate vector big enough for 10 elements.
+     /// let mut v = Vec::with_capacity(10);
+     ///
+     /// // Fill in the first 3 elements.
+     /// let uninit = v.spare_capacity_mut();
+     /// uninit[0].write(0);
+     /// uninit[1].write(1);
+     /// uninit[2].write(2);
+     ///
+     /// // Mark the first 3 elements of the vector as being initialized.
+     /// unsafe {
+     ///     v.set_len(3);
+     /// }
+     ///
+     /// assert_eq!(&v, &[0, 1, 2]);
+     /// ```
+     #[unstable(feature = "vec_spare_capacity", issue = "75017")]
+     #[inline]
+     pub fn spare_capacity_mut(&mut self) -> &mut [MaybeUninit<T>] {
+         unsafe {
+             slice::from_raw_parts_mut(
+                 self.as_mut_ptr().add(self.len) as *mut MaybeUninit<T>,
+                 self.buf.capacity() - self.len,
+             )
+         }
+     }
  }
  
  impl<T: Clone> Vec<T> {