Skip to content

Commit 1862135

Browse files
committed
implement ptr::write without dedicated intrinsic
1 parent bbcaed0 commit 1862135

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

Diff for: library/core/src/intrinsics.rs

-7
Original file line numberDiff line numberDiff line change
@@ -768,13 +768,6 @@ extern "rust-intrinsic" {
768768
#[rustc_const_stable(feature = "const_size_of", since = "1.40.0")]
769769
pub fn size_of<T>() -> usize;
770770

771-
/// Moves a value to an uninitialized memory location.
772-
///
773-
/// Drop glue is not run on the destination.
774-
///
775-
/// The stabilized version of this intrinsic is [`core::ptr::write`](crate::ptr::write).
776-
pub fn move_val_init<T>(dst: *mut T, src: T);
777-
778771
/// The minimum alignment of a type.
779772
///
780773
/// The stabilized version of this intrinsic is [`core::mem::align_of`](crate::mem::align_of).

Diff for: library/core/src/ptr/mod.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -883,12 +883,18 @@ pub const unsafe fn read_unaligned<T>(src: *const T) -> T {
883883
#[inline]
884884
#[stable(feature = "rust1", since = "1.0.0")]
885885
pub unsafe fn write<T>(dst: *mut T, src: T) {
886-
if cfg!(debug_assertions) && !is_aligned_and_not_null(dst) {
887-
// Not panicking to keep codegen impact smaller.
888-
abort();
886+
// We are calling the intrinsics directly to avoid function calls in the generated code.
887+
extern "rust-intrinsic" {
888+
fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize);
889+
}
890+
891+
// SAFETY: the caller must guarantee that `dst` is valid for writes.
892+
// `dst` cannot overlap `src` because the caller has mutable access
893+
// to `dst` while `src` is owned by this function.
894+
unsafe {
895+
copy_nonoverlapping(&src as *const T, dst, 1);
896+
intrinsics::forget(src);
889897
}
890-
// SAFETY: the caller must uphold the safety contract for `move_val_init`.
891-
unsafe { intrinsics::move_val_init(&mut *dst, src) }
892898
}
893899

894900
/// Overwrites a memory location with the given value without reading or

0 commit comments

Comments
 (0)