I want to create an Iterator capable of exposing neighbouring items as well. As long as I don't want to also change these items, it is fine and easy. But how to make a mutable variant of the same structure?
Immutable:
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], } ) } } Mutable variant that doesn't work due to lifetimes:
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], } ) } } Compiler points that:
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], | ^^^^^^^^^^^^^^^ |