Skip to content

Rollup of 7 pull requests #76637

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Sep 12, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
ccf1f58
rustdoc: fix min_const_generics with ty::Param
lcnr Sep 3, 2020
b869aa5
Add saturating methods for `Duration`
marmeladema Aug 30, 2020
75e471a
Add MaybeUninit::drop.
m-ou-se Sep 8, 2020
caef832
Fix doc comment on MaybeUninit::drop.
m-ou-se Sep 8, 2020
656a17b
Rename MaybeUninit::drop to assume_init_drop.
m-ou-se Sep 9, 2020
a14efd1
Rename MaybeUninit::read to assume_init_read.
m-ou-se Sep 9, 2020
a94b2cb
Add safety docs about T's invariants in MaybeUninit::assume_init_drop.
m-ou-se Sep 9, 2020
43c7a9b
Fix broken doc links in MaybeUninit.
m-ou-se Sep 9, 2020
493c037
Eliminate mut reference UB in Drop impl for Rc<T>
carbotaniuman Sep 9, 2020
8f43fa0
Add WeakInner<'_> and have Weak::inner() return it
carbotaniuman Sep 9, 2020
bb57c9f
Format
carbotaniuman Sep 9, 2020
954361a
Update `std::os` module documentation.
CDirkx Sep 11, 2020
9abc6bd
Add revisions to const generic const_evaluatable_checked tests.
hameerabbasi Sep 11, 2020
5e188f5
Add revisions to const generic type-dependent UI tests.
hameerabbasi Sep 11, 2020
b729368
Address review comments
carbotaniuman Sep 11, 2020
bb9ce7c
Add missing examples on binary core traits
GuillaumeGomez Sep 11, 2020
7344f93
Rollup merge of #76114 - marmeladema:duration-saturating-ops, r=shepm…
RalfJung Sep 12, 2020
5d90d6e
Rollup merge of #76297 - lcnr:const-ty-alias, r=varkor
RalfJung Sep 12, 2020
c20356e
Rollup merge of #76484 - fusion-engineering-forks:maybe-uninit-drop, …
RalfJung Sep 12, 2020
a49451c
Rollup merge of #76530 - carbotaniuman:fix-rc, r=RalfJung
RalfJung Sep 12, 2020
2477f07
Rollup merge of #76583 - CDirkx:os-doc, r=jonas-schievink
RalfJung Sep 12, 2020
90c5b8f
Rollup merge of #76599 - hameerabbasi:const-generics-revs, r=lcnr
RalfJung Sep 12, 2020
0ed4bc5
Rollup merge of #76615 - GuillaumeGomez:missing-examples-binary-ops, …
RalfJung Sep 12, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions library/core/src/array/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl<T, const N: usize> Iterator for IntoIter<T, N> {
// dead now (i.e. do not touch). As `idx` was the start of the
// alive-zone, the alive zone is now `data[alive]` again, restoring
// all invariants.
unsafe { self.data.get_unchecked(idx).read() }
unsafe { self.data.get_unchecked(idx).assume_init_read() }
})
}

Expand Down Expand Up @@ -136,7 +136,7 @@ impl<T, const N: usize> DoubleEndedIterator for IntoIter<T, N> {
// dead now (i.e. do not touch). As `idx` was the end of the
// alive-zone, the alive zone is now `data[alive]` again, restoring
// all invariants.
unsafe { self.data.get_unchecked(idx).read() }
unsafe { self.data.get_unchecked(idx).assume_init_read() }
})
}
}
Expand Down
49 changes: 40 additions & 9 deletions library/core/src/mem/maybe_uninit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::any::type_name;
use crate::fmt;
use crate::intrinsics;
use crate::mem::ManuallyDrop;
use crate::ptr;

