Skip to content

Commit

Permalink
Unrolled build for rust-lang#117243
Browse files Browse the repository at this point in the history
Rollup merge of rust-lang#117243 - chfogelman:replace-not-swap-comment, r=thomcc

Explain implementation of mem::replace

This adds a comment to explain why `mem::replace` is not implemented in terms of `mem::swap` to prevent [naïfs like me](rust-lang#117189) from trying to "fix" it.
  • Loading branch information
rust-timer authored Oct 27, 2023
2 parents aa1a71e + 94ecabf commit 629acbf
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions library/core/src/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,10 @@ pub fn take<T: Default>(dest: &mut T) -> T {
#[rustc_const_unstable(feature = "const_replace", issue = "83164")]
#[cfg_attr(not(test), rustc_diagnostic_item = "mem_replace")]
pub const fn replace<T>(dest: &mut T, src: T) -> T {
// It may be tempting to use `swap` to avoid `unsafe` here. Don't!
// The compiler optimizes the implementation below to two `memcpy`s
// while `swap` would require at least three. See PR#83022 for details.

// SAFETY: We read from `dest` but directly write `src` into it afterwards,
// such that the old value is not duplicated. Nothing is dropped and
// nothing here can panic.
Expand Down

0 comments on commit 629acbf

Please sign in to comment.