More work on using interior mutability instead of exterior mutability for dynamic types (Battle, Pokemon, etc).
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2022-06-18 15:52:39 +02:00
parent c45c7538bf
commit 5576bc8b80
21 changed files with 324 additions and 385 deletions

View File

@@ -1,10 +1,9 @@
use crate::dynamic_data::models::pokemon::Pokemon;
use parking_lot::RwLock;
use std::sync::Arc;
#[derive(Debug)]
pub struct PokemonParty<'pokemon, 'library> {
pokemon: Vec<Option<Arc<RwLock<Pokemon<'pokemon, 'library>>>>>,
pokemon: Vec<Option<Arc<Pokemon<'pokemon, 'library>>>>,
}
impl<'own, 'library> PokemonParty<'own, 'library> {
@@ -16,11 +15,11 @@ impl<'own, 'library> PokemonParty<'own, 'library> {
Self { pokemon }
}
pub fn new_from_vec(pokemon: Vec<Option<Arc<RwLock<Pokemon<'own, 'library>>>>>) -> Self {
pub fn new_from_vec(pokemon: Vec<Option<Arc<Pokemon<'own, 'library>>>>) -> Self {
Self { pokemon }
}
pub fn at(&self, index: usize) -> &Option<Arc<RwLock<Pokemon<'own, 'library>>>> {
pub fn at(&self, index: usize) -> &Option<Arc<Pokemon<'own, 'library>>> {
let opt = self.pokemon.get(index);
if let Some(v) = opt {
v
@@ -36,8 +35,8 @@ impl<'own, 'library> PokemonParty<'own, 'library> {
pub fn swap_into(
&mut self,
index: usize,
pokemon: Option<Arc<RwLock<Pokemon<'own, 'library>>>>,
) -> Option<Arc<RwLock<Pokemon<'own, 'library>>>> {
pokemon: Option<Arc<Pokemon<'own, 'library>>>,
) -> Option<Arc<Pokemon<'own, 'library>>> {
if index >= self.pokemon.len() {
return pokemon;
}
@@ -48,7 +47,7 @@ impl<'own, 'library> PokemonParty<'own, 'library> {
pub fn has_usable_pokemon(&self) -> bool {
for pokemon in self.pokemon.iter().flatten() {
if pokemon.read().is_usable() {
if pokemon.is_usable() {
return true;
}
}
@@ -59,7 +58,7 @@ impl<'own, 'library> PokemonParty<'own, 'library> {
self.pokemon.len()
}
pub fn pokemon(&self) -> &Vec<Option<Arc<RwLock<Pokemon<'own, 'library>>>>> {
pub fn pokemon(&self) -> &Vec<Option<Arc<Pokemon<'own, 'library>>>> {
&self.pokemon
}