Derive Hash for many data types

This commit is contained in:
tali 2022-12-14 15:03:43 -05:00
parent 096189373c
commit 8291a07ee4
3 changed files with 16 additions and 8 deletions

View File

@ -8,7 +8,7 @@ use crate::piece::{Piece, Rot, Shape};
///
/// DAS inputs are not included, since functionally DAS is equivalent to repeated taps,
/// and a bot can effectively hypertap infinitely fast during its analysis.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug)]
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Hash)]
pub enum Movement {
/// "Sonic" drop.
Drop,
@ -37,7 +37,7 @@ impl Movement {
}
/// Represents a direction of horizontal movement.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug)]
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Hash)]
#[repr(i16)]
pub enum Horizontal {
/// Left.
@ -56,7 +56,7 @@ pub enum Horizontal {
/// # use mino::{piece::Rot, input::Spin};
/// assert_eq!(Rot::N + Spin::Cw, Rot::E);
/// ```
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug)]
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Hash)]
#[repr(i8)]
pub enum Spin {
/// Clockwise.

View File

@ -5,7 +5,7 @@ use crate::matrix::Mat;
use core::ops::Range;
/// Represents a location for a piece, including its orientation.
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)]
pub struct Loc {
/// Horizontal coordinate.
pub x: i16,
@ -31,7 +31,7 @@ impl From<(i16, i16)> for Loc {
/// Represents a rotation state for a piece. The initial state is "north" (`N`), and there
/// 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, Hash)]
#[repr(i8)]
pub enum Rot {
/// North; the initial state.
@ -78,6 +78,7 @@ pub struct Cells<'c> {
}
impl PartialEq for Cells<'_> {
#[inline]
fn eq(&self, rhs: &Self) -> bool {
// XXX(iitalics): identical 'data' should imply identical width; width is only
// stored to make bounds tests faster but could theoretically be derived from the
@ -87,6 +88,13 @@ impl PartialEq for Cells<'_> {
}
}
impl core::hash::Hash for Cells<'_> {
#[inline]
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
(self.x, self.y, self.data()).hash(state)
}
}
impl<'c> Cells<'c> {
/// Constructs cells from the initial extents and rowwise bit representations of the
/// occupied cells.
@ -183,7 +191,7 @@ impl core::fmt::Debug for Cells<'_> {
}
/// Represents the current state of a piece.
#[derive(Clone, Eq, PartialEq, Debug)]
#[derive(Clone, Eq, PartialEq, Debug, Hash)]
pub struct Piece<S> {
/// The shape of the piece.
pub shape: S,
@ -238,7 +246,7 @@ pub mod test {
// .X.
// .XX origin at (1,1)
// ...
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)]
pub struct Tri;
impl Shape<'static> for Tri {

View File

@ -7,7 +7,7 @@ mod code_gen {
include!(concat!(env!("OUT_DIR"), "/srs.rs"));
}
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Hash)]
#[repr(u8)]
pub enum Shape {
I = code_gen::shape_indices::I as u8,