More removal of RwLocks and replace it with Atomics, to prevent locks.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2022-06-18 14:17:29 +02:00
parent 59d7344729
commit c45c7538bf
15 changed files with 293 additions and 192 deletions

View File

@@ -39,7 +39,7 @@ pub mod tests {
use crate::static_data::species_data::form::Form;
use crate::static_data::species_data::learnable_moves::LearnableMoves;
use crate::static_data::species_data::species::Species;
use crate::static_data::statistic_set::StatisticSet;
use crate::static_data::StaticStatisticSet;
use hashbrown::HashSet;
fn build_species<'a>() -> Species {
@@ -55,7 +55,7 @@ pub mod tests {
0.0,
0,
Vec::new(),
StatisticSet::default(),
StaticStatisticSet::default(),
Vec::new(),
Vec::new(),
LearnableMoves::new(),

View File

@@ -1,8 +1,7 @@
use crate::static_data::Ability;
use crate::static_data::AbilityIndex;
use crate::static_data::LearnableMoves;
use crate::static_data::Statistic;
use crate::static_data::StatisticSet;
use crate::static_data::{Ability, StaticStatisticSet};
use crate::Random;
use crate::StringKey;
use hashbrown::HashSet;
@@ -14,7 +13,7 @@ pub struct Form {
weight: f32,
base_experience: u32,
types: Vec<u8>,
base_stats: StatisticSet<u16>,
base_stats: StaticStatisticSet<u16>,
abilities: Vec<StringKey>,
hidden_abilities: Vec<StringKey>,
moves: LearnableMoves,
@@ -28,7 +27,7 @@ impl Form {
weight: f32,
base_experience: u32,
types: Vec<u8>,
base_stats: StatisticSet<u16>,
base_stats: StaticStatisticSet<u16>,
abilities: Vec<StringKey>,
hidden_abilities: Vec<StringKey>,
moves: LearnableMoves,
@@ -63,8 +62,8 @@ impl Form {
pub fn types(&self) -> &Vec<u8> {
&self.types
}
pub fn base_stats(&self) -> StatisticSet<u16> {
self.base_stats
pub fn base_stats(&self) -> &StaticStatisticSet<u16> {
&self.base_stats
}
pub fn abilities(&self) -> &Vec<StringKey> {
&self.abilities

View File

@@ -1,10 +1,12 @@
use super::statistics::Statistic;
use num_traits::{cast, clamp, PrimInt};
use atomic_prim_traits::AtomicInt;
use num_traits::{clamp, NumCast, PrimInt};
use std::sync::atomic::Ordering;
#[derive(Default, Eq, PartialEq, Copy, Clone, Debug)]
#[derive(Default, Eq, PartialEq, Clone, Debug)]
pub struct StatisticSet<T>
where
T: PrimInt,
T: AtomicInt,
{
hp: T,
attack: T,
@@ -16,85 +18,92 @@ where
impl<T> StatisticSet<T>
where
T: PrimInt,
T: AtomicInt,
{
pub fn new(hp: T, attack: T, defense: T, special_attack: T, special_defense: T, speed: T) -> Self {
pub fn new(
hp: T::Prim,
attack: T::Prim,
defense: T::Prim,
special_attack: T::Prim,
special_defense: T::Prim,
speed: T::Prim,
) -> Self {
Self {
hp,
attack,
defense,
special_attack,
special_defense,
speed,
hp: T::new(hp),
attack: T::new(attack),
defense: T::new(defense),
special_attack: T::new(special_attack),
special_defense: T::new(special_defense),
speed: T::new(speed),
}
}
pub fn hp(&self) -> T {
self.hp
pub fn hp(&self) -> T::Prim {
self.hp.load(Ordering::Relaxed)
}
pub fn attack(&self) -> T {
self.attack
pub fn attack(&self) -> T::Prim {
self.attack.load(Ordering::Relaxed)
}
pub fn defense(&self) -> T {
self.defense
pub fn defense(&self) -> T::Prim {
self.defense.load(Ordering::Relaxed)
}
pub fn special_attack(&self) -> T {
self.special_attack
pub fn special_attack(&self) -> T::Prim {
self.special_attack.load(Ordering::Relaxed)
}
pub fn special_defense(&self) -> T {
self.special_defense
pub fn special_defense(&self) -> T::Prim {
self.special_defense.load(Ordering::Relaxed)
}
pub fn speed(&self) -> T {
self.speed
pub fn speed(&self) -> T::Prim {
self.speed.load(Ordering::Relaxed)
}
pub const fn get_stat(&self, stat: Statistic) -> T {
pub fn get_stat(&self, stat: Statistic) -> T::Prim {
match stat {
Statistic::HP => self.hp,
Statistic::Attack => self.attack,
Statistic::Defense => self.defense,
Statistic::SpecialAttack => self.special_attack,
Statistic::SpecialDefense => self.special_defense,
Statistic::Speed => self.speed,
Statistic::HP => self.hp.load(Ordering::Relaxed),
Statistic::Attack => self.attack.load(Ordering::Relaxed),
Statistic::Defense => self.defense.load(Ordering::Relaxed),
Statistic::SpecialAttack => self.special_attack.load(Ordering::Relaxed),
Statistic::SpecialDefense => self.special_defense.load(Ordering::Relaxed),
Statistic::Speed => self.speed.load(Ordering::Relaxed),
}
}
pub fn set_stat(&mut self, stat: Statistic, value: T) {
pub fn set_stat(&self, stat: Statistic, value: T::Prim) {
match stat {
Statistic::HP => self.hp = value,
Statistic::Attack => self.attack = value,
Statistic::Defense => self.defense = value,
Statistic::SpecialAttack => self.special_attack = value,
Statistic::SpecialDefense => self.special_defense = value,
Statistic::Speed => self.speed = value,
Statistic::HP => self.hp.store(value, Ordering::SeqCst),
Statistic::Attack => self.attack.store(value, Ordering::SeqCst),
Statistic::Defense => self.defense.store(value, Ordering::SeqCst),
Statistic::SpecialAttack => self.special_attack.store(value, Ordering::SeqCst),
Statistic::SpecialDefense => self.special_defense.store(value, Ordering::SeqCst),
Statistic::Speed => self.speed.store(value, Ordering::SeqCst),
}
}
pub fn increase_stat(&mut self, stat: Statistic, value: T) {
pub fn increase_stat(&self, stat: Statistic, value: T::Prim) {
match stat {
Statistic::HP => self.hp = self.hp + value,
Statistic::Attack => self.attack = self.attack + value,
Statistic::Defense => self.defense = self.defense + value,
Statistic::SpecialAttack => self.special_attack = self.special_attack + value,
Statistic::SpecialDefense => self.special_defense = self.special_defense + value,
Statistic::Speed => self.speed = self.speed + value,
}
Statistic::HP => self.hp.fetch_add(value, Ordering::SeqCst),
Statistic::Attack => self.attack.fetch_add(value, Ordering::SeqCst),
Statistic::Defense => self.defense.fetch_add(value, Ordering::SeqCst),
Statistic::SpecialAttack => self.special_attack.fetch_add(value, Ordering::SeqCst),
Statistic::SpecialDefense => self.special_defense.fetch_add(value, Ordering::SeqCst),
Statistic::Speed => self.speed.fetch_add(value, Ordering::SeqCst),
};
}
pub fn decrease_stat(&mut self, stat: Statistic, value: T) {
pub fn decrease_stat(&self, stat: Statistic, value: T::Prim) {
match stat {
Statistic::HP => self.hp = self.hp - value,
Statistic::Attack => self.attack = self.attack - value,
Statistic::Defense => self.defense = self.defense - value,
Statistic::SpecialAttack => self.special_attack = self.special_attack - value,
Statistic::SpecialDefense => self.special_defense = self.special_defense - value,
Statistic::Speed => self.speed = self.speed - value,
}
Statistic::HP => self.hp.fetch_sub(value, Ordering::SeqCst),
Statistic::Attack => self.attack.fetch_sub(value, Ordering::SeqCst),
Statistic::Defense => self.defense.fetch_sub(value, Ordering::SeqCst),
Statistic::SpecialAttack => self.special_attack.fetch_sub(value, Ordering::SeqCst),
Statistic::SpecialDefense => self.special_defense.fetch_sub(value, Ordering::SeqCst),
Statistic::Speed => self.speed.fetch_sub(value, Ordering::SeqCst),
};
}
}
#[derive(Default, Eq, PartialEq, Copy, Clone, Debug)]
pub struct ClampedStatisticSet<T, const MIN: i64, const MAX: i64>
#[derive(Default, Eq, PartialEq, Clone, Debug)]
pub struct StaticStatisticSet<T>
where
T: PrimInt,
{
@@ -106,37 +115,37 @@ where
speed: T,
}
impl<T, const MIN: i64, const MAX: i64> ClampedStatisticSet<T, MIN, MAX>
impl<T> StaticStatisticSet<T>
where
T: PrimInt,
{
pub fn new(hp: T, attack: T, defense: T, special_attack: T, special_defense: T, speed: T) -> Self {
pub const fn new(hp: T, attack: T, defense: T, special_attack: T, special_defense: T, speed: T) -> Self {
Self {
hp: cast(clamp(cast::<T, i64>(hp).unwrap(), MIN, MAX)).unwrap(),
attack: cast(clamp(cast::<T, i64>(attack).unwrap(), MIN, MAX)).unwrap(),
defense: cast(clamp(cast::<T, i64>(defense).unwrap(), MIN, MAX)).unwrap(),
special_attack: cast(clamp(cast::<T, i64>(special_attack).unwrap(), MIN, MAX)).unwrap(),
special_defense: cast(clamp(cast::<T, i64>(special_defense).unwrap(), MIN, MAX)).unwrap(),
speed: cast(clamp(cast::<T, i64>(speed).unwrap(), MIN, MAX)).unwrap(),
hp,
attack,
defense,
special_attack,
special_defense,
speed,
}
}
pub fn hp(&self) -> T {
pub const fn hp(&self) -> T {
self.hp
}
pub fn attack(&self) -> T {
pub const fn attack(&self) -> T {
self.attack
}
pub fn defense(&self) -> T {
pub const fn defense(&self) -> T {
self.defense
}
pub fn special_attack(&self) -> T {
pub const fn special_attack(&self) -> T {
self.special_attack
}
pub fn special_defense(&self) -> T {
pub const fn special_defense(&self) -> T {
self.special_defense
}
pub fn speed(&self) -> T {
pub const fn speed(&self) -> T {
self.speed
}
@@ -150,55 +159,143 @@ where
Statistic::Speed => self.speed,
}
}
}
pub fn set_stat(&mut self, stat: Statistic, mut value: T) {
if value < T::from(MIN).unwrap() {
value = T::from(MIN).unwrap();
} else if value > T::from(MAX).unwrap() {
value = T::from(MAX).unwrap();
}
match stat {
Statistic::HP => self.hp = value,
Statistic::Attack => self.attack = value,
Statistic::Defense => self.defense = value,
Statistic::SpecialAttack => self.special_attack = value,
Statistic::SpecialDefense => self.special_defense = value,
Statistic::Speed => self.speed = value,
#[derive(Default, Eq, PartialEq, Clone, Debug)]
pub struct ClampedStatisticSet<T, const MIN: i64, const MAX: i64>
where
T: AtomicInt,
{
hp: T,
attack: T,
defense: T,
special_attack: T,
special_defense: T,
speed: T,
}
impl<T, const MIN: i64, const MAX: i64> ClampedStatisticSet<T, MIN, MAX>
where
T: AtomicInt,
<T as AtomicInt>::Prim: NumCast,
<T as AtomicInt>::Prim: PrimInt,
{
pub fn min() -> T::Prim {
<<T as AtomicInt>::Prim as NumCast>::from(MIN).unwrap()
}
pub fn max() -> T::Prim {
<<T as AtomicInt>::Prim as NumCast>::from(MAX).unwrap()
}
fn clamped_cast(v: T::Prim) -> T {
let v = clamp(v, Self::min(), Self::max());
T::new(v)
}
pub fn new(
hp: T::Prim,
attack: T::Prim,
defense: T::Prim,
special_attack: T::Prim,
special_defense: T::Prim,
speed: T::Prim,
) -> Self {
Self {
hp: Self::clamped_cast(hp),
attack: Self::clamped_cast(attack),
defense: Self::clamped_cast(defense),
special_attack: Self::clamped_cast(special_attack),
special_defense: Self::clamped_cast(special_defense),
speed: Self::clamped_cast(speed),
}
}
fn change_stat(mut new_value: T, original_value: &mut T) -> bool {
if new_value < T::from(MIN).unwrap() {
new_value = T::from(MIN).unwrap();
} else if new_value > T::from(MAX).unwrap() {
new_value = T::from(MAX).unwrap();
pub fn hp(&self) -> T::Prim {
self.hp.load(Ordering::Relaxed)
}
pub fn attack(&self) -> T::Prim {
self.attack.load(Ordering::Relaxed)
}
pub fn defense(&self) -> T::Prim {
self.defense.load(Ordering::Relaxed)
}
pub fn special_attack(&self) -> T::Prim {
self.special_attack.load(Ordering::Relaxed)
}
pub fn special_defense(&self) -> T::Prim {
self.special_defense.load(Ordering::Relaxed)
}
pub fn speed(&self) -> T::Prim {
self.speed.load(Ordering::Relaxed)
}
pub fn get_stat(&self, stat: Statistic) -> T::Prim {
match stat {
Statistic::HP => self.hp.load(Ordering::Relaxed),
Statistic::Attack => self.attack.load(Ordering::Relaxed),
Statistic::Defense => self.defense.load(Ordering::Relaxed),
Statistic::SpecialAttack => self.special_attack.load(Ordering::Relaxed),
Statistic::SpecialDefense => self.special_defense.load(Ordering::Relaxed),
Statistic::Speed => self.speed.load(Ordering::Relaxed),
}
if *original_value == new_value {
}
pub fn set_stat(&self, stat: Statistic, mut value: T::Prim) {
if value < Self::min() {
value = Self::min();
} else if value > Self::max() {
value = Self::max();
}
match stat {
Statistic::HP => self.hp.store(value, Ordering::SeqCst),
Statistic::Attack => self.attack.store(value, Ordering::SeqCst),
Statistic::Defense => self.defense.store(value, Ordering::SeqCst),
Statistic::SpecialAttack => self.special_attack.store(value, Ordering::SeqCst),
Statistic::SpecialDefense => self.special_defense.store(value, Ordering::SeqCst),
Statistic::Speed => self.speed.store(value, Ordering::SeqCst),
}
}
fn change_stat<F>(v: &T, f: F) -> bool
where
F: FnOnce(T::Prim) -> T::Prim,
{
let original_value = v.load(Ordering::Relaxed);
let mut new_value = f(original_value);
if new_value < Self::min() {
new_value = Self::min();
} else if new_value > Self::max() {
new_value = Self::max();
}
if original_value == new_value {
return false;
}
*original_value = new_value;
v.store(new_value, Ordering::SeqCst);
true
}
pub fn increase_stat(&mut self, stat: Statistic, value: T) -> bool {
pub fn increase_stat(&self, stat: Statistic, value: T::Prim) -> bool
where
<T as AtomicInt>::Prim: PrimInt,
{
match stat {
Statistic::HP => Self::change_stat(self.hp + value, &mut self.hp),
Statistic::Attack => Self::change_stat(self.attack + value, &mut self.attack),
Statistic::Defense => Self::change_stat(self.defense + value, &mut self.defense),
Statistic::SpecialAttack => Self::change_stat(self.special_attack + value, &mut self.special_attack),
Statistic::SpecialDefense => Self::change_stat(self.special_defense + value, &mut self.special_defense),
Statistic::Speed => Self::change_stat(self.speed + value, &mut self.speed),
Statistic::HP => Self::change_stat(&self.hp, |a| a + value),
Statistic::Attack => Self::change_stat(&self.attack, |a| a + value),
Statistic::Defense => Self::change_stat(&self.defense, |a| a + value),
Statistic::SpecialAttack => Self::change_stat(&self.special_attack, |a| a + value),
Statistic::SpecialDefense => Self::change_stat(&self.special_defense, |a| a + value),
Statistic::Speed => Self::change_stat(&self.speed, |a| a + value),
}
}
pub fn decrease_stat(&mut self, stat: Statistic, value: T) -> bool {
pub fn decrease_stat(&self, stat: Statistic, value: T::Prim) -> bool {
match stat {
Statistic::HP => Self::change_stat(self.hp - value, &mut self.hp),
Statistic::Attack => Self::change_stat(self.attack - value, &mut self.attack),
Statistic::Defense => Self::change_stat(self.defense - value, &mut self.defense),
Statistic::SpecialAttack => Self::change_stat(self.special_attack - value, &mut self.special_attack),
Statistic::SpecialDefense => Self::change_stat(self.special_defense - value, &mut self.special_defense),
Statistic::Speed => Self::change_stat(self.speed - value, &mut self.speed),
Statistic::HP => Self::change_stat(&self.hp, |a| a - value),
Statistic::Attack => Self::change_stat(&self.attack, |a| a - value),
Statistic::Defense => Self::change_stat(&self.defense, |a| a - value),
Statistic::SpecialAttack => Self::change_stat(&self.special_attack, |a| a - value),
Statistic::SpecialDefense => Self::change_stat(&self.special_defense, |a| a - value),
Statistic::Speed => Self::change_stat(&self.speed, |a| a - value),
}
}
}
@@ -206,10 +303,11 @@ where
#[cfg(test)]
mod tests {
use super::*;
use std::sync::atomic::AtomicI32;
#[test]
fn create_get_values() {
let set = StatisticSet::new(1, 2, 3, 4, 5, 6);
let set = StatisticSet::<AtomicI32>::new(1, 2, 3, 4, 5, 6);
assert_eq!(set.hp(), 1);
assert_eq!(set.get_stat(Statistic::HP), 1);
assert_eq!(set.attack(), 2);
@@ -226,7 +324,7 @@ mod tests {
#[test]
fn create_set_value() {
let mut set = StatisticSet::new(1, 2, 3, 4, 5, 6);
let set = StatisticSet::<AtomicI32>::new(1, 2, 3, 4, 5, 6);
set.set_stat(Statistic::HP, 20);
assert_eq!(set.hp(), 20);
@@ -244,21 +342,21 @@ mod tests {
#[test]
fn create_increase_value() {
let mut set = StatisticSet::new(1, 2, 3, 4, 5, 6);
let set = StatisticSet::<AtomicI32>::new(1, 2, 3, 4, 5, 6);
set.increase_stat(Statistic::SpecialAttack, 5);
assert_eq!(set.special_attack(), 9);
}
#[test]
fn create_decrease_value() {
let mut set = StatisticSet::new(1, 2, 3, 4, 5, 6);
let set = StatisticSet::<AtomicI32>::new(1, 2, 3, 4, 5, 6);
set.decrease_stat(Statistic::SpecialAttack, 5);
assert_eq!(set.special_attack(), -1);
}
#[test]
fn create_clamped_get_values() {
let set = ClampedStatisticSet::<i32, { -2 }, 4>::new(-5, 2, 3, 4, 5, 6);
let set = ClampedStatisticSet::<AtomicI32, { -2 }, 4>::new(-5, 2, 3, 4, 5, 6);
assert_eq!(set.hp(), -2);
assert_eq!(set.get_stat(Statistic::HP), -2);
assert_eq!(set.attack(), 2);
@@ -275,7 +373,7 @@ mod tests {
#[test]
fn create_clamped_set_value() {
let mut set = ClampedStatisticSet::<i32, { -2 }, 4>::new(1, 2, 3, 4, 5, 6);
let set = ClampedStatisticSet::<AtomicI32, { -2 }, 4>::new(1, 2, 3, 4, 5, 6);
set.set_stat(Statistic::SpecialAttack, 20);
assert_eq!(set.special_attack(), 4);
set.set_stat(Statistic::SpecialAttack, -10);
@@ -284,7 +382,7 @@ mod tests {
#[test]
fn create_clamped_increase_value() {
let mut set = ClampedStatisticSet::<i32, { -2 }, 4>::new(1, 2, 3, 2, 2, 6);
let set = ClampedStatisticSet::<AtomicI32, { -2 }, 4>::new(1, 2, 3, 2, 2, 6);
let mut has_changed = set.increase_stat(Statistic::SpecialAttack, 20);
assert!(has_changed);
assert_eq!(set.special_attack(), 4);
@@ -295,7 +393,7 @@ mod tests {
#[test]
fn create_clamped_decrease_value() {
let mut set = ClampedStatisticSet::<i32, { -2 }, 4>::new(1, 2, 3, 2, 2, 6);
let set = ClampedStatisticSet::<AtomicI32, { -2 }, 4>::new(1, 2, 3, 2, 2, 6);
let mut has_changed = set.decrease_stat(Statistic::SpecialAttack, 20);
assert!(has_changed);
assert_eq!(set.special_attack(), -2);