From bfbd6dc02cf0606ecced196ebfea274471699612 Mon Sep 17 00:00:00 2001 From: Pedro Ferreira Date: Wed, 23 Apr 2025 21:51:38 +0100 Subject: [PATCH] fix(card_deck): make reference necessity consistent --- solutions/card_deck/src/lib.rs | 15 +++++++++------ tests/card_deck_test/src/main.rs | 8 ++++---- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/solutions/card_deck/src/lib.rs b/solutions/card_deck/src/lib.rs index 270118cf..d5d64b5f 100644 --- a/solutions/card_deck/src/lib.rs +++ b/solutions/card_deck/src/lib.rs @@ -18,11 +18,12 @@ pub enum Rank { } impl Rank { + #[inline] pub fn random() -> Self { - let value = rand::thread_rng().gen_range(1..14); - Self::translate(value) + Self::translate(rand::thread_rng().gen_range(1..14)) } + #[inline] pub fn translate(value: u8) -> Self { match value { 1 => Self::Ace, @@ -36,11 +37,12 @@ impl Rank { } impl Suit { + #[inline] pub fn random() -> Self { - let value = rand::thread_rng().gen_range(1..5); - Self::translate(value) + Self::translate(rand::thread_rng().gen_range(1..5)) } + #[inline] pub fn translate(value: u8) -> Self { match value { 1 => Self::Heart, @@ -58,8 +60,9 @@ pub struct Card { pub rank: Rank, } -pub fn winner_card(card: Card) -> bool { - card == Card { +#[inline] +pub fn winner_card(card: &Card) -> bool { + card == &Card { suit: Suit::Spade, rank: Rank::Ace, } diff --git a/tests/card_deck_test/src/main.rs b/tests/card_deck_test/src/main.rs index 58fd8062..43873adf 100644 --- a/tests/card_deck_test/src/main.rs +++ b/tests/card_deck_test/src/main.rs @@ -6,9 +6,9 @@ fn main() { suit: Suit::random(), }; - println!("Your card is {:?}", your_card); + println!("Your card is {:?}", &your_card); - if card_deck::winner_card(your_card) { + if card_deck::winner_card(&your_card) { println!("You are the winner!"); } } @@ -17,7 +17,7 @@ fn main() { mod tests { use super::*; - // We cannot test the randomness as there's no 100% accurate consistent way to prove through a predicate that it yields a truly random number + // We cannot truly test the randomness as there's no 100% accurate consistent way to prove through a predicate that it yields a truly random number #[test] fn test_winner() { @@ -33,7 +33,7 @@ mod tests { suit: Suit::translate(suit), }; - assert_eq!(card_deck::winner_card(card), card == winner); + assert_eq!(card_deck::winner_card(&card), card == winner); } } }