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()
}
}
}
+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> {
/// 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',
}
}
+impl AsRef<u32> for Card {
+ fn as_ref(&self) -> &u32 {
+ &self.0
+ }
+}
+
impl TryFrom<u32> for Card {
type Error = MyError;
}
}
+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;
}
}
+impl Eq for Card {}
+
impl FromStr for Card {
type Err = MyError;