Make Cells::translate a pure function

This commit is contained in:
tali 2022-12-14 10:59:19 -05:00
parent 1075546dd8
commit eebbde71b9
1 changed files with 9 additions and 7 deletions

View File

@ -124,9 +124,11 @@ impl<'c> Cells<'c> {
/// Translates the cells by the given amount in both directions. /// Translates the cells by the given amount in both directions.
#[inline] #[inline]
pub fn translate(&mut self, dx: i16, dy: i16) { pub fn translate(&self, dx: i16, dy: i16) -> Self {
self.x = self.x.checked_add(dx).expect("overflow/underflow"); let mut copy = *self;
self.y = self.y.checked_add(dy).expect("overflow/underflow"); copy.x = self.x.checked_add(dx).expect("overflow/underflow");
copy.y = self.y.checked_add(dy).expect("overflow/underflow");
copy
} }
#[cfg(test)] #[cfg(test)]
@ -157,14 +159,14 @@ pub struct Piece<S> {
impl<'c, S: Shape<'c>> Piece<S> { impl<'c, S: Shape<'c>> Piece<S> {
/// Returns the cells occupied by this piece. /// Returns the cells occupied by this piece.
pub fn cells(&self) -> Cells<'c> { pub fn cells(&self) -> Cells<'c> {
let mut cells = self.shape.cells(self.loc.r); self.shape
cells.translate(self.loc.x, self.loc.y); .cells(self.loc.r)
cells .translate(self.loc.x, self.loc.y)
} }
} }
#[cfg(test)] #[cfg(test)]
mod test { pub mod test {
use super::*; use super::*;
use crate::mat; use crate::mat;