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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user