mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-25 13:24:22 +00:00
Add comment to mem::replace to explain why it's not implemented via mem::swap
This commit is contained in:
parent
ccb160d343
commit
94ecabfc4e
@ -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.
|
||||
|
Loading…
Reference in New Issue
Block a user