-
Notifications
You must be signed in to change notification settings - Fork 13.4k
rename get_{ref, mut} to assume_init_{ref,mut} in Maybeuninit #76047
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
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -167,7 +167,8 @@ use crate::mem::ManuallyDrop; | |
/// | ||
/// // For each item in the array, drop if we allocated it. | ||
/// for elem in &mut data[0..data_len] { | ||
/// unsafe { ptr::drop_in_place(elem.as_mut_ptr()); } | ||
/// unsafe { ptr::drop_in_place(elem. | ||
/// ptr()); } | ||
RalfJung marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// } | ||
/// ``` | ||
/// | ||
|
@@ -369,7 +370,7 @@ impl<T> MaybeUninit<T> { | |
pub fn write(&mut self, val: T) -> &mut T { | ||
unsafe { | ||
self.value = ManuallyDrop::new(val); | ||
self.get_mut() | ||
self.assume_init_mut() | ||
} | ||
} | ||
|
||
|
@@ -601,7 +602,7 @@ impl<T> MaybeUninit<T> { | |
/// // create a shared reference to it: | ||
/// let x: &Vec<u32> = unsafe { | ||
/// // Safety: `x` has been initialized. | ||
/// x.get_ref() | ||
/// x.assume_init_ref() | ||
/// }; | ||
/// assert_eq!(x, &vec![1, 2, 3]); | ||
/// ``` | ||
|
@@ -613,7 +614,7 @@ impl<T> MaybeUninit<T> { | |
/// use std::mem::MaybeUninit; | ||
/// | ||
/// let x = MaybeUninit::<Vec<u32>>::uninit(); | ||
/// let x_vec: &Vec<u32> = unsafe { x.get_ref() }; | ||
/// let x_vec: &Vec<u32> = unsafe { x.assume_init_ref() }; | ||
/// // We have created a reference to an uninitialized vector! This is undefined behavior. ⚠️ | ||
/// ``` | ||
/// | ||
|
@@ -624,14 +625,14 @@ impl<T> MaybeUninit<T> { | |
/// let b = MaybeUninit::<Cell<bool>>::uninit(); | ||
/// // Initialize the `MaybeUninit` using `Cell::set`: | ||
/// unsafe { | ||
/// b.get_ref().set(true); | ||
/// b.assume_init_ref().set(true); | ||
/// // ^^^^^^^^^^^ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
/// // Reference to an uninitialized `Cell<bool>`: UB! | ||
/// } | ||
/// ``` | ||
#[unstable(feature = "maybe_uninit_ref", issue = "63568")] | ||
#[inline(always)] | ||
pub unsafe fn get_ref(&self) -> &T { | ||
pub unsafe fn assume_init_ref(&self) -> &T { | ||
// SAFETY: the caller must guarantee that `self` is initialized. | ||
// This also means that `self` must be a `value` variant. | ||
unsafe { | ||
|
@@ -650,7 +651,7 @@ impl<T> MaybeUninit<T> { | |
/// | ||
/// Calling this when the content is not yet fully initialized causes undefined | ||
/// behavior: it is up to the caller to guarantee that the `MaybeUninit<T>` really | ||
/// is in an initialized state. For instance, `.get_mut()` cannot be used to | ||
/// is in an initialized state. For instance, `.assume_init_mut()` cannot be used to | ||
/// initialize a `MaybeUninit`. | ||
/// | ||
/// # Examples | ||
|
@@ -678,7 +679,7 @@ impl<T> MaybeUninit<T> { | |
/// // the `&mut MaybeUninit<[u8; 2048]>` to a `&mut [u8; 2048]`: | ||
/// let buf: &mut [u8; 2048] = unsafe { | ||
/// // Safety: `buf` has been initialized. | ||
/// buf.get_mut() | ||
/// buf.assume_init_mut() | ||
/// }; | ||
/// | ||
/// // Now we can use `buf` as a normal slice: | ||
|
@@ -691,15 +692,15 @@ impl<T> MaybeUninit<T> { | |
/// | ||
/// ### *Incorrect* usages of this method: | ||
/// | ||
/// You cannot use `.get_mut()` to initialize a value: | ||
/// You cannot use `.assume_init_mut()` to initialize a value: | ||
/// | ||
/// ```rust,no_run | ||
/// #![feature(maybe_uninit_ref)] | ||
/// use std::mem::MaybeUninit; | ||
/// | ||
/// let mut b = MaybeUninit::<bool>::uninit(); | ||
/// unsafe { | ||
/// *b.get_mut() = true; | ||
/// *b.assume_init_mut() = true; | ||
/// // We have created a (mutable) reference to an uninitialized `bool`! | ||
/// // This is undefined behavior. ⚠️ | ||
/// } | ||
|
@@ -716,7 +717,7 @@ impl<T> MaybeUninit<T> { | |
/// fn read_chunk (reader: &'_ mut dyn io::Read) -> io::Result<[u8; 64]> | ||
/// { | ||
/// let mut buffer = MaybeUninit::<[u8; 64]>::uninit(); | ||
/// reader.read_exact(unsafe { buffer.get_mut() })?; | ||
/// reader.read_exact(unsafe { buffer.assume_init_mut() })?; | ||
/// // ^^^^^^^^^^^^^^^^ | ||
RalfJung marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// // (mutable) reference to uninitialized memory! | ||
/// // This is undefined behavior. | ||
|
@@ -737,11 +738,11 @@ impl<T> MaybeUninit<T> { | |
/// | ||
/// let foo: Foo = unsafe { | ||
/// let mut foo = MaybeUninit::<Foo>::uninit(); | ||
/// ptr::write(&mut foo.get_mut().a as *mut u32, 1337); | ||
/// ptr::write(&mut foo.assume_init_mut().a as *mut u32, 1337); | ||
/// // ^^^^^^^^^^^^^ | ||
/// // (mutable) reference to uninitialized memory! | ||
/// // This is undefined behavior. | ||
/// ptr::write(&mut foo.get_mut().b as *mut u8, 42); | ||
/// ptr::write(&mut foo.assume_init_mut().b as *mut u8, 42); | ||
/// // ^^^^^^^^^^^^^ | ||
/// // (mutable) reference to uninitialized memory! | ||
/// // This is undefined behavior. | ||
|
@@ -753,7 +754,7 @@ impl<T> MaybeUninit<T> { | |
// a final decision about the rules before stabilization. | ||
#[unstable(feature = "maybe_uninit_ref", issue = "63568")] | ||
#[inline(always)] | ||
pub unsafe fn get_mut(&mut self) -> &mut T { | ||
pub unsafe fn assume_init_mut(&mut self) -> &mut T { | ||
// SAFETY: the caller must guarantee that `self` is initialized. | ||
// This also means that `self` must be a `value` variant. | ||
unsafe { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.