Change to RwLock over Mutex, so we can read without locking, changes to BattleRandom to allow for cloning, and prevent race conditions.

This commit is contained in:
2022-06-04 12:40:32 +02:00
parent 310bf857d2
commit df662ce6b5
7 changed files with 57 additions and 57 deletions

View File

@@ -1,8 +1,22 @@
use crate::utils::random::Random;
use std::fmt::{Debug, Formatter};
use std::sync::Mutex;
#[derive(Default)]
pub struct BattleRandom {
random: Random,
random: Mutex<Random>,
}
impl BattleRandom {
pub fn new_with_seed(seed: u128) -> Self {
BattleRandom {
random: Mutex::new(Random::new(seed)),
}
}
pub fn get_rng(&self) -> &Mutex<Random> {
&self.random
}
}
impl Debug for BattleRandom {
@@ -11,20 +25,10 @@ impl Debug for BattleRandom {
}
}
impl BattleRandom {
pub fn new() -> Self {
return BattleRandom {
random: Default::default(),
};
}
pub fn new_with_seed(seed: u128) -> Self {
return BattleRandom {
random: Random::new(seed),
};
}
pub fn get_rng(&mut self) -> &mut Random {
&mut self.random
impl Clone for BattleRandom {
fn clone(&self) -> Self {
Self {
random: Mutex::new(self.random.lock().unwrap().clone()),
}
}
}