rename get_{ref, mut} to assume_init_{ref,mut} in Maybeuninit
This commit is contained in:
parent
bf4342114e
commit
5e208efaa8
4 changed files with 29 additions and 28 deletions
|
@ -28,8 +28,8 @@ where
|
||||||
*num,
|
*num,
|
||||||
sign,
|
sign,
|
||||||
precision,
|
precision,
|
||||||
buf.get_mut(),
|
buf.assume_init_mut(),
|
||||||
parts.get_mut(),
|
parts.assume_init_mut(),
|
||||||
);
|
);
|
||||||
fmt.pad_formatted_parts(&formatted)
|
fmt.pad_formatted_parts(&formatted)
|
||||||
}
|
}
|
||||||
|
@ -58,8 +58,8 @@ where
|
||||||
*num,
|
*num,
|
||||||
sign,
|
sign,
|
||||||
precision,
|
precision,
|
||||||
buf.get_mut(),
|
buf.assume_init_mut(),
|
||||||
parts.get_mut(),
|
parts.assume_init_mut(),
|
||||||
);
|
);
|
||||||
fmt.pad_formatted_parts(&formatted)
|
fmt.pad_formatted_parts(&formatted)
|
||||||
}
|
}
|
||||||
|
@ -114,8 +114,8 @@ where
|
||||||
sign,
|
sign,
|
||||||
precision,
|
precision,
|
||||||
upper,
|
upper,
|
||||||
buf.get_mut(),
|
buf.assume_init_mut(),
|
||||||
parts.get_mut(),
|
parts.assume_init_mut(),
|
||||||
);
|
);
|
||||||
fmt.pad_formatted_parts(&formatted)
|
fmt.pad_formatted_parts(&formatted)
|
||||||
}
|
}
|
||||||
|
@ -145,8 +145,8 @@ where
|
||||||
sign,
|
sign,
|
||||||
(0, 0),
|
(0, 0),
|
||||||
upper,
|
upper,
|
||||||
buf.get_mut(),
|
buf.assume_init_mut(),
|
||||||
parts.get_mut(),
|
parts.assume_init_mut(),
|
||||||
);
|
);
|
||||||
fmt.pad_formatted_parts(&formatted)
|
fmt.pad_formatted_parts(&formatted)
|
||||||
}
|
}
|
||||||
|
|
|
@ -167,7 +167,8 @@ use crate::mem::ManuallyDrop;
|
||||||
///
|
///
|
||||||
/// // For each item in the array, drop if we allocated it.
|
/// // For each item in the array, drop if we allocated it.
|
||||||
/// for elem in &mut data[0..data_len] {
|
/// for elem in &mut data[0..data_len] {
|
||||||
/// unsafe { ptr::drop_in_place(elem.as_mut_ptr()); }
|
/// unsafe { ptr::drop_in_place(elem.
|
||||||
|
/// ptr()); }
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
|
@ -369,7 +370,7 @@ impl<T> MaybeUninit<T> {
|
||||||
pub fn write(&mut self, val: T) -> &mut T {
|
pub fn write(&mut self, val: T) -> &mut T {
|
||||||
unsafe {
|
unsafe {
|
||||||
self.value = ManuallyDrop::new(val);
|
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:
|
/// // create a shared reference to it:
|
||||||
/// let x: &Vec<u32> = unsafe {
|
/// let x: &Vec<u32> = unsafe {
|
||||||
/// // Safety: `x` has been initialized.
|
/// // Safety: `x` has been initialized.
|
||||||
/// x.get_ref()
|
/// x.assume_init_ref()
|
||||||
/// };
|
/// };
|
||||||
/// assert_eq!(x, &vec![1, 2, 3]);
|
/// assert_eq!(x, &vec![1, 2, 3]);
|
||||||
/// ```
|
/// ```
|
||||||
|
@ -613,7 +614,7 @@ impl<T> MaybeUninit<T> {
|
||||||
/// use std::mem::MaybeUninit;
|
/// use std::mem::MaybeUninit;
|
||||||
///
|
///
|
||||||
/// let x = MaybeUninit::<Vec<u32>>::uninit();
|
/// 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. ⚠️
|
/// // 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();
|
/// let b = MaybeUninit::<Cell<bool>>::uninit();
|
||||||
/// // Initialize the `MaybeUninit` using `Cell::set`:
|
/// // Initialize the `MaybeUninit` using `Cell::set`:
|
||||||
/// unsafe {
|
/// unsafe {
|
||||||
/// b.get_ref().set(true);
|
/// b.assume_init_ref().set(true);
|
||||||
/// // ^^^^^^^^^^^
|
/// // ^^^^^^^^^^^
|
||||||
/// // Reference to an uninitialized `Cell<bool>`: UB!
|
/// // Reference to an uninitialized `Cell<bool>`: UB!
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
#[unstable(feature = "maybe_uninit_ref", issue = "63568")]
|
#[unstable(feature = "maybe_uninit_ref", issue = "63568")]
|
||||||
#[inline(always)]
|
#[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.
|
// SAFETY: the caller must guarantee that `self` is initialized.
|
||||||
// This also means that `self` must be a `value` variant.
|
// This also means that `self` must be a `value` variant.
|
||||||
unsafe {
|
unsafe {
|
||||||
|
@ -650,7 +651,7 @@ impl<T> MaybeUninit<T> {
|
||||||
///
|
///
|
||||||
/// Calling this when the content is not yet fully initialized causes undefined
|
/// 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
|
/// 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`.
|
/// initialize a `MaybeUninit`.
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
|
@ -678,7 +679,7 @@ impl<T> MaybeUninit<T> {
|
||||||
/// // the `&mut MaybeUninit<[u8; 2048]>` to a `&mut [u8; 2048]`:
|
/// // the `&mut MaybeUninit<[u8; 2048]>` to a `&mut [u8; 2048]`:
|
||||||
/// let buf: &mut [u8; 2048] = unsafe {
|
/// let buf: &mut [u8; 2048] = unsafe {
|
||||||
/// // Safety: `buf` has been initialized.
|
/// // Safety: `buf` has been initialized.
|
||||||
/// buf.get_mut()
|
/// buf.assume_init_ref()
|
||||||
/// };
|
/// };
|
||||||
///
|
///
|
||||||
/// // Now we can use `buf` as a normal slice:
|
/// // Now we can use `buf` as a normal slice:
|
||||||
|
@ -691,7 +692,7 @@ impl<T> MaybeUninit<T> {
|
||||||
///
|
///
|
||||||
/// ### *Incorrect* usages of this method:
|
/// ### *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
|
/// ```rust,no_run
|
||||||
/// #![feature(maybe_uninit_ref)]
|
/// #![feature(maybe_uninit_ref)]
|
||||||
|
@ -699,7 +700,7 @@ impl<T> MaybeUninit<T> {
|
||||||
///
|
///
|
||||||
/// let mut b = MaybeUninit::<bool>::uninit();
|
/// let mut b = MaybeUninit::<bool>::uninit();
|
||||||
/// unsafe {
|
/// unsafe {
|
||||||
/// *b.get_mut() = true;
|
/// *b.assume_init_mut() = true;
|
||||||
/// // We have created a (mutable) reference to an uninitialized `bool`!
|
/// // We have created a (mutable) reference to an uninitialized `bool`!
|
||||||
/// // This is undefined behavior. ⚠️
|
/// // This is undefined behavior. ⚠️
|
||||||
/// }
|
/// }
|
||||||
|
@ -716,7 +717,7 @@ impl<T> MaybeUninit<T> {
|
||||||
/// fn read_chunk (reader: &'_ mut dyn io::Read) -> io::Result<[u8; 64]>
|
/// fn read_chunk (reader: &'_ mut dyn io::Read) -> io::Result<[u8; 64]>
|
||||||
/// {
|
/// {
|
||||||
/// let mut buffer = MaybeUninit::<[u8; 64]>::uninit();
|
/// let mut buffer = MaybeUninit::<[u8; 64]>::uninit();
|
||||||
/// reader.read_exact(unsafe { buffer.get_mut() })?;
|
/// reader.read_exact(unsafe { buffer.assume_init_mut() })?;
|
||||||
/// // ^^^^^^^^^^^^^^^^
|
/// // ^^^^^^^^^^^^^^^^
|
||||||
/// // (mutable) reference to uninitialized memory!
|
/// // (mutable) reference to uninitialized memory!
|
||||||
/// // This is undefined behavior.
|
/// // This is undefined behavior.
|
||||||
|
@ -737,15 +738,15 @@ impl<T> MaybeUninit<T> {
|
||||||
///
|
///
|
||||||
/// let foo: Foo = unsafe {
|
/// let foo: Foo = unsafe {
|
||||||
/// let mut foo = MaybeUninit::<Foo>::uninit();
|
/// 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!
|
/// // (mutable) reference to uninitialized memory!
|
||||||
/// // This is undefined behavior.
|
/// // 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!
|
/// // (mutable) reference to uninitialized memory!
|
||||||
/// // This is undefined behavior.
|
/// // This is undefined behavior.
|
||||||
/// foo.assume_init()
|
/// foo.assume_init_mut()
|
||||||
/// };
|
/// };
|
||||||
/// ```
|
/// ```
|
||||||
// FIXME(#53491): We currently rely on the above being incorrect, i.e., we have references
|
// FIXME(#53491): We currently rely on the above being incorrect, i.e., we have references
|
||||||
|
@ -753,7 +754,7 @@ impl<T> MaybeUninit<T> {
|
||||||
// a final decision about the rules before stabilization.
|
// a final decision about the rules before stabilization.
|
||||||
#[unstable(feature = "maybe_uninit_ref", issue = "63568")]
|
#[unstable(feature = "maybe_uninit_ref", issue = "63568")]
|
||||||
#[inline(always)]
|
#[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.
|
// SAFETY: the caller must guarantee that `self` is initialized.
|
||||||
// This also means that `self` must be a `value` variant.
|
// This also means that `self` must be a `value` variant.
|
||||||
unsafe {
|
unsafe {
|
||||||
|
|
|
@ -55,18 +55,18 @@ where
|
||||||
// to uninitialized data, but within libstd we can rely on more guarantees
|
// to uninitialized data, but within libstd we can rely on more guarantees
|
||||||
// than if this code were in an external lib.
|
// than if this code were in an external lib.
|
||||||
unsafe {
|
unsafe {
|
||||||
reader.initializer().initialize(buf.get_mut());
|
reader.initializer().initialize(buf.assume_init_mut());
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut written = 0;
|
let mut written = 0;
|
||||||
loop {
|
loop {
|
||||||
let len = match reader.read(unsafe { buf.get_mut() }) {
|
let len = match reader.read(unsafe { buf.assume_init_mut() }) {
|
||||||
Ok(0) => return Ok(written),
|
Ok(0) => return Ok(written),
|
||||||
Ok(len) => len,
|
Ok(len) => len,
|
||||||
Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
|
Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
|
||||||
Err(e) => return Err(e),
|
Err(e) => return Err(e),
|
||||||
};
|
};
|
||||||
writer.write_all(unsafe { &buf.get_ref()[..len] })?;
|
writer.write_all(unsafe { &buf.assume_init_ref()[..len] })?;
|
||||||
written += len as u64;
|
written += len as u64;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -376,13 +376,13 @@ impl<T> SyncOnceCell<T> {
|
||||||
/// Safety: The value must be initialized
|
/// Safety: The value must be initialized
|
||||||
unsafe fn get_unchecked(&self) -> &T {
|
unsafe fn get_unchecked(&self) -> &T {
|
||||||
debug_assert!(self.is_initialized());
|
debug_assert!(self.is_initialized());
|
||||||
(&*self.value.get()).get_ref()
|
(&*self.value.get()).assume_init_ref()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Safety: The value must be initialized
|
/// Safety: The value must be initialized
|
||||||
unsafe fn get_unchecked_mut(&mut self) -> &mut T {
|
unsafe fn get_unchecked_mut(&mut self) -> &mut T {
|
||||||
debug_assert!(self.is_initialized());
|
debug_assert!(self.is_initialized());
|
||||||
(&mut *self.value.get()).get_mut()
|
(&mut *self.value.get()).assume_init_mut()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue