[core] Improve API of Shape and Color
Clean up type and method declarations by using better type spelling. Use more standard method spelling for iterators. Implement some useful traits.
This commit is contained in:
parent
534c022981
commit
7c65232c35
2 changed files with 38 additions and 13 deletions
|
@ -17,10 +17,14 @@ impl Color {
|
|||
/// Slice of all possible colors
|
||||
pub const ALL: [Color; Color::NUM] = [Color::White, Color::Black];
|
||||
|
||||
pub fn iter() -> impl Iterator<Item = &'static Color> {
|
||||
pub fn iter() -> std::slice::Iter<'static, Color> {
|
||||
Color::ALL.iter()
|
||||
}
|
||||
|
||||
pub fn into_iter() -> std::array::IntoIter<Color, { Self::NUM }> {
|
||||
Color::ALL.into_iter()
|
||||
}
|
||||
|
||||
/// The other color
|
||||
#[must_use]
|
||||
pub const fn other(&self) -> Color {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
// Eryn Wells <eryn@erynwells.me>
|
||||
|
||||
use crate::{errors::TryFromCharError, try_from_string, Color, Square};
|
||||
use std::{fmt, slice::Iter};
|
||||
use std::{array, fmt, slice};
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
|
||||
pub enum Shape {
|
||||
|
@ -14,7 +14,11 @@ pub enum Shape {
|
|||
}
|
||||
|
||||
impl Shape {
|
||||
pub const ALL: [Shape; 6] = [
|
||||
/// Number of piece shapes
|
||||
pub const NUM: usize = 6;
|
||||
|
||||
/// A slice of all piece shapes
|
||||
pub const ALL: [Shape; Self::NUM] = [
|
||||
Shape::Pawn,
|
||||
Shape::Knight,
|
||||
Shape::Bishop,
|
||||
|
@ -23,11 +27,16 @@ impl Shape {
|
|||
Shape::King,
|
||||
];
|
||||
|
||||
pub fn iter() -> Iter<'static, Shape> {
|
||||
pub fn iter() -> slice::Iter<'static, Self> {
|
||||
Shape::ALL.iter()
|
||||
}
|
||||
|
||||
pub fn promotable() -> Iter<'static, Shape> {
|
||||
pub fn into_iter() -> array::IntoIter<Self, { Self::NUM }> {
|
||||
Shape::ALL.into_iter()
|
||||
}
|
||||
|
||||
/// An iterator over the shapes that a pawn can promote to
|
||||
pub fn promotable() -> slice::Iter<'static, Shape> {
|
||||
const PROMOTABLE_SHAPES: [Shape; 4] =
|
||||
[Shape::Queen, Shape::Rook, Shape::Bishop, Shape::Knight];
|
||||
|
||||
|
@ -64,15 +73,15 @@ impl TryFrom<char> for Shape {
|
|||
|
||||
try_from_string!(Shape);
|
||||
|
||||
impl Into<char> for &Shape {
|
||||
fn into(self) -> char {
|
||||
self.to_ascii()
|
||||
impl From<&Shape> for char {
|
||||
fn from(shape: &Shape) -> char {
|
||||
char::from(*shape)
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<char> for Shape {
|
||||
fn into(self) -> char {
|
||||
self.to_ascii()
|
||||
impl From<Shape> for char {
|
||||
fn from(shape: Shape) -> char {
|
||||
shape.to_ascii()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -174,6 +183,18 @@ impl fmt::Display for Piece {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<PlacedPiece> for Piece {
|
||||
fn from(value: PlacedPiece) -> Self {
|
||||
value.piece
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&PlacedPiece> for Piece {
|
||||
fn from(value: &PlacedPiece) -> Self {
|
||||
value.piece
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
|
||||
pub struct PlacedPiece {
|
||||
piece: Piece,
|
||||
|
@ -198,8 +219,8 @@ impl PlacedPiece {
|
|||
/// The [Piece] itself
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn piece(&self) -> &Piece {
|
||||
&self.piece
|
||||
pub fn piece(&self) -> Piece {
|
||||
self.piece
|
||||
}
|
||||
|
||||
/// The square the piece is on
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue