Skip to content

Commit 9d4e08e

Browse files
authored
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.
2 parents bb8c2f4 + 9ac6277 commit 9d4e08e

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

library/core/src/mem/mod.rs

+22
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,28 @@ pub const fn replace<T>(dest: &mut T, src: T) -> T {
973973
#[cfg_attr(not(test), rustc_diagnostic_item = "mem_drop")]
974974
pub fn drop<T>(_x: T) {}
975975

976+
/// Bitwise-copies a value.
977+
///
978+
/// This function is not magic; it is literally defined as
979+
/// ```
980+
/// pub fn copy<T: Copy>(x: &T) -> T { *x }
981+
/// ```
982+
///
983+
/// It is useful when you want to pass a function pointer to a combinator, rather than defining a new closure.
984+
///
985+
/// Example:
986+
/// ```
987+
/// #![feature(mem_copy_fn)]
988+
/// use core::mem::copy;
989+
/// let result_from_ffi_function: Result<(), &i32> = Err(&1);
990+
/// let result_copied: Result<(), i32> = result_from_ffi_function.map_err(copy);
991+
/// ```
992+
#[inline]
993+
#[unstable(feature = "mem_copy_fn", issue = "98262")]
994+
pub fn copy<T: Copy>(x: &T) -> T {
995+
*x
996+
}
997+
976998
/// Interprets `src` as having type `&U`, and then reads `src` without moving
977999
/// the contained value.
9781000
///

0 commit comments

Comments
 (0)