Remove lifetime mess, replace a lot of code with Arc instead of borrows.
Some checks failed
continuous-integration/drone/push Build is failing
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:
@@ -1,3 +1,5 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use indexmap::IndexMap;
|
||||
|
||||
use crate::static_data::Ability;
|
||||
@@ -8,7 +10,7 @@ use crate::StringKey;
|
||||
#[derive(Debug)]
|
||||
pub struct AbilityLibrary {
|
||||
/// The underlying map for the library.
|
||||
map: IndexMap<StringKey, Box<Ability>>,
|
||||
map: IndexMap<StringKey, Arc<Ability>>,
|
||||
}
|
||||
|
||||
impl AbilityLibrary {
|
||||
@@ -20,11 +22,11 @@ impl AbilityLibrary {
|
||||
}
|
||||
}
|
||||
|
||||
impl DataLibrary<'_, Box<Ability>> for AbilityLibrary {
|
||||
fn map(&self) -> &IndexMap<StringKey, Box<Ability>> {
|
||||
impl DataLibrary<'_, Ability> for AbilityLibrary {
|
||||
fn map(&self) -> &IndexMap<StringKey, Arc<Ability>> {
|
||||
&self.map
|
||||
}
|
||||
fn get_modify(&mut self) -> &mut IndexMap<StringKey, Box<Ability>> {
|
||||
fn get_modify(&mut self) -> &mut IndexMap<StringKey, Arc<Ability>> {
|
||||
&mut self.map
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use indexmap::IndexMap;
|
||||
|
||||
use crate::Random;
|
||||
@@ -7,13 +9,13 @@ use crate::StringKey;
|
||||
/// by both key, while keeping their insertion order.
|
||||
pub trait DataLibrary<'a, T: 'a> {
|
||||
/// Returns the underlying map.
|
||||
fn map(&self) -> &IndexMap<StringKey, T>;
|
||||
fn map(&self) -> &IndexMap<StringKey, Arc<T>>;
|
||||
/// Returns the underlying map in mutable manner.
|
||||
fn get_modify(&mut self) -> &mut IndexMap<StringKey, T>;
|
||||
fn get_modify(&mut self) -> &mut IndexMap<StringKey, Arc<T>>;
|
||||
|
||||
/// Adds a new value to the library.
|
||||
fn add(&mut self, key: &StringKey, value: T) {
|
||||
self.get_modify().insert(key.clone(), value);
|
||||
self.get_modify().insert(key.clone(), Arc::new(value));
|
||||
}
|
||||
|
||||
/// Removes a value from the library.
|
||||
@@ -22,20 +24,15 @@ pub trait DataLibrary<'a, T: 'a> {
|
||||
}
|
||||
|
||||
/// Gets a value from the library.
|
||||
fn get(&'a self, key: &StringKey) -> Option<&'a T> {
|
||||
fn get(&'a self, key: &StringKey) -> Option<&Arc<T>> {
|
||||
self.map().get::<StringKey>(key)
|
||||
}
|
||||
|
||||
/// Gets a value from the library.
|
||||
fn get_by_hash(&'a self, key: u32) -> Option<&'a T> {
|
||||
fn get_by_hash(&'a self, key: u32) -> Option<&Arc<T>> {
|
||||
self.map().get::<u32>(&key)
|
||||
}
|
||||
|
||||
/// Gets a mutable value from the library.
|
||||
fn get_mut(&mut self, key: &StringKey) -> Option<&mut T> {
|
||||
self.get_modify().get_mut(key)
|
||||
}
|
||||
|
||||
/// Gets the amount of values in the library.
|
||||
fn len(&self) -> usize {
|
||||
self.map().len()
|
||||
@@ -46,7 +43,7 @@ pub trait DataLibrary<'a, T: 'a> {
|
||||
}
|
||||
|
||||
/// Gets a random value from the library.
|
||||
fn random_value(&self, rand: &mut Random) -> &T {
|
||||
fn random_value(&self, rand: &mut Random) -> &Arc<T> {
|
||||
let i = rand.get_between(0, self.len() as i32);
|
||||
return self.map().get_index(i as usize).unwrap().1;
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use indexmap::IndexMap;
|
||||
|
||||
use crate::static_data::DataLibrary;
|
||||
@@ -9,7 +11,7 @@ use crate::StringKey;
|
||||
#[cfg_attr(feature = "wasm", derive(unique_type_id_derive::UniqueTypeId))]
|
||||
pub struct ItemLibrary {
|
||||
/// The underlying data structure.
|
||||
map: IndexMap<StringKey, Box<Item>>,
|
||||
map: IndexMap<StringKey, Arc<Item>>,
|
||||
}
|
||||
|
||||
impl ItemLibrary {
|
||||
@@ -21,12 +23,12 @@ impl ItemLibrary {
|
||||
}
|
||||
}
|
||||
|
||||
impl DataLibrary<'_, Box<Item>> for ItemLibrary {
|
||||
fn map(&self) -> &IndexMap<StringKey, Box<Item>> {
|
||||
impl DataLibrary<'_, Item> for ItemLibrary {
|
||||
fn map(&self) -> &IndexMap<StringKey, Arc<Item>> {
|
||||
&self.map
|
||||
}
|
||||
|
||||
fn get_modify(&mut self) -> &mut IndexMap<StringKey, Box<Item>> {
|
||||
fn get_modify(&mut self) -> &mut IndexMap<StringKey, Arc<Item>> {
|
||||
&mut self.map
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use indexmap::IndexMap;
|
||||
|
||||
use crate::static_data::DataLibrary;
|
||||
@@ -9,7 +11,7 @@ use crate::StringKey;
|
||||
#[cfg_attr(feature = "wasm", derive(unique_type_id_derive::UniqueTypeId))]
|
||||
pub struct MoveLibrary {
|
||||
/// The underlying map.
|
||||
map: IndexMap<StringKey, MoveData>,
|
||||
map: IndexMap<StringKey, Arc<MoveData>>,
|
||||
}
|
||||
|
||||
impl MoveLibrary {
|
||||
@@ -22,10 +24,10 @@ impl MoveLibrary {
|
||||
}
|
||||
|
||||
impl DataLibrary<'_, MoveData> for MoveLibrary {
|
||||
fn map(&self) -> &IndexMap<StringKey, MoveData> {
|
||||
fn map(&self) -> &IndexMap<StringKey, Arc<MoveData>> {
|
||||
&self.map
|
||||
}
|
||||
fn get_modify(&mut self) -> &mut IndexMap<StringKey, MoveData> {
|
||||
fn get_modify(&mut self) -> &mut IndexMap<StringKey, Arc<MoveData>> {
|
||||
&mut self.map
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use indexmap::IndexMap;
|
||||
|
||||
use crate::static_data::DataLibrary;
|
||||
@@ -9,7 +11,7 @@ use crate::StringKey;
|
||||
#[cfg_attr(feature = "wasm", derive(unique_type_id_derive::UniqueTypeId))]
|
||||
pub struct SpeciesLibrary {
|
||||
/// The underlying map.
|
||||
map: IndexMap<StringKey, Box<Species>>,
|
||||
map: IndexMap<StringKey, Arc<Species>>,
|
||||
}
|
||||
|
||||
impl SpeciesLibrary {
|
||||
@@ -21,11 +23,11 @@ impl SpeciesLibrary {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> DataLibrary<'a, Box<Species>> for SpeciesLibrary {
|
||||
fn map(&self) -> &IndexMap<StringKey, Box<Species>> {
|
||||
impl<'a> DataLibrary<'a, Species> for SpeciesLibrary {
|
||||
fn map(&self) -> &IndexMap<StringKey, Arc<Species>> {
|
||||
&self.map
|
||||
}
|
||||
fn get_modify(&mut self) -> &mut IndexMap<StringKey, Box<Species>> {
|
||||
fn get_modify(&mut self) -> &mut IndexMap<StringKey, Arc<Species>> {
|
||||
&mut self.map
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use hashbrown::HashMap;
|
||||
|
||||
use crate::static_data::Statistic;
|
||||
@@ -60,7 +62,7 @@ impl Nature {
|
||||
#[derive(Debug)]
|
||||
pub struct NatureLibrary {
|
||||
/// The underlying data structure.
|
||||
map: HashMap<StringKey, Nature>,
|
||||
map: HashMap<StringKey, Arc<Nature>>,
|
||||
}
|
||||
|
||||
impl NatureLibrary {
|
||||
@@ -73,20 +75,20 @@ impl NatureLibrary {
|
||||
|
||||
/// Adds a new nature with name to the library.
|
||||
pub fn load_nature(&mut self, name: StringKey, nature: Nature) {
|
||||
self.map.insert(name, nature);
|
||||
self.map.insert(name, Arc::new(nature));
|
||||
}
|
||||
|
||||
/// Gets a nature by name.
|
||||
pub fn get_nature(&self, key: &StringKey) -> Option<&Nature> {
|
||||
pub fn get_nature(&self, key: &StringKey) -> Option<&Arc<Nature>> {
|
||||
self.map.get(key)
|
||||
}
|
||||
|
||||
/// Finds a nature name by nature.
|
||||
pub fn get_nature_name(&self, nature: &Nature) -> StringKey {
|
||||
pub fn get_nature_name(&self, nature: &Arc<Nature>) -> StringKey {
|
||||
for kv in &self.map {
|
||||
// As natures can't be copied, and should always be the same reference as the value
|
||||
// in the map, we just compare by reference.
|
||||
if std::ptr::eq(kv.1, nature) {
|
||||
if Arc::ptr_eq(kv.1, nature) {
|
||||
return kv.0.clone();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use hashbrown::{HashMap, HashSet};
|
||||
|
||||
use crate::static_data::Form;
|
||||
@@ -21,7 +23,7 @@ pub struct Species {
|
||||
/// uncatchable.
|
||||
capture_rate: u8,
|
||||
/// The forms that belong to this Pokemon.
|
||||
forms: HashMap<StringKey, Form>,
|
||||
forms: HashMap<StringKey, Arc<Form>>,
|
||||
/// The arbitrary flags that can be set on a Pokemon for script use.
|
||||
flags: HashSet<StringKey>,
|
||||
}
|
||||
@@ -46,7 +48,7 @@ impl Species {
|
||||
flags: HashSet<StringKey>,
|
||||
) -> Species {
|
||||
let mut forms = HashMap::with_capacity(1);
|
||||
forms.insert_unique_unchecked(get_default_key(), default_form);
|
||||
forms.insert_unique_unchecked(get_default_key(), Arc::new(default_form));
|
||||
Species {
|
||||
id,
|
||||
name: name.clone(),
|
||||
@@ -80,7 +82,7 @@ impl Species {
|
||||
self.capture_rate
|
||||
}
|
||||
/// The forms that belong to this Pokemon.
|
||||
pub fn forms(&self) -> &HashMap<StringKey, Form> {
|
||||
pub fn forms(&self) -> &HashMap<StringKey, Arc<Form>> {
|
||||
&self.forms
|
||||
}
|
||||
/// The arbitrary flags that can be set on a Pokemon for script use.
|
||||
@@ -90,16 +92,16 @@ impl Species {
|
||||
|
||||
/// Adds a new form to the species.
|
||||
pub fn add_form(&mut self, id: StringKey, form: Form) {
|
||||
self.forms.insert(id, form);
|
||||
self.forms.insert(id, Arc::new(form));
|
||||
}
|
||||
|
||||
/// Gets a form by name.
|
||||
pub fn get_form(&self, id: &StringKey) -> Option<&Form> {
|
||||
pub fn get_form(&self, id: &StringKey) -> Option<&Arc<Form>> {
|
||||
self.forms.get(id)
|
||||
}
|
||||
|
||||
/// Gets the form the Pokemon will have by default, if no other form is specified.
|
||||
pub fn get_default_form(&self) -> &Form {
|
||||
pub fn get_default_form(&self) -> &Arc<Form> {
|
||||
self.forms.get(&get_default_key()).unwrap()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user