use crate::app_interface::{DataLibrary, Species}; use alloc::rc::Rc; pub trait SpeciesLibraryTrait: DataLibrary {} pub type SpeciesLibrary = Rc; #[cfg(not(feature = "mock_data"))] mod implementation { use crate::app_interface::species_library::SpeciesLibraryTrait; use crate::app_interface::{DataLibrary, Species, SpeciesImpl, StringKey}; use crate::handling::extern_ref::{ExternRef, ExternalReferenceType}; use alloc::rc::Rc; use spin::RwLock; struct SpeciesLibraryInner { ptr: ExternRef, cache: RwLock>, } #[derive(Clone)] pub struct SpeciesLibraryImpl { inner: Rc, } impl SpeciesLibraryImpl { pub fn new(ptr: ExternRef) -> Self { Self { inner: Rc::new(SpeciesLibraryInner { ptr, cache: Default::default(), }), } } } impl SpeciesLibraryTrait for SpeciesLibraryImpl {} impl DataLibrary for SpeciesLibraryImpl { 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) -> u32 { unsafe { species_library_get_species(ptr, name).get_internal_index() } } fn _get_ref_by_hash(ptr: ExternRef, hash: u32) -> u32 { unsafe { species_library_get_species_by_hash(ptr, hash).get_internal_index() } } fn _from_external_ref_index(index: u32) -> Option { let v = ExternRef::::from(index).get_value(); if let Some(v) = v { Some(Rc::new(v)) } else { None } } } crate::handling::cacheable::cacheable!(SpeciesLibraryImpl); impl ExternalReferenceType for SpeciesLibraryImpl { fn from_extern_value(reference: ExternRef) -> Self { Self::new(reference) } } extern "wasm" { fn species_library_get_species( ptr: ExternRef, name: ExternRef, ) -> ExternRef; fn species_library_get_species_by_hash( ptr: ExternRef, hash: u32, ) -> ExternRef; } } #[cfg(not(feature = "mock_data"))] pub use implementation::*;