我想创建一个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
不允许这样做。
这是您的三个可能的选择。当然还有其他。
&
)引用公开迭代,如果您需要更改原始网格,则可以创建修改后的副本,并在完成迭代后将其与原始网格交换。无论如何,这通常是您想要的,如果您正在编写诸如图像滤镜或元胞自动机之类的东西,其中每个输出都取决于多个输入。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。Cell
,RefCell
,Atomic???
等)通过&
引用进行突变,而不需要&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/