【问题标题】:Mutable reference in object returned from Iterator从迭代器返回的对象中的可变引用
【发布时间】:2020-08-23 21:54:39
【问题描述】:

我想创建一个Iterator 也能够暴露相邻的项目。只要我不想也更改这些项目,这很好,很容易。但是如何制作相同结构的可变变体呢?

不可变:

struct NearestNeighbours2D<'a, T> {
    mid: &'a T,
    left: &'a T,
    right: &'a T,
    top: &'a T,
    bot: &'a T,
}

struct EnumerateNearestNeighbours2D<'a, I>
where I: std::ops::Index<usize> {
    x: usize,
    y: usize,
    width: usize,
    height: usize,
    inner: &'a I
}

impl<'a, I: std::ops::Index<usize>> Iterator for EnumerateNearestNeighbours2D<'a, I>
where <I as std::ops::Index<usize>>::Output: std::marker::Sized {
    
    type Item = NearestNeighbours2D<'a, I::Output>;
    fn next(&mut self) -> std::option::Option<<Self as std::iter::Iterator>::Item> {

        let (top, left, mid, right, bot) = (
            (self.y - 1) * self.width + self.x,
            self.y * self.width + self.x - 1,
            self.y * self.width + self.x,
            self.y * self.width + self.x + 1,
            (self.y + 1) * self.width + self.x,
        );

        Some(
            NearestNeighbours2D {
                mid: &self.inner[mid],
                left: &self.inner[left],
                right: &self.inner[right],
                top: &self.inner[top],
                bot: &self.inner[bot],
            }
        )
    }
}

由于生命周期而无法工作的可变变体:

struct NearestNeighbours2DMut<'a, T> {
    mid: &'a mut T,
    left: &'a mut T,
    right: &'a mut T,
    top: &'a mut T,
    bot: &'a mut T,
}

struct EnumerateNearestNeighbours2DMut<'a, I>
where I: std::ops::IndexMut<usize> {
    x: usize,
    y: usize,
    width: usize,
    height: usize,
    inner: &'a mut I
}

impl<'a, I: std::ops::IndexMut<usize>> Iterator for EnumerateNearestNeighbours2DMut<'a, I>
where <I as std::ops::Index<usize>>::Output: std::marker::Sized {
    
    type Item = NearestNeighbours2DMut<'a, I::Output>;
    fn next(&mut self) -> std::option::Option<<Self as std::iter::Iterator>::Item> {

        let (top, left, mid, right, bot) = (
            (self.y - 1) * self.width + self.x,
            self.y * self.width + self.x - 1,
            self.y * self.width + self.x,
            self.y * self.width + self.x + 1,
            (self.y + 1) * self.width + self.x,
        );

        Some(
            NearestNeighbours2DMut {
                mid: &mut self.inner[mid],
                left: &mut self.inner[left],
                right: &mut self.inner[right],
                top: &mut self.inner[top],
                bot: &mut self.inner[bot],
            }
        )
    }
}

编译器指出:

error[E0495]: cannot infer an appropriate lifetime for lifetime parameter in function call due to conflicting requirements
   --> src\lib.rs:99:27
    |
99  |                 mid: &mut self.inner[mid],
    |                           ^^^^^^^^^^^^^^^
    |

【问题讨论】:

  • @trentcl 你也可以看看那个吗?
  • 仅供参考,除非他们是被评论帖子的作者或之前曾评论过同一帖子,否则人们不会收到在 cmets 中被 @ed 的通知。不过我碰巧在这里,所以我会看看:-)

标签: rust


【解决方案1】:

不幸的是,EnumerateNearestNeighbors2DMut 无法正确生成——这是不合理的。每次调用next 时,您都会得到&amp;mut 引用,这些引用可能与先前调用next 返回的&amp;mut 引用重叠。这意味着,如果它有效,它将通过创建别名 &amp;muts 来违反引用规则。

这与存在std::slice::Windows,但没有WindowsMut 的原因相同(尽管ChunksMut 很好,因为块不重叠)。

有些问题 (here's one example) 会给出看起来相似的错误消息,但实际上是可以解决的(在某些情况下使用 unsafe),因为被引用的项目实际上是不重叠的。这些解决方案在这里不起作用。如果您可以write an iterator that returns references to itself(“流式迭代器”),API 就可以正常运行。但是,Iterator 不允许这样做。

这里有三个可能的选项供您选择。当然还有其他人。

  • 根本不允许可变迭代(与邻居)。只需通过共享 (&amp;) 引用公开迭代,如果您需要改变原始网格,请创建一个修改后的副本并在完成迭代后将其与原始网格交换。无论如何,这通常是您想要的,如果您正在编写图像过滤器或元胞自动机之类的东西,其中每个输出取决于多个输入。

  • 接受闭包并在您的 API 中使用内部迭代而不是外部迭代。所以不要像这样:

    for neighbors in array.enumerate_2d_neighbors_mut() {
        println!("{}", neighbors.top);
    }
    

    你会这样写:

    array.foreach_2d_neighbors_mut(|neighbors| {
        println!("{}", neighbors.top);
    });
    

    在这种情况下,对array 项目的引用在foreach_2d_neighbors_mut 方法内的for 循环中获取,并且不会对其进行转义。即使没有 unsafe 代码,这个 API 也可以很容易地编写。

  • 使用内部可变性(CellRefCellAtomic??? 等)通过 &amp; 引用而不是需要 &amp;mut 进行变异。取决于你在做什么,这可能是正确的方法。请注意,您可以sneakily use Cell for interior mutability without having to change the type I, when I is a slice or vector。但是,大多数时候这不会是我的首选。

【讨论】:

  • 我用谷歌搜索了Cell,它指出的第一件事正是我犯的错误:“[...] 只能有以下之一:有几个不可变的引用 (&T)对象(也称为别名)。对对象有一个可变引用(&mut T)(也称为可变性)。”。我只需要它来读取“混合”相邻元素并保存在不同的缓冲区中,但我想知道“如果”以防我将来需要它:D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-01-12
  • 1970-01-01
  • 1970-01-01
  • 2014-07-30
  • 1970-01-01
  • 2011-07-23
  • 1970-01-01
相关资源
最近更新 更多