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, 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()
}