card: excessively simple card iterator

This commit is contained in:
2026-04-14 16:51:15 +01:00
committed by oreodave
parent f1a26592ec
commit 2d0e98c7f4
2 changed files with 52 additions and 0 deletions

51
src/card/iters.rs Normal file
View File

@@ -0,0 +1,51 @@
use crate::card::{Card, PlayingCard, Rank, Suit};
pub struct CardIterator(Card);
impl Iterator for CardIterator {
type Item = Card;
fn next(&mut self) -> Option<Card> {
match self.0 {
Card::Joker(_) => None,
Card::PlayingCard(PlayingCard {
rank: Rank::Two,
suit: Suit::Spade,
deck,
}) => {
self.0 = Card::Joker((deck + 1) * -1);
None
}
Card::PlayingCard(pc) => {
self.0 = Card::from(i64::from(pc) + 1);
Some(self.0)
}
}
}
}
impl DoubleEndedIterator for CardIterator {
fn next_back(&mut self) -> Option<<Self as Iterator>::Item> {
match self.0 {
Card::Joker(_) => None,
Card::PlayingCard(PlayingCard {
rank: Rank::Three,
suit: Suit::Diamond,
deck,
}) => {
self.0 = Card::Joker((deck + 1) * -1);
None
}
Card::PlayingCard(pc) => {
self.0 = Card::from(i64::from(pc) - 1);
Some(self.0)
}
}
}
}
impl Card {
pub fn into_iter(self) -> CardIterator {
CardIterator(self)
}
}

View File

@@ -2,6 +2,7 @@ mod default;
mod display; mod display;
mod hash; mod hash;
mod impls; mod impls;
mod iters;
mod numerics; mod numerics;
mod ord; mod ord;