fix for older rustc
authorGeoffrey Allott <geoffrey@allott.email>
Tue, 23 Mar 2021 20:10:33 +0000 (20:10 +0000)
committerGeoffrey Allott <geoffrey@allott.email>
Tue, 23 Mar 2021 20:10:33 +0000 (20:10 +0000)
src/game/poker/holdem.rs
src/util/array.rs [new file with mode: 0644]
src/util/mod.rs

index ed64bff622986805bdddcde3d475da07bdb93e87..5361ac9aae47552e82d5a56421c6096843c3c00a 100644 (file)
@@ -1,6 +1,5 @@
 use std::cmp::Ordering;
 use std::collections::{HashMap, HashSet};
-use std::convert::TryInto;
 
 use itertools::Itertools;
 
@@ -8,7 +7,7 @@ use crate::card::{Card, FIFTY_TWO_CARD_DECK};
 use crate::rng::{Seed, WaveRng};
 use crate::seats::Seats;
 use crate::username::Username;
-use crate::util::{max::IteratorMaxItems, timestamp::Timestamp};
+use crate::util::{array::TryIntoArray, max::IteratorMaxItems, timestamp::Timestamp};
 
 use super::super::{Action, ActionError, DealerAction, Game, StartCondition, UserAction, ValidatedUserAction};
 
@@ -640,7 +639,7 @@ impl Game for TexasHoldEm {
                     .iter()
                     .sorted_by_key(|&(username, _)| username)
                     .map(|(&username, hand)| (username, hand.iter().chain(self.community.iter()).cloned().collect::<Vec<_>>()))
-                    .filter_map(|(username, cards)| cards.try_into().ok().map(rank_7_card_hand).map(|hand| (username, hand)))
+                    .filter_map(|(username, cards)| cards.try_into_array().ok().map(rank_7_card_hand).map(|hand| (username, hand)))
                     .max_items_by_key(|(_, hand)| *hand);
                 info!("Showdown: community: {:?}", self.community);
                 info!("Showdown: all hands: {:?}", self.hands);
diff --git a/src/util/array.rs b/src/util/array.rs
new file mode 100644 (file)
index 0000000..882dd21
--- /dev/null
@@ -0,0 +1,12 @@
+pub trait TryIntoArray<A> {
+    fn try_into_array(self) -> Result<A, usize>;
+}
+
+impl <T: Copy> TryIntoArray<[T; 7]> for Vec<T> {
+    fn try_into_array(self) -> Result<[T; 7], usize> {
+        match *self {
+            [a, b, c, d, e, f, g] => Ok([a, b, c, d, e, f, g]),
+            _ => Err(self.len()),
+        }
+    }
+}
index 1a4b2720dfd954fc9a5fc8183b9d840c41953a07..6eddd824a97085b021cccf1917b219e0cd7cda59 100644 (file)
@@ -1,3 +1,4 @@
+pub mod array;
 pub mod dedup;
 pub mod max;
 pub mod timestamp;