Remove lifetime mess, replace a lot of code with Arc instead of borrows.
Some checks failed
continuous-integration/drone/push Build is failing

This cleans up the codebase massively, and allows me to maintain some semblance of sanity.
This commit is contained in:
2022-08-20 13:17:20 +02:00
parent 2d4253e155
commit 55cc0906c9
34 changed files with 320 additions and 366 deletions

View File

@@ -115,10 +115,7 @@ pub fn load_items(path: &String, lib: &mut ItemLibrary) {
}
}
lib.add(
&name,
Box::new(Item::new(&name, category, battle_category, price as i32, flags)),
);
lib.add(&name, Item::new(&name, category, battle_category, price as i32, flags));
}
}
@@ -161,7 +158,7 @@ pub fn load_abilities(path: &String, ability_library: &mut AbilityLibrary) {
}
}
ability_library.add(&name, Box::new(Ability::new(&name, &effect, parameters)));
ability_library.add(&name, Ability::new(&name, &effect, parameters));
}
}
@@ -262,7 +259,7 @@ pub fn load_species(path: &String, library: &mut StaticData) {
let default_form_value = &forms["default"];
let default_form = parse_form("default".into(), default_form_value, library);
let species = Box::new(Species::new(
let species = Species::new(
id as u16,
&name,
gender_rate as f32,
@@ -270,7 +267,7 @@ pub fn load_species(path: &String, library: &mut StaticData) {
catch_rate as u8,
default_form,
Default::default(),
));
);
library.species_mut().add(&name, species);
}
}

View File

@@ -43,13 +43,13 @@ struct TestPokemon {
}
impl TestCase {
pub fn run_test(&self, library: &DynamicLibrary) {
pub fn run_test(&self, library: Arc<DynamicLibrary>) {
let mut parties = Vec::new();
for party in &self.battle_setup.parties {
let pokemon = party
.pokemon
.iter()
.map(|a| Some(Arc::new(a.to_pokemon(library))))
.map(|a| Some(Arc::new(a.to_pokemon(library.clone()))))
.collect();
let indices = party.indices.iter().map(|a| (a[0], a[1])).collect();
parties.push((Arc::new(PokemonParty::new_from_vec(pokemon)), indices));
@@ -73,7 +73,7 @@ impl TestCase {
}
impl TestPokemon {
fn to_pokemon<'a>(&'a self, library: &'a DynamicLibrary) -> Pokemon {
fn to_pokemon(&self, library: Arc<DynamicLibrary>) -> Pokemon {
let mut builder = PokemonBuilder::new(library, StringKey::new(self.species.as_str()), self.level);
for move_name in &self.moves {
builder = builder.learn_move(StringKey::new(move_name));

View File

@@ -16,16 +16,18 @@ use crate::common::{library_loader, TestCase};
pub mod common;
static LIBRARY: OnceCell<DynamicLibrary> = OnceCell::uninit();
static LIBRARY: OnceCell<Arc<DynamicLibrary>> = OnceCell::uninit();
fn get_library<'a>() -> &'a DynamicLibrary {
LIBRARY.get_or_init(|| {
let start_time = chrono::Utc::now();
let lib = library_loader::load_library();
let end_time = chrono::Utc::now();
println!("Built library in {} ms", (end_time - start_time).num_milliseconds());
lib
})
fn get_library<'a>() -> Arc<DynamicLibrary> {
LIBRARY
.get_or_init(|| {
let start_time = chrono::Utc::now();
let lib = library_loader::load_library();
let end_time = chrono::Utc::now();
println!("Built library in {} ms", (end_time - start_time).num_milliseconds());
Arc::new(lib)
})
.clone()
}
#[test]
@@ -73,7 +75,7 @@ fn validate_script_2() {
.unwrap()
.unwrap();
let user = Arc::new(
PokemonBuilder::new(&lib, "charizard".into(), 100)
PokemonBuilder::new(lib, "charizard".into(), 100)
.learn_move("fire_blast".into())
.build(),
);