feat: apply C-COMMON-TRAITS C-CONV-TRAITS C-COLLECT for Cards and Card
authorMA Beaudet <ma@beaudet.xyz>
Wed, 10 Nov 2021 11:48:36 +0000 (12:48 +0100)
committerMA Beaudet <ma@beaudet.xyz>
Wed, 10 Nov 2021 11:48:36 +0000 (12:48 +0100)
src/card.rs

index 1d8f45f..43bf785 100644 (file)
@@ -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<Card>);
+#[derive(Debug, Default, Clone, Hash)]
+pub struct Cards(Vec<Card>);
 
 impl Cards {
+    pub fn new() -> Cards {
+        Cards(Vec::new())
+    }
+
     pub fn into_inner(self) -> Vec<u32> {
         self.0.into_iter().map(|c| c.into_inner()).collect()
     }
@@ -19,6 +23,31 @@ impl Cards {
     }
 }
 
+impl FromIterator<Card> for Cards {
+    fn from_iter<I>(iter: I) -> Self
+    where
+        I: IntoIterator<Item = Card>,
+    {
+        let mut cards = Cards::new();
+        for i in iter {
+            cards.0.push(i)
+        }
+
+        cards
+    }
+}
+
+impl Extend<Card> for Cards {
+    fn extend<T>(&mut self, iter: T)
+    where
+        T: IntoIterator<Item = Card>,
+    {
+        for i in iter {
+            self.0.push(i);
+        }
+    }
+}
+
 impl FromStr for Cards {
     type Err = MyError;
     fn from_str(s: &str) -> Result<Self, Self::Err> {
@@ -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<u32> for Card {
+    fn as_ref(&self) -> &u32 {
+        &self.0
+    }
+}
+
 impl TryFrom<u32> 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;