about summary refs log tree commit diff
diff options
context:
space:
mode:
authorCarter Hunt Fogelman <chfogelman@gmail.com>2023-10-26 11:10:04 -0700
committerCarter Hunt Fogelman <chfogelman@gmail.com>2023-10-26 11:10:04 -0700
commit94ecabfc4eea1c58dfd412b00e4a518f0dc87431 (patch)
tree078ef72c8e8911b6d5ebfb3ffc5a02a5d666e52a
parentccb160d3432dff025ab62696a82e576bf08aa006 (diff)
downloadrust-94ecabfc4eea1c58dfd412b00e4a518f0dc87431.tar.gz
rust-94ecabfc4eea1c58dfd412b00e4a518f0dc87431.zip
Add comment to mem::replace to explain why it's not implemented via mem::swap
-rw-r--r--library/core/src/mem/mod.rs4
1 files changed, 4 insertions, 0 deletions
diff --git a/library/core/src/mem/mod.rs b/library/core/src/mem/mod.rs
index a79a204e2c6..7ef84b0f5b5 100644
--- a/library/core/src/mem/mod.rs
+++ b/library/core/src/mem/mod.rs
@@ -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.