Skip to content

Commit 5e208ef

Browse files
committed
rename get_{ref, mut} to assume_init_{ref,mut} in Maybeuninit
1 parent bf43421 commit 5e208ef

File tree

4 files changed

+29
-28
lines changed

4 files changed

+29
-28
lines changed

library/core/src/fmt/float.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ where
2828
*num,
2929
sign,
3030
precision,
31-
buf.get_mut(),
32-
parts.get_mut(),
31+
buf.assume_init_mut(),
32+
parts.assume_init_mut(),
3333
);
3434
fmt.pad_formatted_parts(&formatted)
3535
}
@@ -58,8 +58,8 @@ where
5858
*num,
5959
sign,
6060
precision,
61-
buf.get_mut(),
62-
parts.get_mut(),
61+
buf.assume_init_mut(),
62+
parts.assume_init_mut(),
6363
);
6464
fmt.pad_formatted_parts(&formatted)
6565
}
@@ -114,8 +114,8 @@ where
114114
sign,
115115
precision,
116116
upper,
117-
buf.get_mut(),
118-
parts.get_mut(),
117+
buf.assume_init_mut(),
118+
parts.assume_init_mut(),
119119
);
120120
fmt.pad_formatted_parts(&formatted)
121121
}
@@ -145,8 +145,8 @@ where
145145
sign,
146146
(0, 0),
147147
upper,
148-
buf.get_mut(),
149-
parts.get_mut(),
148+
buf.assume_init_mut(),
149+
parts.assume_init_mut(),
150150
);
151151
fmt.pad_formatted_parts(&formatted)
152152
}

library/core/src/mem/maybe_uninit.rs

