use crate::app_interface::{DataLibrary, Species}; use crate::{ExternRef, ExternalReferenceType, StringKey}; use alloc::rc::Rc; use spin::RwLock; struct SpeciesLibraryInner { ptr: ExternRef, cache: RwLock>, } #[derive(Clone)] pub struct SpeciesLibrary { inner: Rc, } impl SpeciesLibrary { #[cfg(not(feature = "mock_data"))] pub fn new(ptr: ExternRef) -> Self { Self { inner: Rc::new(SpeciesLibraryInner { ptr, cache: Default::default(), }), } } #[cfg(feature = "mock_data")] pub fn mock() -> Self { Self { inner: Rc::new(SpeciesLibraryInner { ptr: ExternRef::mock(), cache: Default::default(), }), } } } impl DataLibrary for SpeciesLibrary { fn get_cache(&self) -> &spin::rwlock::RwLock> { &self.inner.cache } fn get_self_ref(&self) -> ExternRef { self.inner.ptr } #[cfg(not(feature = "mock_data"))] fn _get_ref_by_name(ptr: ExternRef, name: ExternRef) -> ExternRef { unsafe { species_library_get_species(ptr, name) } } #[cfg(not(feature = "mock_data"))] fn _get_ref_by_hash(ptr: ExternRef, hash: u32) -> ExternRef { unsafe { species_library_get_species_by_hash(ptr, hash) } } } crate::handling::cacheable::cacheable!(SpeciesLibrary); #[cfg(not(feature = "mock_data"))] impl ExternalReferenceType for SpeciesLibrary { fn from_extern_value(reference: ExternRef) -> Self { Self::new(reference) } } #[cfg(not(feature = "mock_data"))] extern "wasm" { fn species_library_get_species( ptr: ExternRef, name: ExternRef, ) -> ExternRef; fn species_library_get_species_by_hash( ptr: ExternRef, hash: u32, ) -> ExternRef; }