about summary refs log tree commit diff
path: root/library/core/src/ptr/mod.rs
diff options
context:
space:
mode:
authorRalf Jung <post@ralfj.de>2020-12-22 13:05:31 +0100
committerRalf Jung <post@ralfj.de>2020-12-30 18:39:05 +0100
commit1862135351e87905e7a497bee96f199040ce1b51 (patch)
tree2659bb878af8ce4e063025a93c256bf22f282594 /library/core/src/ptr/mod.rs
parentbbcaed03bf5505f3fed351887769ed1531599502 (diff)
downloadrust-1862135351e87905e7a497bee96f199040ce1b51.tar.gz
rust-1862135351e87905e7a497bee96f199040ce1b51.zip
implement ptr::write without dedicated intrinsic
Diffstat (limited to 'library/core/src/ptr/mod.rs')
-rw-r--r--library/core/src/ptr/mod.rs16
1 files changed, 11 insertions, 5 deletions
diff --git a/library/core/src/ptr/mod.rs b/library/core/src/ptr/mod.rs
index 807f114ea46..8fe10099bb9 100644
--- a/library/core/src/ptr/mod.rs
+++ b/library/core/src/ptr/mod.rs
@@ -883,12 +883,18 @@ pub const unsafe fn read_unaligned<T>(src: *const T) -> T {
 #[inline]
 #[stable(feature = "rust1", since = "1.0.0")]
 pub unsafe fn write<T>(dst: *mut T, src: T) {
-    if cfg!(debug_assertions) && !is_aligned_and_not_null(dst) {
-        // Not panicking to keep codegen impact smaller.
-        abort();
+    // We are calling the intrinsics directly to avoid function calls in the generated code.
+    extern "rust-intrinsic" {
+        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