about summary refs log tree commit diff
diff options
context:
space:
mode:
authorYuki Okushi <jtitor@2k36.org>2022-06-20 07:37:40 +0900
committerGitHub <noreply@github.com>2022-06-20 07:37:40 +0900
commit9d4e08e725592a703b9846a8421ff5eed0aff3c3 (patch)
treef9f9a4d82b679572c5d7480068d65c5092117370
parentbb8c2f41174caceec00c28bc6c5c20ae9f9a175c (diff)
parent9ac6277bad856e0a1373e1fd26ee3e70b10074a8 (diff)
downloadrust-9d4e08e725592a703b9846a8421ff5eed0aff3c3.tar.gz
rust-9d4e08e725592a703b9846a8421ff5eed0aff3c3.zip
Rollup merge of #95534 - jyn514:std-mem-copy, r=joshtriplett
Add `core::mem::copy` to complement `core::mem::drop`.

This is useful for combinators. I didn't add `clone` since you can already
use `Clone::clone` in its place; copy has no such corresponding function.
-rw-r--r--library/core/src/mem/mod.rs22
1 files changed, 22 insertions, 0 deletions
diff --git a/library/core/src/mem/mod.rs b/library/core/src/mem/mod.rs
index 71ea3b4ba85..ecd2b75ae44 100644
--- a/library/core/src/mem/mod.rs
+++ b/library/core/src/mem/mod.rs
@@ -973,6 +973,28 @@ pub const fn replace<T>(dest: &mut T, src: T) -> T {
 #[cfg_attr(not(test), rustc_diagnostic_item = "mem_drop")]
 pub fn drop<T>(_x: T) {}
 
+/// Bitwise-copies a value.
+///
+/// This function is not magic; it is literally defined as
+/// ```
+/// pub fn copy<T: Copy>(x: &T) -> T { *x }
+/// ```
+///
+/// It is useful when you want to pass a function pointer to a combinator, rather than defining a new closure.
+///
+/// Example:
+/// ```
+/// #![feature(mem_copy_fn)]
+/// use core::mem::copy;
+/// let result_from_ffi_function: Result<(), &i32> = Err(&1);
+/// let result_copied: Result<(), i32> = result_from_ffi_function.map_err(copy);
+/// ```
+#[inline]
+#[unstable(feature = "mem_copy_fn", issue = "98262")]
+pub fn copy<T: Copy>(x: &T) -> T {
+    *x
+}
+
 /// Interprets `src` as having type `&U`, and then reads `src` without moving
 /// the contained value.
 ///