I am writing a little Cards Against Humanity clone for personal use and am using it as a chance to learn Rust. I currently have the following structs:
// game.rs
pub struct Game<'c> {
deck: Deck,
players: Vec<Player<'c>>,
current_czar_index: usize,
}
// player.rs
pub struct Player<'c> {
pub hand: Vec<&'c Card>,
pub max_hand_size: usize,
pub is_czar: bool,
pub score: u8,
pub name: String,
}
// deck.rs
struct DiscardPile {
pub white: Mutex<Vec<usize>>,
pub black: Mutex<Vec<usize>>,
}
pub struct Deck {
white: Vec<Card>,
black: Vec<Card>,
pub used_sets: Vec<String>,
discard_pile: DiscardPile,
}
// card.rs
pub struct Card {
pub text: String,
pub uuid: Uuid,
pub pick: Option<u8>,
}
There are a few others (namely set which is used for reading in JSON files), but they aren't relevant.
I have a function in game.rs
pub fn deal_hands(&self) -> Result<(), Box<dyn std::error::Error>> {
for player in &mut self.players {
while player.hand.len() < player.max_hand_size {
player.add_card(self.deck.draw_white().unwrap())?;
}
}
Ok(())
}
that is giving me the following error:
error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements
--> src/game.rs:42:31
|
42 | player.add_card(self.deck.draw_white().unwrap())?;
| ^^^^^^^^^^
|
It specifically says that it is expecting &mut Player<'_>
but found a lifetime of &mut Player<'c>
.
HERE if you want to see the code firsthand.
How do I fix such a lifetime error, or do I need to rethink my architecture?