Skip to content

Commit f272133

Browse files
committed
core: optimize ptr::replace
1 parent c5b5713 commit f272133

File tree

1 file changed

+2
-3
lines changed

1 file changed

+2
-3
lines changed

library/core/src/ptr/mod.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1114,7 +1114,7 @@ const unsafe fn swap_nonoverlapping_simple_untyped<T>(x: *mut T, y: *mut T, coun
11141114
#[stable(feature = "rust1", since = "1.0.0")]
11151115
#[rustc_const_unstable(feature = "const_replace", issue = "83164")]
11161116
#[rustc_diagnostic_item = "ptr_replace"]
1117-
pub const unsafe fn replace<T>(dst: *mut T, mut src: T) -> T {
1117+
pub const unsafe fn replace<T>(dst: *mut T, src: T) -> T {
11181118
// SAFETY: the caller must guarantee that `dst` is valid to be
11191119
// cast to a mutable reference (valid for writes, aligned, initialized),
11201120
// and cannot overlap `src` since `dst` must point to a distinct
@@ -1128,9 +1128,8 @@ pub const unsafe fn replace<T>(dst: *mut T, mut src: T) -> T {
11281128
align: usize = align_of::<T>(),
11291129
) => is_aligned_and_not_null(addr, align)
11301130
);
1131-
mem::swap(&mut *dst, &mut src); // cannot overlap
1131+
mem::replace(&mut *dst, src)
11321132
}
1133-
src
11341133
}
11351134

11361135
/// Reads the value from `src` without moving it. This leaves the

0 commit comments

Comments
 (0)