implement ptr::write without dedicated intrinsic

This commit is contained in:
Ralf Jung 2020-12-22 13:05:31 +01:00
parent bbcaed03bf
commit 1862135351
2 changed files with 11 additions and 12 deletions

View File

@ -768,13 +768,6 @@ extern "rust-intrinsic" {
#[rustc_const_stable(feature = "const_size_of", since = "1.40.0")] #[rustc_const_stable(feature = "const_size_of", since = "1.40.0")]
pub fn size_of<T>() -> usize; pub fn size_of<T>() -> usize;
/// Moves a value to an uninitialized memory location.
///
/// Drop glue is not run on the destination.
///
/// The stabilized version of this intrinsic is [`core::ptr::write`](crate::ptr::write).
pub fn move_val_init<T>(dst: *mut T, src: T);
/// The minimum alignment of a type. /// The minimum alignment of a type.
/// ///
/// The stabilized version of this intrinsic is [`core::mem::align_of`](crate::mem::align_of). /// The stabilized version of this intrinsic is [`core::mem::align_of`](crate::mem::align_of).

View File

@ -883,12 +883,18 @@ pub const unsafe fn read_unaligned<T>(src: *const T) -> T {
#[inline] #[inline]
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub unsafe fn write<T>(dst: *mut T, src: T) { pub unsafe fn write<T>(dst: *mut T, src: T) {
if cfg!(debug_assertions) && !is_aligned_and_not_null(dst) { // We are calling the intrinsics directly to avoid function calls in the generated code.
// Not panicking to keep codegen impact smaller. extern "rust-intrinsic" {
abort(); fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize);
}
// SAFETY: the caller must guarantee that `dst` is valid for writes.
// `dst` cannot overlap `src` because the caller has mutable access
// to `dst` while `src` is owned by this function.
unsafe {
copy_nonoverlapping(&src as *const T, dst, 1);
intrinsics::forget(src);
} }
// SAFETY: the caller must uphold the safety contract for `move_val_init`.
unsafe { intrinsics::move_val_init(&mut *dst, src) }
} }
/// Overwrites a memory location with the given value without reading or /// Overwrites a memory location with the given value without reading or