diff --git a/core/src/colors.rs b/core/src/colors.rs index b60531b..9d67877 100644 --- a/core/src/colors.rs +++ b/core/src/colors.rs @@ -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 { + pub fn iter() -> std::slice::Iter<'static, Color> { Color::ALL.iter() } + pub fn into_iter() -> std::array::IntoIter { + Color::ALL.into_iter() + } + /// The other color #[must_use] pub const fn other(&self) -> Color { diff --git a/core/src/pieces.rs b/core/src/pieces.rs index d980105..00d0536 100644 --- a/core/src/pieces.rs +++ b/core/src/pieces.rs @@ -1,7 +1,7 @@ // Eryn Wells 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 { + 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 for Shape { try_from_string!(Shape); -impl Into for &Shape { - fn into(self) -> char { - self.to_ascii() +impl From<&Shape> for char { + fn from(shape: &Shape) -> char { + char::from(*shape) } } -impl Into for Shape { - fn into(self) -> char { - self.to_ascii() +impl From for char { + fn from(shape: Shape) -> char { + shape.to_ascii() } } @@ -174,6 +183,18 @@ impl fmt::Display for Piece { } } +impl From 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