Skip to content

Instantly share code, notes, and snippets.

@umurgdk
Created August 4, 2017 23:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save umurgdk/50329332571a2c6c3f7cbf0d595fc35e to your computer and use it in GitHub Desktop.
Save umurgdk/50329332571a2c6c3f7cbf0d595fc35e to your computer and use it in GitHub Desktop.
pub struct TileRefMut<'a, T: Tile + 'a> {
pub tile: &'a mut T,
pub x: usize,
pub y: usize
}
pub struct IterMut<'a, T: Tile + 'a> {
tile_map: &'a mut TileMap<T>,
x: usize,
y: usize
}
impl<'a, T: Tile + 'a> Iterator for IterMut<'a, T> {
type Item = TileRefMut<'a, T>;
fn next(&mut self) -> Option<Self::Item> {
self.tile_map.at_mut(self.x, self.y).map(|t| {
let tile_ref = TileRefMut { tile: t, x: self.x, y: self.y };
self.x += 1;
if self.x >= self.tile_map.width {
self.y += 1;
self.x = 0;
}
tile_ref
})
}
}
@umurgdk
Copy link
Author

umurgdk commented Aug 4, 2017

error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements
   --> src/tilemap.rs:105:23
    |
105 |         self.tile_map.at_mut(self.x, self.y).map(|t| {
    |                       ^^^^^^
    |
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 104:5...
   --> src/tilemap.rs:104:5
    |
104 | /     fn next(&mut self) -> Option<Self::Item> {
105 | |         self.tile_map.at_mut(self.x, self.y).map(|t| {
106 | |             let tile_ref = TileRefMut { tile: t, x: self.x, y: self.y };
107 | |
...   |
116 | |         })
117 | |     }
    | |_____^
note: ...so that reference does not outlive borrowed content
   --> src/tilemap.rs:105:9
    |
105 |         self.tile_map.at_mut(self.x, self.y).map(|t| {
    |         ^^^^^^^^^^^^^
note: but, the lifetime must be valid for the lifetime 'a as defined on the impl at 101:1...
   --> src/tilemap.rs:101:1
    |
101 | / impl<'a, T: Tile + 'a> Iterator for IterMut<'a, T> {
102 | |     type Item = TileRefMut<'a, T>;
103 | |
104 | |     fn next(&mut self) -> Option<Self::Item> {
...   |
117 | |     }
118 | | }
    | |_^
note: ...so that types are compatible (expected std::iter::Iterator, found std::iter::Iterator)
   --> src/tilemap.rs:104:46
    |
104 |       fn next(&mut self) -> Option<Self::Item> {
    |  ______________________________________________^
105 | |         self.tile_map.at_mut(self.x, self.y).map(|t| {
106 | |             let tile_ref = TileRefMut { tile: t, x: self.x, y: self.y };
107 | |
...   |
116 | |         })
117 | |     }
    | |_____^

error: aborting due to previous error

error: Could not compile `hunger`.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment