Skip to content

Commit 2be1737

Browse files
Rename MaybeUninit::get_{ref,mut} to MaybeUninit::assume_init_{ref,mut}
This is the last remaining concern blocking the stabilization of the by-ref accessors of `MaybeUninit` (rust-lang#63568). This change of name not only does align with `.into_inner()` being called `.assume_init()`, it also conveys the dangerous semantics of the method in a much clearer and more direct way, reducing the change of misuse and thus of unsoundness.
1 parent 3804876 commit 2be1737

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

src/libcore/mem/maybe_uninit.rs

+17-17
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ impl<T> MaybeUninit<T> {
317317
pub fn write(&mut self, val: T) -> &mut T {
318318
unsafe {
319319
self.value = ManuallyDrop::new(val);
320-
self.get_mut()
320+
self.assume_init_mut()
321321
}
322322
}
323323

@@ -536,7 +536,7 @@ impl<T> MaybeUninit<T> {
536536
/// // create a shared reference to it:
537537
/// let x: &Vec<u32> = unsafe {
538538
/// // Safety: `x` has been initialized.
539-
/// x.get_ref()
539+
/// x.assume_init_ref()
540540
/// };
541541
/// assert_eq!(x, &vec![1, 2, 3]);
542542
/// ```
@@ -548,7 +548,7 @@ impl<T> MaybeUninit<T> {
548548
/// use std::mem::MaybeUninit;
549549
///
550550
/// let x = MaybeUninit::<Vec<u32>>::uninit();
551-
/// let x_vec: &Vec<u32> = unsafe { x.get_ref() };
551+
/// let x_vec: &Vec<u32> = unsafe { x.assume_init_ref() };
552552
/// // We have created a reference to an uninitialized vector! This is undefined behavior.
553553
/// ```
554554
///
@@ -559,14 +559,14 @@ impl<T> MaybeUninit<T> {
559559
/// let b = MaybeUninit::<Cell<bool>>::uninit();
560560
/// // Initialize the `MaybeUninit` using `Cell::set`:
561561
/// unsafe {
562-
/// b.get_ref().set(true);
563-
/// // ^^^^^^^^^^^
562+
/// b.assume_init_ref().set(true);
563+
/// // ^^^^^^^^^^^^^^^^^^^
564564
/// // Reference to an uninitialized `Cell<bool>`: UB!
565565
/// }
566566
/// ```
567567
#[unstable(feature = "maybe_uninit_ref", issue = "63568")]
568568
#[inline(always)]
569-
pub unsafe fn get_ref(&self) -> &T {
569+
pub unsafe fn assume_init_ref(&self) -> &T {
570570
intrinsics::panic_if_uninhabited::<T>();
571571
&*self.value
572572
}
@@ -581,7 +581,7 @@ impl<T> MaybeUninit<T> {
581581
///
582582
/// Calling this when the content is not yet fully initialized causes undefined
583583
/// behavior: it is up to the caller to guarantee that the `MaybeUninit<T>` really
584-
/// is in an initialized state. For instance, `.get_mut()` cannot be used to
584+
/// is in an initialized state. For instance, `.assume_init_mut()` cannot be used to
585585
/// initialize a `MaybeUninit`.
586586
///
587587
/// # Examples
@@ -609,7 +609,7 @@ impl<T> MaybeUninit<T> {
609609
/// // the `&mut MaybeUninit<[u8; 2048]>` to a `&mut [u8; 2048]`:
610610
/// let buf: &mut [u8; 2048] = unsafe {
611611
/// // Safety: `buf` has been initialized.
612-
/// buf.get_mut()
612+
/// buf.assume_init_mut()
613613
/// };
614614
///
615615
/// // Now we can use `buf` as a normal slice:
@@ -622,15 +622,15 @@ impl<T> MaybeUninit<T> {
622622
///
623623
/// ### *Incorrect* usages of this method:
624624
///
625-
/// You cannot use `.get_mut()` to initialize a value:
625+
/// You cannot use `.assume_init_mut()` to initialize a value:
626626
///
627627
/// ```rust,no_run
628628
/// #![feature(maybe_uninit_ref)]
629629
/// use std::mem::MaybeUninit;
630630
///
631631
/// let mut b = MaybeUninit::<bool>::uninit();
632632
/// unsafe {
633-
/// *b.get_mut() = true;
633+
/// *b.assume_init_mut() = true;
634634
/// // We have created a (mutable) reference to an uninitialized `bool`!
635635
/// // This is undefined behavior.
636636
/// }
@@ -647,8 +647,8 @@ impl<T> MaybeUninit<T> {
647647
/// fn read_chunk (reader: &'_ mut dyn io::Read) -> io::Result<[u8; 64]>
648648
/// {
649649
/// let mut buffer = MaybeUninit::<[u8; 64]>::uninit();
650-
/// reader.read_exact(unsafe { buffer.get_mut() })?;
651-
/// // ^^^^^^^^^^^^^^^^
650+
/// reader.read_exact(unsafe { buffer.assume_init_mut() })?;
651+
/// // ^^^^^^^^^^^^^^^^^^^^^^^^
652652
/// // (mutable) reference to uninitialized memory!
653653
/// // This is undefined behavior.
654654
/// Ok(unsafe { buffer.assume_init() })
@@ -668,12 +668,12 @@ impl<T> MaybeUninit<T> {
668668
///
669669
/// let foo: Foo = unsafe {
670670
/// let mut foo = MaybeUninit::<Foo>::uninit();
671-
/// ptr::write(&mut foo.get_mut().a as *mut u32, 1337);
672-
/// // ^^^^^^^^^^^^^
671+
/// ptr::write(&mut foo.assume_init_mut().a as *mut u32, 1337);
672+
/// // ^^^^^^^^^^^^^^^^^^^^^
673673
/// // (mutable) reference to uninitialized memory!
674674
/// // This is undefined behavior.
675-
/// ptr::write(&mut foo.get_mut().b as *mut u8, 42);
676-
/// // ^^^^^^^^^^^^^
675+
/// ptr::write(&mut foo.assume_init_mut().b as *mut u8, 42);
676+
/// // ^^^^^^^^^^^^^^^^^^^^^
677677
/// // (mutable) reference to uninitialized memory!
678678
/// // This is undefined behavior.
679679
/// foo.assume_init()
@@ -684,7 +684,7 @@ impl<T> MaybeUninit<T> {
684684
// a final decision about the rules before stabilization.
685685
#[unstable(feature = "maybe_uninit_ref", issue = "63568")]
686686
#[inline(always)]
687-
pub unsafe fn get_mut(&mut self) -> &mut T {
687+
pub unsafe fn assume_init_mut(&mut self) -> &mut T {
688688
intrinsics::panic_if_uninhabited::<T>();
689689
&mut *self.value
690690
}

0 commit comments

Comments
 (0)