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