Skip to content

Commit bc0788b

Browse files
author
Lukas Markeffsky
committed
Use mem::replace in ptr::replace instead of mem::swap
1 parent ecdd374 commit bc0788b

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
@@ -1012,16 +1012,15 @@ const unsafe fn swap_nonoverlapping_simple<T>(x: *mut T, y: *mut T, count: usize
10121012
#[inline]
10131013
#[stable(feature = "rust1", since = "1.0.0")]
10141014
#[rustc_const_unstable(feature = "const_replace", issue = "83164")]
1015-
pub const unsafe fn replace<T>(dst: *mut T, mut src: T) -> T {
1015+
pub const unsafe fn replace<T>(dst: *mut T, src: T) -> T {
10161016
// SAFETY: the caller must guarantee that `dst` is valid to be
10171017
// cast to a mutable reference (valid for writes, aligned, initialized),
10181018
// and cannot overlap `src` since `dst` must point to a distinct
10191019
// allocated object.
10201020
unsafe {
10211021
assert_unsafe_precondition!(is_aligned_and_not_null(dst));
1022-
mem::swap(&mut *dst, &mut src); // cannot overlap
1022+
mem::replace(&mut *dst, src)
10231023
}
1024-
src
10251024
}
10261025

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

0 commit comments

Comments
 (0)