/// A wrapper type to construct uninitialized instances of `T`.
///
Expand Down Expand Up @@ -471,6 +472,8 @@ impl<T> MaybeUninit<T> {
/// *immediate* undefined behavior, but will cause undefined behavior with most
/// safe operations (including dropping it).
///
/// [`Vec<T>`]: ../../std/vec/struct.Vec.html
///
/// # Examples
///
/// Correct usage of this method:
Expand Down Expand Up @@ -519,8 +522,8 @@ impl<T> MaybeUninit<T> {
/// this initialization invariant.
///
/// Moreover, this leaves a copy of the same data behind in the `MaybeUninit<T>`. When using
/// multiple copies of the data (by calling `read` multiple times, or first
/// calling `read` and then [`assume_init`]), it is your responsibility
/// multiple copies of the data (by calling `assume_init_read` multiple times, or first
/// calling `assume_init_read` and then [`assume_init`]), it is your responsibility
/// to ensure that that data may indeed be duplicated.
///
/// [inv]: #initialization-invariant
Expand All @@ -536,16 +539,16 @@ impl<T> MaybeUninit<T> {
///
/// let mut x = MaybeUninit::<u32>::uninit();
/// x.write(13);
/// let x1 = unsafe { x.read() };
/// let x1 = unsafe { x.assume_init_read() };
/// // `u32` is `Copy`, so we may read multiple times.
/// let x2 = unsafe { x.read() };
/// let x2 = unsafe { x.assume_init_read() };
/// assert_eq!(x1, x2);
///
/// let mut x = MaybeUninit::<Option<Vec<u32>>>::uninit();
/// x.write(None);
/// let x1 = unsafe { x.read() };
/// let x1 = unsafe { x.assume_init_read() };
/// // Duplicating a `None` value is okay, so we may read multiple times.
/// let x2 = unsafe { x.read() };
/// let x2 = unsafe { x.assume_init_read() };
/// assert_eq!(x1, x2);
/// ```
///
Expand All @@ -557,14 +560,14 @@ impl<T> MaybeUninit<T> {
///
/// let mut x = MaybeUninit::<Option<Vec<u32>>>::uninit();
/// x.write(Some(vec![0,1,2]));
/// let x1 = unsafe { x.read() };
/// let x2 = unsafe { x.read() };
/// let x1 = unsafe { x.assume_init_read() };
/// let x2 = unsafe { x.assume_init_read() };
/// // We now created two copies of the same vector, leading to a double-free ⚠️ when
/// // they both get dropped!
/// ```
#[unstable(feature = "maybe_uninit_extra", issue = "63567")]
#[inline(always)]
pub unsafe fn read(&self) -> T {
pub unsafe fn assume_init_read(&self) -> T {
// SAFETY: the caller must guarantee that `self` is initialized.
// Reading from `self.as_ptr()` is safe since `self` should be initialized.
unsafe {
Expand All @@ -573,6 +576,34 @@ impl<T> MaybeUninit<T> {
}
}

/// Drops the contained value in place.
///
/// If you have ownership of the `MaybeUninit`, you can use [`assume_init`] instead.
///
/// # Safety
///
/// It is up to the caller to guarantee that the `MaybeUninit<T>` really is
/// in an initialized state. Calling this when the content is not yet fully
/// initialized causes undefined behavior.
///
/// On top of that, all additional invariants of the type `T` must be
/// satisfied, as the `Drop` implementation of `T` (or its members) may
/// rely on this. For example, a `1`-initialized [`Vec<T>`] is considered
/// initialized (under the current implementation; this does not constitute
/// a stable guarantee) because the only requirement the compiler knows
/// about it is that the data pointer must be non-null. Dropping such a
/// `Vec<T>` however will cause undefined behaviour.
///
/// [`assume_init`]: MaybeUninit::assume_init
/// [`Vec<T>`]: ../../std/vec/struct.Vec.html
#[unstable(feature = "maybe_uninit_extra", issue = "63567")]
pub unsafe fn assume_init_drop(&mut self) {
// SAFETY: the caller must guarantee that `self` is initialized and
// satisfies all invariants of `T`.
// Dropping the value in place is safe if that is the case.
unsafe { ptr::drop_in_place(self.as_mut_ptr()) }
}

/// Gets a shared reference to the contained value.
///
/// This can be useful when we want to access a `MaybeUninit` that has been
Expand Down