Skip to content

Commit d9cd4a3

Browse files
committed
Auto merge of rust-lang#76047 - Dylan-DPC:rename/maybe, r=RalfJung
rename get_{ref, mut} to assume_init_{ref,mut} in Maybeuninit References rust-lang#63568 Rework with comments addressed from rust-lang#66174 Have replaced most of the occurrences I've found, hopefully didn't miss out anything r? @RalfJung (thanks @danielhenrymantilla for the initial work on this)
2 parents 445f34b + 943911c commit d9cd4a3

File tree

4 files changed

+41
-41
lines changed

4 files changed

+41
-41
lines changed

library/core/src/fmt/float.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ fn float_to_decimal_common_exact<T>(
1414
where
1515
T: flt2dec::DecodableFloat,
1616
{
17-
// SAFETY: Possible undefined behavior, see FIXME(#53491)
17+
// SAFETY: Possible undefined behavior, see FIXME(#76092)
1818
unsafe {
1919
let mut buf = MaybeUninit::<[u8; 1024]>::uninit(); // enough for f32 and f64
2020
let mut parts = MaybeUninit::<[flt2dec::Part<'_>; 4]>::uninit();
21-
// FIXME(#53491): This is calling `get_mut` on an uninitialized
21+
// FIXME(#76092): This is calling `assume_init_mut` on an uninitialized
2222
// `MaybeUninit` (here and elsewhere in this file). Revisit this once
2323
// we decided whether that is valid or not.
2424
// We can do this only because we are libstd and coupled to the compiler.
@@ -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
}
@@ -47,19 +47,19 @@ fn float_to_decimal_common_shortest<T>(
4747
where
4848
T: flt2dec::DecodableFloat,
4949
{
50-
// SAFETY: Possible undefined behavior, see FIXME(#53491)
50+
// SAFETY: Possible undefined behavior, see FIXME(#76092)
5151
unsafe {
5252
// enough for f32 and f64
5353
let mut buf = MaybeUninit::<[u8; flt2dec::MAX_SIG_DIGITS]>::uninit();
5454
let mut parts = MaybeUninit::<[flt2dec::Part<'_>; 4]>::uninit();
55-
// FIXME(#53491)
55+
// FIXME(#76092)
5656
let formatted = flt2dec::to_shortest_str(
5757
flt2dec::strategy::grisu::format_shortest,
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
}
@@ -103,19 +103,19 @@ fn float_to_exponential_common_exact<T>(
103103
where
104104
T: flt2dec::DecodableFloat,
105105
{
106-
// SAFETY: Possible undefined behavior, see FIXME(#53491)
106+
// SAFETY: Possible undefined behavior, see FIXME(#76092)
107107
unsafe {
108108
let mut buf = MaybeUninit::<[u8; 1024]>::uninit(); // enough for f32 and f64
109109
let mut parts = MaybeUninit::<[flt2dec::Part<'_>; 6]>::uninit();
110-
// FIXME(#53491)
110+
// FIXME(#76092)
111111
let formatted = flt2dec::to_exact_exp_str(
112112
flt2dec::strategy::grisu::format_exact,
113113
*num,
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
}
@@ -133,20 +133,20 @@ fn float_to_exponential_common_shortest<T>(
133133
where
134134
T: flt2dec::DecodableFloat,
135135
{
136-
// SAFETY: Possible undefined behavior, see FIXME(#53491)
136+
// SAFETY: Possible undefined behavior, see FIXME(#76092)
137137
unsafe {
138138
// enough for f32 and f64
139139
let mut buf = MaybeUninit::<[u8; flt2dec::MAX_SIG_DIGITS]>::uninit();
140140
let mut parts = MaybeUninit::<[flt2dec::Part<'_>; 6]>::uninit();
141-
// FIXME(#53491)
141+
// FIXME(#76092)
142142
let formatted = flt2dec::to_shortest_exp_str(
143143
flt2dec::strategy::grisu::format_shortest,
144144
*num,
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

+19-19
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ impl<T> MaybeUninit<T> {
369369
pub fn write(&mut self, val: T) -> &mut T {
370370
unsafe {
371371
self.value = ManuallyDrop::new(val);
372-
self.get_mut()
372+
self.assume_init_mut()
373373
}
374374
}
375375

@@ -601,7 +601,7 @@ impl<T> MaybeUninit<T> {
601601
/// // create a shared reference to it:
602602
/// let x: &Vec<u32> = unsafe {
603603
/// // Safety: `x` has been initialized.
604-
/// x.get_ref()
604+
/// x.assume_init_ref()
605605
/// };
606606
/// assert_eq!(x, &vec![1, 2, 3]);
607607
/// ```
@@ -613,7 +613,7 @@ impl<T> MaybeUninit<T> {
613613
/// use std::mem::MaybeUninit;
614614
///
615615
/// let x = MaybeUninit::<Vec<u32>>::uninit();
616-
/// let x_vec: &Vec<u32> = unsafe { x.get_ref() };
616+
/// let x_vec: &Vec<u32> = unsafe { x.assume_init_ref() };
617617
/// // We have created a reference to an uninitialized vector! This is undefined behavior. ⚠️
618618
/// ```
619619
///
@@ -624,14 +624,14 @@ impl<T> MaybeUninit<T> {
624624
/// let b = MaybeUninit::<Cell<bool>>::uninit();
625625
/// // Initialize the `MaybeUninit` using `Cell::set`:
626626
/// unsafe {
627-
/// b.get_ref().set(true);
628-
/// // ^^^^^^^^^^^
629-
/// // Reference to an uninitialized `Cell<bool>`: UB!
627+
/// b.assume_init_ref().set(true);
628+
/// // ^^^^^^^^^^^^^^^
629+
/// // Reference to an uninitialized `Cell<bool>`: UB!
630630
/// }
631631
/// ```
632632
#[unstable(feature = "maybe_uninit_ref", issue = "63568")]
633633
#[inline(always)]
634-
pub unsafe fn get_ref(&self) -> &T {
634+
pub unsafe fn assume_init_ref(&self) -> &T {
635635
// SAFETY: the caller must guarantee that `self` is initialized.
636636
// This also means that `self` must be a `value` variant.
637637
unsafe {
@@ -650,7 +650,7 @@ impl<T> MaybeUninit<T> {
650650
///
651651
/// Calling this when the content is not yet fully initialized causes undefined
652652
/// 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
653+
/// is in an initialized state. For instance, `.assume_init_mut()` cannot be used to
654654
/// initialize a `MaybeUninit`.
655655
///
656656
/// # Examples
@@ -678,7 +678,7 @@ impl<T> MaybeUninit<T> {
678678
/// // the `&mut MaybeUninit<[u8; 2048]>` to a `&mut [u8; 2048]`:
679679
/// let buf: &mut [u8; 2048] = unsafe {
680680
/// // Safety: `buf` has been initialized.
681-
/// buf.get_mut()
681+
/// buf.assume_init_mut()
682682
/// };
683683
///
684684
/// // Now we can use `buf` as a normal slice:
@@ -691,15 +691,15 @@ impl<T> MaybeUninit<T> {
691691
///
692692
/// ### *Incorrect* usages of this method:
693693
///
694-
/// You cannot use `.get_mut()` to initialize a value:
694+
/// You cannot use `.assume_init_mut()` to initialize a value:
695695
///
696696
/// ```rust,no_run
697697
/// #![feature(maybe_uninit_ref)]
698698
/// use std::mem::MaybeUninit;
699699
///
700700
/// let mut b = MaybeUninit::<bool>::uninit();
701701
/// unsafe {
702-
/// *b.get_mut() = true;
702+
/// *b.assume_init_mut() = true;
703703
/// // We have created a (mutable) reference to an uninitialized `bool`!
704704
/// // This is undefined behavior. ⚠️
705705
/// }
@@ -716,8 +716,8 @@ impl<T> MaybeUninit<T> {
716716
/// fn read_chunk (reader: &'_ mut dyn io::Read) -> io::Result<[u8; 64]>
717717
/// {
718718
/// let mut buffer = MaybeUninit::<[u8; 64]>::uninit();
719-
/// reader.read_exact(unsafe { buffer.get_mut() })?;
720-
/// // ^^^^^^^^^^^^^^^^
719+
/// reader.read_exact(unsafe { buffer.assume_init_mut() })?;
720+
/// // ^^^^^^^^^^^^^^^^^^^^^^^^
721721
/// // (mutable) reference to uninitialized memory!
722722
/// // This is undefined behavior.
723723
/// Ok(unsafe { buffer.assume_init() })
@@ -737,23 +737,23 @@ impl<T> MaybeUninit<T> {
737737
///
738738
/// let foo: Foo = unsafe {
739739
/// let mut foo = MaybeUninit::<Foo>::uninit();
740-
/// ptr::write(&mut foo.get_mut().a as *mut u32, 1337);
741-
/// // ^^^^^^^^^^^^^
740+
/// ptr::write(&mut foo.assume_init_mut().a as *mut u32, 1337);
741+
/// // ^^^^^^^^^^^^^^^^^^^^^
742742
/// // (mutable) reference to uninitialized memory!
743743
/// // This is undefined behavior.
744-
/// ptr::write(&mut foo.get_mut().b as *mut u8, 42);
745-
/// // ^^^^^^^^^^^^^
744+
/// ptr::write(&mut foo.assume_init_mut().b as *mut u8, 42);
745+
/// // ^^^^^^^^^^^^^^^^^^^^^
746746
/// // (mutable) reference to uninitialized memory!
747747
/// // This is undefined behavior.
748748
/// foo.assume_init()
749749
/// };
750750
/// ```
751-
// FIXME(#53491): We currently rely on the above being incorrect, i.e., we have references
751+
// FIXME(#76092): We currently rely on the above being incorrect, i.e., we have references
752752
// to uninitialized data (e.g., in `libcore/fmt/float.rs`). We should make
753753
// a final decision about the rules before stabilization.
754754
#[unstable(feature = "maybe_uninit_ref", issue = "63568")]
755755
#[inline(always)]
756-
pub unsafe fn get_mut(&mut self) -> &mut T {
756+
pub unsafe fn assume_init_mut(&mut self) -> &mut T {
757757
// SAFETY: the caller must guarantee that `self` is initialized.
758758
// This also means that `self` must be a `value` variant.
759759
unsafe {

library/std/src/io/util.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -52,24 +52,24 @@ where
5252
W: Write,
5353
{
5454
let mut buf = MaybeUninit::<[u8; super::DEFAULT_BUF_SIZE]>::uninit();
55-
// FIXME(#53491): This is calling `get_mut` and `get_ref` on an uninitialized
55+
// FIXME(#76092): This is calling `get_mut` and `get_ref` on an uninitialized
5656
// `MaybeUninit`. Revisit this once we decided whether that is valid or not.
5757
// This is still technically undefined behavior due to creating a reference
5858
// to uninitialized data, but within libstd we can rely on more guarantees
5959
// than if this code were in an external lib.
6060
unsafe {
61-
reader.initializer().initialize(buf.get_mut());
61+
reader.initializer().initialize(buf.assume_init_mut());
6262
}
6363

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

library/std/src/lazy.rs

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

385385
/// Safety: The value must be initialized
386386
unsafe fn get_unchecked_mut(&mut self) -> &mut T {
387387
debug_assert!(self.is_initialized());
388-
(&mut *self.value.get()).get_mut()
388+
(&mut *self.value.get()).assume_init_mut()
389389
}
390390
}
391391

0 commit comments

Comments
 (0)