Skip to content

Commit 91609c1

Browse files
committed
Auto merge of rust-lang#75033 - Manishearth:rollup-d8afil1, r=Manishearth
Rollup of 5 pull requests Successful merges: - rust-lang#74602 (Clarify the doc for MaybeUninit::zeroed on incorrect use) - rust-lang#74720 (Clean up E0728 explanation) - rust-lang#74992 (fix rustdoc generic param order) - rust-lang#75015 (Add Vec::spare_capacity_mut) - rust-lang#75022 (Use a slice pattern instead of rchunks_exact(_).next()) Failed merges: r? @ghost
2 parents 7740a5f + 08d8033 commit 91609c1

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

alloc/src/vec.rs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ use core::hash::{Hash, Hasher};
6565
use core::intrinsics::{arith_offset, assume};
6666
use core::iter::{FromIterator, FusedIterator, TrustedLen};
6767
use core::marker::PhantomData;
68-
use core::mem::{self, ManuallyDrop};
68+
use core::mem::{self, ManuallyDrop, MaybeUninit};
6969
use core::ops::Bound::{Excluded, Included, Unbounded};
7070
use core::ops::{self, Index, IndexMut, RangeBounds};
7171
use core::ptr::{self, NonNull};
@@ -1523,6 +1523,47 @@ impl<T> Vec<T> {
15231523
{
15241524
Box::leak(self.into_boxed_slice())
15251525
}
1526+
1527+
/// Returns the remaining spare capacity of the vector as a slice of
1528+
/// `MaybeUninit<T>`.
1529+
///
1530+
/// The returned slice can be used to fill the vector with data (e.g. by
1531+
/// reading from a file) before marking the data as initialized using the
1532+
/// [`set_len`] method.
1533+
///
1534+
/// [`set_len`]: #method.set_len
1535+
///
1536+
/// # Examples
1537+
///
1538+
/// ```
1539+
/// #![feature(vec_spare_capacity, maybe_uninit_extra)]
1540+
///
1541+
/// // Allocate vector big enough for 10 elements.
1542+
/// let mut v = Vec::with_capacity(10);
1543+
///
1544+
/// // Fill in the first 3 elements.
1545+
/// let uninit = v.spare_capacity_mut();
1546+
/// uninit[0].write(0);
1547+
/// uninit[1].write(1);
1548+
/// uninit[2].write(2);
1549+
///
1550+
/// // Mark the first 3 elements of the vector as being initialized.
1551+
/// unsafe {
1552+
/// v.set_len(3);
1553+
/// }
1554+
///
1555+
/// assert_eq!(&v, &[0, 1, 2]);
1556+
/// ```
1557+
#[unstable(feature = "vec_spare_capacity", issue = "75017")]
1558+
#[inline]
1559+
pub fn spare_capacity_mut(&mut self) -> &mut [MaybeUninit<T>] {
1560+
unsafe {
1561+
slice::from_raw_parts_mut(
1562+
self.as_mut_ptr().add(self.len) as *mut MaybeUninit<T>,
1563+
self.buf.capacity() - self.len,
1564+
)
1565+
}
1566+
}
15261567
}
15271568

15281569
impl<T: Clone> Vec<T> {

core/src/mem/maybe_uninit.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,8 @@ impl<T> MaybeUninit<T> {
336336
/// assert_eq!(x, (0, false));
337337
/// ```
338338
///
339-
/// *Incorrect* usage of this function: initializing a struct with zero, where some fields
340-
/// cannot hold 0 as a valid value.
339+
/// *Incorrect* usage of this function: calling `x.zeroed().assume_init()`
340+
/// when `0` is not a valid bit-pattern for the type:
341341
///
342342
/// ```rust,no_run
343343
/// use std::mem::MaybeUninit;

0 commit comments

Comments
 (0)