From ba78690ba462f2eb9956f865d08a7da64f617b5e Mon Sep 17 00:00:00 2001 From: Johannes Date: Tue, 10 Dec 2024 19:06:07 +0100 Subject: [PATCH] fixed grid boundary checks --- src/utils/grid.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/utils/grid.rs b/src/utils/grid.rs index a3e8b81..3194613 100644 --- a/src/utils/grid.rs +++ b/src/utils/grid.rs @@ -36,11 +36,17 @@ impl Grid { } fn index_of(&self, c: Coord) -> usize { + if !self.contains_key(c) { + panic!( + "point {c:?} out of range (width: {}, height: {})", + self.content_width, self.content_height + ); + } (c.1 * self.content_width + c.0) as usize } pub fn get(&self, c: Coord) -> Option<&T> { - if c.0 < 0 || c.0 >= self.content_width || c.1 < 0 || c.1 >= self.content_height { + if !self.contains_key(c) { return None; } let index = self.index_of(c); @@ -52,6 +58,9 @@ impl Grid { } pub fn get_mut(&mut self, c: Coord) -> Option<&mut T> { + if !self.contains_key(c) { + return None; + } let index = self.index_of(c); if index < self.content.len() { Some(&mut self.content[index]) @@ -78,6 +87,8 @@ impl Grid { }) // .collect_vec() } + + // todo: map from T to U } impl Grid {