feat: add conversion AsRef for cards and some delegate methods to cards
authorMA Beaudet <ma@beaudet.xyz>
Wed, 10 Nov 2021 19:49:54 +0000 (20:49 +0100)
committerMA Beaudet <ma@beaudet.xyz>
Wed, 10 Nov 2021 19:49:54 +0000 (20:49 +0100)
src/card.rs

index 5595bf4..3e72a0f 100644 (file)
@@ -14,12 +14,20 @@ impl Cards {
         Cards(Vec::new())
     }
 
-    pub fn into_inner(self) -> Vec<u32> {
-        self.0.into_iter().map(|c| c.into_inner()).collect()
+    pub fn into_inner(self) -> Vec<Card> {
+        self.0
     }
 
-    pub fn as_vec_card(&self) -> &Vec<Card> {
-        &self.0
+    pub fn push(&mut self, value: Card) {
+        self.0.push(value)
+    }
+
+    pub fn is_empty(&self) -> bool {
+        self.0.is_empty()
+    }
+
+    pub fn len(&self) -> usize {
+        self.0.len()
     }
 }
 
@@ -30,13 +38,42 @@ impl FromIterator<Card> for Cards {
     {
         let mut cards = Cards::new();
         for i in iter {
-            cards.0.push(i)
+            cards.push(i)
         }
 
         cards
     }
 }
 
+impl FromIterator<Card> for Vec<u32> {
+    fn from_iter<T>(iter: T) -> Self
+    where
+        T: IntoIterator<Item = Card>,
+    {
+        let mut v = Vec::new();
+        for i in iter {
+            v.push(i.0)
+        }
+        v
+    }
+}
+
+impl IntoIterator for Cards {
+    type Item = Card;
+
+    type IntoIter = std::vec::IntoIter<Self::Item>;
+
+    fn into_iter(self) -> Self::IntoIter {
+        self.0.into_iter()
+    }
+}
+
+impl AsRef<Vec<Card>> for Cards {
+    fn as_ref(&self) -> &Vec<Card> {
+        &self.0
+    }
+}
+
 impl Extend<Card> for Cards {
     fn extend<T>(&mut self, iter: T)
     where
@@ -72,7 +109,7 @@ impl std::fmt::Display for Cards {
 
 impl From<Cards> for Vec<u32> {
     fn from(c: Cards) -> Self {
-        Vec::from(c.into_inner())
+        c.into_iter().map(|card| card.into_inner()).collect()
     }
 }
 
@@ -193,6 +230,12 @@ impl AsRef<u32> for Card {
     }
 }
 
+impl AsRef<Card> for Card {
+    fn as_ref(&self) -> &Card {
+        self
+    }
+}
+
 impl TryFrom<u32> for Card {
     type Error = ParseCardError;