别名规则和使Rust函数“generic over aliasing”

tuwxkamq  于 9个月前  发布在  其他
关注(0)|答案(3)|浏览(107)

我有一个例程,它接受一个不可变的点切片,并执行一些转换,写入一个可变的点切片。(作为一个简单的例子,让我们假设我们用某个常数转换所有的点。)假设实现不能简化为逐点处理。(在我的例子中,我在确认编译器不能自动完成之后,对算法进行了向量化。)
在尽量减少重复代码的同时,我希望例程能够同时处理1.将一个数组转换为另一个数组,以及2.就地转换单个可变数组。
假设现有的实现具有以下形式

fn transform(in_: &[Point], out: &mut [Point], translate: Point) {
   // ...
}

字符串
这解决了case(1),但是因为我们不能同时以可变和不变的方式借用同一个数组,所以这对case(2)不起作用。实际上,尝试使用unsafe,并同时获得可变和不可变的引用,是即时未定义的行为。
我们怎么才能绕过这个问题呢?我们能通过做一些像这样的事情来绕过规则吗?

unsafe fn transform_impl(in_: &[UnsafeCell<Point>], out: &[UnsafeCell<Point>], translate: Point) {
  // ... promise we won't modify in_ here, nor have overlapping lifetimes
  // between values read from in_ and written to out ...
}

fn transform_in_place(inout: &mut [Point], translate: Point) {
  unsafe {
    let a: &[UnsafeCell<Point>] = std::mem::transmute(inout);
    transform_impl(a, a, translate);
  }
}

fn transform(in_: &[Point], out: &mut [FPoint], translate: Point) {
  unsafe {
    transform_impl(std::mem::transmute(in_), std::mem::transmute(out), translate);
  }
}


使用内部可变性,并保证我们不会从in_中的值的“下面”更改out中的值?或者像以前一样,将不可变切片转换为UnsafeCell切片,即时未定义行为?
(In C中,我们可以只使用指针,因为相同类型的非restrict指针可能会别名。
我更倾向于用一个函数来处理这两种情况,如果只是为了最小化重复生成的代码量。如果这最终是不切实际的,什么是一个优雅的方法来使它工作而不需要在源代码中实现算法两次?

pb3s4cty

pb3s4cty1#

考虑使用traits来处理这种情况。
你可以创建一个trait来定义“点I/O”,并使用方法来获取不可变和可变切片。然后你可以为&mut [Point](就地情况)和(&[Point], &mut [Point])(非重叠情况)实现它。
trait不需要是公共的,因为你可以提供不同的公共函数来处理每种情况,但是委托给一个公共的实现函数。
这就要求实现的编写方式不需要读操作与写操作重叠。

use std::slice::SliceIndex;

struct Point;

trait PointIO {
    fn len(&self) -> usize;

    fn get<I: SliceIndex<[Point]>>(&self, range: I) -> &I::Output;
    fn get_mut<I: SliceIndex<[Point]>>(&mut self, range: I) -> &mut I::Output;
}

impl PointIO for &mut [Point] {
    fn len(&self) -> usize {
        <[Point]>::len(self)
    }

    fn get<I: SliceIndex<[Point]>>(&self, range: I) -> &I::Output {
        &self[range]
    }

    fn get_mut<I: SliceIndex<[Point]>>(&mut self, range: I) -> &mut I::Output {
        &mut self[range]
    }
}

impl PointIO for (&[Point], &mut [Point]) {
    fn len(&self) -> usize {
        std::cmp::min(self.0.len(), self.1.len())
    }

    fn get<I: SliceIndex<[Point]>>(&self, range: I) -> &I::Output {
        &self.0[range]
    }

    fn get_mut<I: SliceIndex<[Point]>>(&mut self, range: I) -> &mut I::Output {
        &mut self.1[range]
    }
}

fn transform_impl(points: impl PointIO, translate: Point) {
    todo!()
}

fn transform(in_: &[Point], out: &mut [Point], translate: Point) {
    transform_impl((in_, out), translate);
}

fn transform_overlapping(inout: &mut [Point], translate: Point) {
    transform_impl(inout, translate);
}

字符串

jyztefdp

jyztefdp2#

&mut T转换为&UnsafeCell<T>(因此&mut [Point]&[UnsafeCell<Point>]绝对没问题。这是有文档记录的。你必须小心不要同时持有可变和共享引用,虽然,这意味着限制从in_访问&或从out访问&mut,但是不能同时对同一个索引执行这两个操作。甚至可以安全地使用Cell,使用Cell::from_mut()Cell::as_slice_of_cells()(取决于您需要如何访问Point)。
问题是另一个类型转换,&T&UnsafeCell<T>。如果引用然后被写入,这无疑是UB。问题是当我们只创建引用而不写入时会发生什么。
Stacked Borrows标记了这个UB。Tree Borrows(另一个可能的别名模型)接受这个。我的理解是人们希望允许这个,但是不清楚如何允许Stacked Borrows。
考虑到别名模型还没有决定,我建议避免这种模式。如果你使用原始点而不是引用,你可以做你想做的事情;尽管这意味着更不安全的代码。

e0bqpujr

e0bqpujr3#

作为替代方案,我建议不依赖transmute,而是定义transform_inplace()的所有细节,并为不需要就地进行转换的情况提供简单的适配器。

#[derive(Debug, Clone, Copy)]
struct Point {
    x: f64,
    y: f64,
}

fn transform_inplace(
    inout: &mut [Point],
    translate: Point,
) {
    for p in inout.iter_mut() {
        p.x += translate.x;
        p.y += translate.y;
    }
}

fn transform(
    input: &[Point],
    output: &mut [Point],
    translate: Point,
) {
    output.clone_from_slice(input);
    transform_inplace(output, translate);
}

fn transformed(
    input: &[Point],
    translate: Point,
) -> Vec<Point> {
    let mut output = input.to_owned();
    transform_inplace(&mut output, translate);
    output
}

fn main() {
    {
        let mut points = [
            Point { x: 1.1, y: 2.2 },
            Point { x: 3.3, y: 4.4 },
            Point { x: 5.5, y: 6.6 },
        ];
        transform_inplace(&mut points, Point { x: 10.0, y: 20.0 });
        println!("transform_inplace(): {:?}", points);
    }
    {
        let input = [
            Point { x: 1.1, y: 2.2 },
            Point { x: 3.3, y: 4.4 },
            Point { x: 5.5, y: 6.6 },
        ];
        let mut output = [Point { x: 0.0, y: 0.0 }; 3];
        transform(&input, &mut output, Point { x: 10.0, y: 20.0 });
        println!("transform(): {:?}", output);
    }
    {
        let input = [
            Point { x: 1.1, y: 2.2 },
            Point { x: 3.3, y: 4.4 },
            Point { x: 5.5, y: 6.6 },
        ];
        let output = transformed(&input, Point { x: 10.0, y: 20.0 });
        println!("transformed(): {:?}", output);
    }
}
/*
transform_inplace(): [Point { x: 11.1, y: 22.2 }, Point { x: 13.3, y: 24.4 }, Point { x: 15.5, y: 26.6 }]
transform(): [Point { x: 11.1, y: 22.2 }, Point { x: 13.3, y: 24.4 }, Point { x: 15.5, y: 26.6 }]
transformed(): [Point { x: 11.1, y: 22.2 }, Point { x: 13.3, y: 24.4 }, Point { x: 15.5, y: 26.6 }]
*/

字符串

相关问题