A lot more work on a bunch of different parts of the system.

This commit is contained in:
2022-06-11 17:22:46 +02:00
parent 10e93949e4
commit 6e8f4dd4a5
35 changed files with 735 additions and 197 deletions

View File

@@ -1,11 +1,12 @@
use crate::static_data::libraries::data_library::DataLibrary;
use crate::static_data::species_data::species::Species;
use crate::StringKey;
use hashbrown::HashMap;
#[derive(Debug)]
pub struct SpeciesLibrary<'a> {
map: HashMap<String, Box<Species<'a>>>,
list: Vec<String>,
map: HashMap<StringKey, Box<Species<'a>>>,
list: Vec<StringKey>,
}
impl<'a> SpeciesLibrary<'a> {
@@ -18,15 +19,20 @@ impl<'a> SpeciesLibrary<'a> {
}
impl<'a> DataLibrary<'a, Box<Species<'a>>> for SpeciesLibrary<'a> {
fn map(&self) -> &HashMap<String, Box<Species<'a>>> {
fn map(&self) -> &HashMap<StringKey, Box<Species<'a>>> {
&self.map
}
fn list_values(&self) -> &Vec<String> {
fn list_values(&self) -> &Vec<StringKey> {
&self.list
}
fn get_modify(&mut self) -> (&mut HashMap<String, Box<Species<'a>>>, &mut Vec<String>) {
fn get_modify(
&mut self,
) -> (
&mut HashMap<StringKey, Box<Species<'a>>>,
&mut Vec<StringKey>,
) {
(&mut self.map, &mut self.list)
}
}
@@ -44,12 +50,12 @@ pub mod tests {
fn build_species<'a>() -> Species<'a> {
Species::new(
0,
"foo",
&"foo".into(),
0.5,
"test_growthrate",
&"test_growthrate".into(),
0,
Form::new(
"default",
&"default".into(),
0.0,
0.0,
0,
@@ -69,7 +75,7 @@ pub mod tests {
let species = build_species();
// Borrow as mut so we can insert
let w = &mut lib;
w.add("foo", Box::from(species));
w.add(&"foo".into(), Box::from(species));
// Drops borrow as mut
lib
@@ -81,10 +87,10 @@ pub mod tests {
// Borrow as read so we can read
let r = &lib;
let mon = r.get("foo");
let mon = r.get(&"foo".into());
assert!(mon.is_some());
assert_eq!(mon.unwrap().id(), 0_u16);
assert_eq!(mon.unwrap().as_ref().name(), "foo");
assert_eq!(mon.unwrap().as_ref().name(), &"foo".into());
assert_eq!(r.len(), 1);
}
@@ -92,11 +98,11 @@ pub mod tests {
fn add_species_to_library_then_remove() {
let mut lib = build();
lib.remove("foo");
lib.remove(&"foo".into());
// Borrow as read so we can read
let r = &lib;
let mon = r.get("foo");
let mon = r.get(&"foo".into());
assert!(mon.is_none());
assert_eq!(r.len(), 0);
}