Rename types in location.rs

This commit is contained in:
tali 2022-12-13 15:50:05 -05:00
parent 0597b3b538
commit fa6aac86f6
2 changed files with 24 additions and 24 deletions

View File

@ -3,7 +3,7 @@
pub mod location; pub mod location;
pub mod matrix; pub mod matrix;
pub use location::Location; pub use location::Loc;
pub use matrix::Mat; pub use matrix::Mat;
/// Allows constructing a `Mat` constant with string literals: /// Allows constructing a `Mat` constant with string literals:

View File

@ -2,20 +2,20 @@
/// Represents a location for a piece, including its orientation. /// Represents a location for a piece, including its orientation.
#[derive(Copy, Clone, Eq, PartialEq, Debug)] #[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub struct Location { pub struct Loc {
/// Horizontal coordinate. /// Horizontal coordinate.
pub x: i16, pub x: i16,
/// Vertical coordinate. /// Vertical coordinate.
pub y: i16, pub y: i16,
/// Orientation. /// Rotation state.
pub r: Orientation, pub r: Rot,
} }
/// Represents an orientation state for a piece. The initial state is "north" (`N`), and /// Represents a rotation state for a piece. The initial state is "north" (`N`), and there
/// there are 4 total orientations to represent each 90 degree turn possible. /// are 4 total orientations to represent each 90 degree turn possible.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Default)] #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Default)]
#[repr(i8)] #[repr(i8)]
pub enum Orientation { pub enum Rot {
/// North. The initial state. /// North. The initial state.
#[default] #[default]
N = 0, N = 0,
@ -27,26 +27,26 @@ pub enum Orientation {
W = 3, W = 3,
} }
impl From<i8> for Orientation { impl From<i8> for Rot {
#[inline] #[inline]
fn from(v: i8) -> Self { fn from(v: i8) -> Self {
unsafe { core::mem::transmute(v & 3) } unsafe { core::mem::transmute(v & 3) }
} }
} }
/// Represents a rotation operation. Includes 180 degree "flips", which are non-standard /// Represents a rotating operation. Includes 180 degree "flips", which are non-standard
/// for tetris but exist in many unofficial iterations of the game. /// for tetris but exist in many unofficial iterations of the game.
/// ///
/// Rotations may be combined with orientations using the [addition /// Rotations may be performed on [rotation states](Rot) using the [addition
/// operator](core::ops::Add). /// operator](core::ops::Add).
/// ///
/// ``` /// ```
/// # use mino::location::{Orientation, Rotation}; /// # use mino::location::{Rot, Spin};
/// assert_eq!(Orientation::N + Rotation::Cw, Orientation::E); /// assert_eq!(Rot::N + Spin::Cw, Rot::E);
/// ``` /// ```
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug)] #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug)]
#[repr(i8)] #[repr(i8)]
pub enum Rotation { pub enum Spin {
/// Clockwise. /// Clockwise.
Cw = 1, Cw = 1,
/// 180 degrees. /// 180 degrees.
@ -55,15 +55,15 @@ pub enum Rotation {
Ccw = 3, Ccw = 3,
} }
impl core::ops::Add<Rotation> for Orientation { impl core::ops::Add<Spin> for Rot {
type Output = Orientation; type Output = Rot;
fn add(self, r: Rotation) -> Orientation { fn add(self, r: Spin) -> Rot {
(self as i8).wrapping_add(r as i8).into() (self as i8).wrapping_add(r as i8).into()
} }
} }
impl core::ops::AddAssign<Rotation> for Orientation { impl core::ops::AddAssign<Spin> for Rot {
fn add_assign(&mut self, r: Rotation) { fn add_assign(&mut self, r: Spin) {
*self = *self + r; *self = *self + r;
} }
} }
@ -74,8 +74,8 @@ mod test {
#[test] #[test]
fn test_angle_rotate() { fn test_angle_rotate() {
use Orientation::*; use Rot::*;
use Rotation::*; use Spin::*;
assert_eq!(N + Cw, E); assert_eq!(N + Cw, E);
assert_eq!(N + Ccw, W); assert_eq!(N + Ccw, W);
assert_eq!(N + Flip, S); assert_eq!(N + Flip, S);
@ -96,14 +96,14 @@ mod test {
#[test] #[test]
fn test_angle_from_i8() { fn test_angle_from_i8() {
let mut r = Orientation::N; let mut r = Rot::N;
for i in 0i8..=127 { for i in 0i8..=127 {
assert_eq!(r, i.into(), "i={i}"); assert_eq!(r, i.into(), "i={i}");
r += Rotation::Cw; r += Spin::Cw;
} }
r = Orientation::N; r = Rot::N;
for i in (-128i8..=-1).rev() { for i in (-128i8..=-1).rev() {
r += Rotation::Ccw; r += Spin::Ccw;
assert_eq!(r, i.into(), "i={i}"); assert_eq!(r, i.into(), "i={i}");
} }
} }