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

@@ -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();
}
}