Skip to content

Commit

Permalink
Rollup merge of #95534 - jyn514:std-mem-copy, r=joshtriplett
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JohnTitor authored Jun 19, 2022
2 parents bb8c2f4 + 9ac6277 commit 9d4e08e
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions library/core/src/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down

0 comments on commit 9d4e08e

Please sign in to comment.