`std :: mem :: swap`如何工作?

时间:2015-07-03 19:39:58

标签: memory rust swap

  

在相同类型的两个可变位置交换值,而不取消初始化或复制任何一个。

use std::mem;

let x = &mut 5;
let y = &mut 42;

mem::swap(x, y);

assert_eq!(42, *x);
assert_eq!(5, *y);

(来自offical Rust doc

如何在不复制的情况下交换两个值?价值42是如何从y转到x的?这不可能。

2 个答案:

答案 0 :(得分:6)

该函数实际上在内部进行复制:这是从文档中提取的源代码:

pub fn swap<T>(x: &mut T, y: &mut T) {
    unsafe {
        // Give ourselves some scratch space to work with
        let mut t: T = uninitialized();

        // Perform the swap, `&mut` pointers never alias
        ptr::copy_nonoverlapping(&*x, &mut t, 1);
        ptr::copy_nonoverlapping(&*y, x, 1);
        ptr::copy_nonoverlapping(&t, y, 1);

        // y and t now point to the same thing,
        // but we need to completely forget `t`
        // because it's no longer relevant.
        forget(t);
    }
}

答案 1 :(得分:3)

previous answer在语义上是正确的,但在详细信息上已过时。

逻辑上,交换两个值的方法是将值A读到临时位置,将B复制到A的顶部,然后将临时值写回到B。在很短的时间内,相同的值在内存中存在两次。这就是为什么这些功能的实现需要unsafe代码的原因,因为只有人才能保证Rust的安全要求得到遵守。

从Rust 1.43.0开始,mem::swap is implemented as

pub fn swap<T>(x: &mut T, y: &mut T) {
    // SAFETY: the raw pointers have been created from safe mutable references satisfying all the
    // constraints on `ptr::swap_nonoverlapping_one`
    unsafe {
        ptr::swap_nonoverlapping_one(x, y);
    }
}

swap_nonoverlapping_one是私有的,但是its implementation是:

pub(crate) unsafe fn swap_nonoverlapping_one<T>(x: *mut T, y: *mut T) {
    // For types smaller than the block optimization below,
    // just swap directly to avoid pessimizing codegen.
    if mem::size_of::<T>() < 32 {
        let z = read(x);
        copy_nonoverlapping(y, x, 1);
        write(y, z);
    } else {
        swap_nonoverlapping(x, y, 1);
    }
}

您可以查看ptr::copy_nonoverlappingptr::swap_nonoverlapping的文档。后者基本上是针对较大值进行复制的高度优化版本。