Skip to content

Commit 9ac6277

Browse files
committed
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.
1 parent 37b55c8 commit 9ac6277

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
@@ -964,6 +964,28 @@ pub const fn replace<T>(dest: &mut T, src: T) -> T {
964964
#[cfg_attr(not(test), rustc_diagnostic_item = "mem_drop")]
965965
pub fn drop<T>(_x: T) {}
966966

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

0 commit comments

Comments
 (0)