diff --git a/crates/servicepoint/src/byte_grid.rs b/crates/servicepoint/src/byte_grid.rs index 163237a..d6d5759 100644 --- a/crates/servicepoint/src/byte_grid.rs +++ b/crates/servicepoint/src/byte_grid.rs @@ -43,10 +43,6 @@ impl ByteGrid { } } - fn is_position_valid(&self, x: isize, y: isize) -> bool { - x > 0 && x < self.width as isize && y > 0 && y < self.height as isize - } - fn check_indexes(&self, x: usize, y: usize) { assert!( x < self.width, @@ -96,22 +92,6 @@ impl Grid for ByteGrid { self.data[x + y * self.width] } - fn get_optional(&self, x: isize, y: isize) -> Option { - if self.is_position_valid(x, y) { - Some(self.get(x as usize, y as usize)) - } else { - None - } - } - - fn set_optional(&mut self, x: isize, y: isize, value: u8) -> Option { - if self.is_position_valid(x, y) { - Some(self.set(x as usize, y as usize, value)) - } else { - None - } - } - fn fill(&mut self, value: u8) { self.data.fill(value); } @@ -151,7 +131,7 @@ impl RefGrid for ByteGrid { } fn get_ref_optional(&self, x: isize, y: isize) -> Option<&u8> { - if self.is_position_valid(x, y) { + if self.is_in_bounds(x, y) { Some(&self.data[x as usize + y as usize * self.width]) } else { None @@ -164,7 +144,7 @@ impl RefGrid for ByteGrid { } fn get_ref_mut_optional(&mut self, x: isize, y: isize) -> Option<&mut u8> { - if self.is_position_valid(x, y) { + if self.is_in_bounds(x, y) { Some(&mut self.data[x as usize + y as usize * self.width]) } else { None diff --git a/crates/servicepoint/src/grid.rs b/crates/servicepoint/src/grid.rs index 79cdbfb..76c80db 100644 --- a/crates/servicepoint/src/grid.rs +++ b/crates/servicepoint/src/grid.rs @@ -31,7 +31,13 @@ pub trait Grid { /// * `x` and `y`: position of the cell to read /// /// returns: Value at position or None - fn get_optional(&self, x: isize, y: isize) -> Option; + fn get_optional(&self, x: isize, y: isize) -> Option { + if self.is_in_bounds(x, y) { + Some(self.get(x as usize, y as usize)) + } else { + None + } + } /// Sets the value at the specified position if the position is inside of bounds /// @@ -40,7 +46,13 @@ pub trait Grid { /// * `x` and `y`: position of the cell to read /// /// returns: the old value or None - fn set_optional(&mut self, x: isize, y: isize, value: T) -> Option; + fn set_optional(&mut self, x: isize, y: isize, value: T) -> Option { + if self.is_in_bounds(x, y) { + Some(self.set(x as usize, y as usize, value)) + } else { + None + } + } /// Sets all cells in the grid to the specified value fn fill(&mut self, value: T); @@ -50,6 +62,14 @@ pub trait Grid { /// the height in y-direction fn height(&self) -> usize; + + /// Checks whether the specified position is + fn is_in_bounds(&self, x: isize, y: isize) -> bool { + x > 0 + && x < self.width() as isize + && y > 0 + && y < self.height() as isize + } } /// A grid that can return cells as references. diff --git a/crates/servicepoint/src/pixel_grid.rs b/crates/servicepoint/src/pixel_grid.rs index e6a3840..c6d02dd 100644 --- a/crates/servicepoint/src/pixel_grid.rs +++ b/crates/servicepoint/src/pixel_grid.rs @@ -60,10 +60,6 @@ impl PixelGrid { } } - fn is_position_valid(&self, x: isize, y: isize) -> bool { - x > 0 && x < self.width as isize && y > 0 && y < self.height as isize - } - fn check_indexes(&self, x: usize, y: usize) { assert!( x < self.width, @@ -100,27 +96,6 @@ impl Grid for PixelGrid { self.bit_vec.get(x + y * self.width) } - fn get_optional(&self, x: isize, y: isize) -> Option { - if self.is_position_valid(x, y) { - Some(self.get(x as usize, y as usize)) - } else { - None - } - } - - fn set_optional( - &mut self, - x: isize, - y: isize, - value: bool, - ) -> Option { - if self.is_position_valid(x, y) { - Some(self.set(x as usize, y as usize, value)) - } else { - None - } - } - /// Sets the state of all pixels in the `PixelGrid`. /// /// # Arguments