From: MA Beaudet Date: Wed, 10 Nov 2021 11:48:36 +0000 (+0100) Subject: feat: apply C-COMMON-TRAITS C-CONV-TRAITS C-COLLECT for Cards and Card X-Git-Url: https://git.beaudet.xyz/?a=commitdiff_plain;h=86243562e663e48c783e8b35069472dbc858c796;p=poker-eval.git feat: apply C-COMMON-TRAITS C-CONV-TRAITS C-COLLECT for Cards and Card --- diff --git a/src/card.rs b/src/card.rs index 1d8f45f..43bf785 100644 --- a/src/card.rs +++ b/src/card.rs @@ -6,10 +6,14 @@ use crate::{constants::PRIMES, MyError, Rules}; use serde::{Deserialize, Serialize}; #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -#[derive(Debug, Default, Clone)] -pub struct Cards(pub(crate) Vec); +#[derive(Debug, Default, Clone, Hash)] +pub struct Cards(Vec); impl Cards { + pub fn new() -> Cards { + Cards(Vec::new()) + } + pub fn into_inner(self) -> Vec { self.0.into_iter().map(|c| c.into_inner()).collect() } @@ -19,6 +23,31 @@ impl Cards { } } +impl FromIterator for Cards { + fn from_iter(iter: I) -> Self + where + I: IntoIterator, + { + let mut cards = Cards::new(); + for i in iter { + cards.0.push(i) + } + + cards + } +} + +impl Extend for Cards { + fn extend(&mut self, iter: T) + where + T: IntoIterator, + { + for i in iter { + self.0.push(i); + } + } +} + impl FromStr for Cards { type Err = MyError; fn from_str(s: &str) -> Result { @@ -127,19 +156,15 @@ impl Default for Deck { /// 0b00001000_00000000_01001011_00100101_u32 /// ); #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Clone, Copy, Hash)] #[repr(transparent)] -pub struct Card(pub(crate) u32); +pub struct Card(u32); impl Card { pub fn into_inner(self) -> u32 { self.0 } - pub fn as_u32(&self) -> u32 { - self.0 - } - pub fn suit(&self) -> char { match (self.0 >> 12) & 0xF { 0b1000 => 'c', @@ -177,6 +202,12 @@ impl Card { } } +impl AsRef for Card { + fn as_ref(&self) -> &u32 { + &self.0 + } +} + impl TryFrom for Card { type Error = MyError; @@ -210,6 +241,12 @@ impl PartialOrd for Card { } } +impl Ord for Card { + fn cmp(&self, other: &Self) -> std::cmp::Ordering { + ((self.0 >> 8) & 0xF).cmp(&((other.0 >> 8) & 0xF)) + } +} + /// ``` /// use poker_eval::{Card}; /// use std::str::FromStr; @@ -224,6 +261,8 @@ impl PartialEq for Card { } } +impl Eq for Card {} + impl FromStr for Card { type Err = MyError;