use crate::app_interface::{DataLibrary, Item}; use alloc::rc::Rc; pub trait ItemLibraryTrait: DataLibrary {} pub type ItemLibrary = Rc; #[cfg(not(feature = "mock_data"))] mod implementation { use super::*; use crate::app_interface::{ItemImpl, StringKey}; use crate::handling::extern_ref::{ExternRef, ExternalReferenceType}; use spin::RwLock; struct ItemLibraryInner { ptr: ExternRef, cache: RwLock>, } #[derive(Clone)] pub struct ItemLibraryImpl { inner: Rc, } impl ItemLibraryImpl { pub(crate) fn new(ptr: ExternRef) -> Self { Self { inner: Rc::new(ItemLibraryInner { ptr, cache: Default::default(), }), } } } impl ItemLibraryTrait for ItemLibraryImpl {} impl DataLibrary for ItemLibraryImpl { fn get_cache(&self) -> &spin::rwlock::RwLock> { &self.inner.cache } fn get_self_ref(&self) -> ExternRef { self.inner.ptr } fn _get_ref_by_name(ptr: ExternRef, name: ExternRef) -> u32 { unsafe { item_library_get_item(ptr, name).get_internal_index() } } fn _get_ref_by_hash(ptr: ExternRef, hash: u32) -> u32 { unsafe { item_library_get_item_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!(ItemLibraryImpl); impl ExternalReferenceType for ItemLibraryImpl { fn from_extern_value(reference: ExternRef) -> Self { Self::new(reference) } } extern "wasm" { fn item_library_get_item( ptr: ExternRef, name: ExternRef, ) -> ExternRef; fn item_library_get_item_by_hash( ptr: ExternRef, hash: u32, ) -> ExternRef; } } #[cfg(not(feature = "mock_data"))] pub use implementation::*;