use hashbrown::HashMap; use crate::static_data::DataLibrary; use crate::static_data::Species; use crate::StringKey; #[derive(Debug)] pub struct SpeciesLibrary { map: HashMap>, list: Vec, } impl SpeciesLibrary { pub fn new(capacity: usize) -> SpeciesLibrary { SpeciesLibrary { map: HashMap::with_capacity(capacity), list: Vec::with_capacity(capacity), } } } impl<'a> DataLibrary<'a, Box> for SpeciesLibrary { fn map(&self) -> &HashMap> { &self.map } fn list_values(&self) -> &Vec { &self.list } fn get_modify(&mut self) -> (&mut HashMap>, &mut Vec) { (&mut self.map, &mut self.list) } } #[cfg(test)] pub mod tests { use hashbrown::HashSet; use crate::static_data::libraries::data_library::DataLibrary; use crate::static_data::libraries::species_library::SpeciesLibrary; use crate::static_data::Form; use crate::static_data::LearnableMoves; use crate::static_data::Species; use crate::static_data::StaticStatisticSet; fn build_species() -> Species { Species::new( 0, &"foo".into(), 0.5, &"test_growthrate".into(), 0, Form::new( &"default".into(), 0.0, 0.0, 0, Vec::new(), StaticStatisticSet::default(), Vec::new(), Vec::new(), LearnableMoves::new(), HashSet::new(), ), HashSet::new(), ) } pub fn build() -> SpeciesLibrary { let mut lib = SpeciesLibrary::new(1); let species = build_species(); // Borrow as mut so we can insert let w = &mut lib; w.add(&"foo".into(), Box::from(species)); // Drops borrow as mut lib } #[test] fn add_species_to_library_and_fetch() { let lib = build(); // Borrow as read so we can read let r = &lib; 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".into()); assert_eq!(r.len(), 1); } #[test] fn add_species_to_library_then_remove() { let mut lib = build(); lib.remove(&"foo".into()); // Borrow as read so we can read let r = &lib; let mon = r.get(&"foo".into()); assert!(mon.is_none()); assert_eq!(r.len(), 0); } }