From f2721338f624657da58e21a1e2cf827b3e54301a Mon Sep 17 00:00:00 2001 From: joboet Date: Sat, 16 Mar 2024 17:51:00 +0100 Subject: [PATCH] core: optimize `ptr::replace` --- library/core/src/ptr/mod.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/library/core/src/ptr/mod.rs b/library/core/src/ptr/mod.rs index 30af752236846..1f0204daf7238 100644 --- a/library/core/src/ptr/mod.rs +++ b/library/core/src/ptr/mod.rs @@ -1114,7 +1114,7 @@ const unsafe fn swap_nonoverlapping_simple_untyped(x: *mut T, y: *mut T, coun #[stable(feature = "rust1", since = "1.0.0")] #[rustc_const_unstable(feature = "const_replace", issue = "83164")] #[rustc_diagnostic_item = "ptr_replace"] -pub const unsafe fn replace(dst: *mut T, mut src: T) -> T { +pub const unsafe fn replace(dst: *mut T, src: T) -> T { // SAFETY: the caller must guarantee that `dst` is valid to be // cast to a mutable reference (valid for writes, aligned, initialized), // and cannot overlap `src` since `dst` must point to a distinct @@ -1128,9 +1128,8 @@ pub const unsafe fn replace(dst: *mut T, mut src: T) -> T { align: usize = align_of::(), ) => is_aligned_and_not_null(addr, align) ); - mem::swap(&mut *dst, &mut src); // cannot overlap + mem::replace(&mut *dst, src) } - src } /// Reads the value from `src` without moving it. This leaves the