+16-15
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ use crate::mem::ManuallyDrop;
167167
///
168168
/// // For each item in the array, drop if we allocated it.
169169
/// for elem in &mut data[0..data_len] {
170-
/// unsafe { ptr::drop_in_place(elem.as_mut_ptr()); }
170+
/// unsafe { ptr::drop_in_place(elem.
171+
/// ptr()); }
171172
/// }
172173
/// ```
173174
///
@@ -369,7 +370,7 @@ impl<T> MaybeUninit<T> {
369370
pub fn write(&mut self, val: T) -> &mut T {
370371
unsafe {
371372
self.value = ManuallyDrop::new(val);
372-
self.get_mut()
373+
self.assume_init_mut()
373374
}
374375
}
375376

@@ -601,7 +602,7 @@ impl<T> MaybeUninit<T> {
601602
/// // create a shared reference to it:
602603
/// let x: &Vec<u32> = unsafe {
603604
/// // Safety: `x` has been initialized.
604-
/// x.get_ref()
605+
/// x.assume_init_ref()
605606
/// };
606607
/// assert_eq!(x, &vec![1, 2, 3]);
607608
/// ```
@@ -613,7 +614,7 @@ impl<T> MaybeUninit<T> {
613614
/// use std::mem::MaybeUninit;
614615
///
615616
/// let x = MaybeUninit::<Vec<u32>>::uninit();
616-
/// let x_vec: &Vec<u32> = unsafe { x.get_ref() };
617+
/// let x_vec: &Vec<u32> = unsafe { x.assume_init_ref() };
617618
/// // We have created a reference to an uninitialized vector! This is undefined behavior. ⚠️
618619
/// ```
619620
///
@@ -624,14 +625,14 @@ impl<T> MaybeUninit<T> {
624625
/// let b = MaybeUninit::<Cell<bool>>::uninit();
625626
/// // Initialize the `MaybeUninit` using `Cell::set`:
626627
/// unsafe {
627-
/// b.get_ref().set(true);
628+
/// b.assume_init_ref().set(true);
628629
/// // ^^^^^^^^^^^
629630
/// // Reference to an uninitialized `Cell<bool>`: UB!
630631
/// }
631632
/// ```
632633
#[unstable(feature = "maybe_uninit_ref", issue = "63568")]
633634
#[inline(always)]
634-
pub unsafe fn get_ref(&self) -> &T {
635+
pub unsafe fn assume_init_ref(&self) -> &T {
635636
// SAFETY: the caller must guarantee that `self` is initialized.
636637
// This also means that `self` must be a `value` variant.
637638
unsafe {
@@ -650,7 +651,7 @@ impl<T> MaybeUninit<T> {
650651
///
651652
/// Calling this when the content is not yet fully initialized causes undefined
652653
/// behavior: it is up to the caller to guarantee that the `MaybeUninit<T>` really
653-
/// is in an initialized state. For instance, `.get_mut()` cannot be used to
654+
/// is in an initialized state. For instance, `.assume_init_mut()` cannot be used to
654655
/// initialize a `MaybeUninit`.
655656
///
656657
/// # Examples
@@ -678,7 +679,7 @@ impl<T> MaybeUninit<T> {
678679
/// // the `&mut MaybeUninit<[u8; 2048]>` to a `&mut [u8; 2048]`:
679680
/// let buf: &mut [u8; 2048] = unsafe {
680681
/// // Safety: `buf` has been initialized.
681-
/// buf.get_mut()
682+
/// buf.assume_init_ref()
682683
/// };
683684
///
684685
/// // Now we can use `buf` as a normal slice:
@@ -691,15 +692,15 @@ impl<T> MaybeUninit<T> {
691692
///
692693
/// ### *Incorrect* usages of this method:
693694
///
694-
/// You cannot use `.get_mut()` to initialize a value:
695+
/// You cannot use `.assume_init_mut()` to initialize a value:
695696
///
696697
/// ```rust,no_run
697698
/// #![feature(maybe_uninit_ref)]
698699
/// use std::mem::MaybeUninit;
699700
///
700701
/// let mut b = MaybeUninit::<bool>::uninit();
701702
/// unsafe {
702-
/// *b.get_mut() = true;
703+
/// *b.assume_init_mut() = true;
703704
/// // We have created a (mutable) reference to an uninitialized `bool`!
704705
/// // This is undefined behavior. ⚠️
705706
/// }
@@ -716,7 +717,7 @@ impl<T> MaybeUninit<T> {
716717
/// fn read_chunk (reader: &'_ mut dyn io::Read) -> io::Result<[u8; 64]>
717718
/// {
718719
/// let mut buffer = MaybeUninit::<[u8; 64]>::uninit();
719-
/// reader.read_exact(unsafe { buffer.get_mut() })?;
720+
/// reader.read_exact(unsafe { buffer.assume_init_mut() })?;
720721
/// // ^^^^^^^^^^^^^^^^
721722
/// // (mutable) reference to uninitialized memory!
722723
/// // This is undefined behavior.
@@ -737,23 +738,23 @@ impl<T> MaybeUninit<T> {
737738
///
738739
/// let foo: Foo = unsafe {
739740
/// let mut foo = MaybeUninit::<Foo>::uninit();
740-
/// ptr::write(&mut foo.get_mut().a as *mut u32, 1337);
741+
/// ptr::write(&mut foo.assume_init_mut().a as *mut u32, 1337);
741742
/// // ^^^^^^^^^^^^^
742743
/// // (mutable) reference to uninitialized memory!
743744
/// // This is undefined behavior.
744-
/// ptr::write(&mut foo.get_mut().b as *mut u8, 42);
745+
/// ptr::write(&mut foo.assume_init_mut().b as *mut u8, 42);
745746
/// // ^^^^^^^^^^^^^
746747
/// // (mutable) reference to uninitialized memory!
747748
/// // This is undefined behavior.
748-
/// foo.assume_init()
749+
/// foo.assume_init_mut()
749750
/// };
750751
/// ```
751752
// FIXME(#53491): We currently rely on the above being incorrect, i.e., we have references
752753
// to uninitialized data (e.g., in `libcore/fmt/float.rs`). We should make
753754
// a final decision about the rules before stabilization.
754755
#[unstable(feature = "maybe_uninit_ref", issue = "63568")]
755756
#[inline(always)]
756-
pub unsafe fn get_mut(&mut self) -> &mut T {
757+
pub unsafe fn assume_init_mut(&mut self) -> &mut T {
757758
// SAFETY: the caller must guarantee that `self` is initialized.
758759
// This also means that `self` must be a `value` variant.
759760
unsafe {

library/std/src/io/util.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,18 @@ where
5555
// to uninitialized data, but within libstd we can rely on more guarantees
5656
// than if this code were in an external lib.
5757
unsafe {
58-
reader.initializer().initialize(buf.get_mut());
58+
reader.initializer().initialize(buf.assume_init_mut());
5959
}
6060

6161
let mut written = 0;
6262
loop {
63-
let len = match reader.read(unsafe { buf.get_mut() }) {
63+
let len = match reader.read(unsafe { buf.assume_init_mut() }) {
6464
Ok(0) => return Ok(written),
6565
Ok(len) => len,
6666
Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
6767
Err(e) => return Err(e),
6868
};
69-
writer.write_all(unsafe { &buf.get_ref()[..len] })?;
69+
writer.write_all(unsafe { &buf.assume_init_ref()[..len] })?;
7070
written += len as u64;
7171
}
7272
}

library/std/src/lazy.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -376,13 +376,13 @@ impl<T> SyncOnceCell<T> {
376376
/// Safety: The value must be initialized
377377
unsafe fn get_unchecked(&self) -> &T {
378378
debug_assert!(self.is_initialized());
379-
(&*self.value.get()).get_ref()
379+
(&*self.value.get()).assume_init_ref()
380380
}
381381

382382
/// Safety: The value must be initialized
383383
unsafe fn get_unchecked_mut(&mut self) -> &mut T {
384384
debug_assert!(self.is_initialized());
385-
(&mut *self.value.get()).get_mut()
385+
(&mut *self.value.get()).assume_init_mut()
386386
}
387387
}
388388

0 commit comments

Comments
 (0)