rust - 从Iterator返回的对象中的可变引用

标签 rust

我想创建一个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],
    |                           ^^^^^^^^^^^^^^^
    |

最佳答案

不幸的是,无法正确制作EnumerateNearestNeighbors2DMut -这是不正确的。每次调用next时,您都会获得&mut引用,这些引用可能与上次对&mut的调用返回的next引用重叠。这意味着,如果可行,将通过创建别名&mut来违反引用规则。
这与存在 std::slice::Windows 而不是WindowsMut的原因相同(尽管ChunksMut很好,因为块不重叠)。
某些问题(here's one example)给出了类似的错误消息,但实际上可以解决(在某些情况下,使用unsafe),因为所引用的项实际上是不重叠的。这些解决方案在这里不起作用。如果可以使用write an iterator that returns references to itself(“流式迭代器”),则可以使该API正常运行。但是,Iterator不允许这样做。
这是您的三个可能的选择。当然还有其他。

  • 根本不允许可变迭代(与邻居进行迭代)。只需通过共享(&)引用公开迭代,如果您需要更改原始网格,则可以创建修改后的副本,并在完成迭代后将其与原始网格交换。无论如何,这通常是您想要的,如果您正在编写诸如图像滤镜或元胞自动机之类的东西,其中每个输出都取决于多个输入。
  • 接受闭包,并在API中使用内部迭代,而不是外部迭代。因此,而不是像这样的事情:
    for neighbors in array.enumerate_2d_neighbors_mut() {
        println!("{}", neighbors.top);
    }
    
    您将编写如下内容:
    array.foreach_2d_neighbors_mut(|neighbors| {
        println!("{}", neighbors.top);
    });
    
    在这种情况下,对array项的引用是在for方法内部的foreach_2d_neighbors_mut循环中获取的,并且不会对其进行转义。即使没有unsafe代码,也可以很容易地编写此API。
  • 使用内部可变性(CellRefCellAtomic???等)通过&引用进行突变,而不需要&mut。根据您正在执行的操作,这可能是正确的方法。请注意,您可以sneakily use Cell for interior mutability without having to change the type I , when I is a slice or vector。但是,在大多数情况下,这并不是我的首选。
  • 关于rust - 从Iterator返回的对象中的可变引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63552367/

    相关文章:

    rust - 这是 !Sync 类型声音的包装器吗?

    generics - 为什么在关联类型上不识别除第一个之外的 super 特征边界?

    arrays - 所有排列直到 n = x

    optional-parameters - 如何在 Rust 中使用参数重载或可选参数?

    rust - 将枚举转换为 BTreeMap?

    rust - 使用泛型时, `From` 的实现如何冲突?

    rust - 我如何应对惰性迭代器?

    macros - 如何从常量值和值集合生成 `quote::Tokens`?

    multithreading - 如何将 "unlock"设为 RwLock?

    c++ - 将调用 C++ 的 Rust 编译为 WASM