use crate::app_interface::{DataLibrary, Item}; use crate::{ExternRef, ExternalReferenceType, StringKey}; use alloc::rc::Rc; use spin::rwlock::RwLock; struct ItemLibraryInner { ptr: ExternRef, cache: RwLock>, } #[derive(Clone)] pub struct ItemLibrary { inner: Rc, } impl ItemLibrary { #[cfg(not(feature = "mock_data"))] pub(crate) fn new(ptr: ExternRef) -> Self { Self { inner: Rc::new(ItemLibraryInner { ptr, cache: Default::default(), }), } } } impl DataLibrary for ItemLibrary { 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 { move_library_get_move(ptr, name) } } #[cfg(not(feature = "mock_data"))] fn _get_ref_by_hash(ptr: ExternRef, hash: u32) -> ExternRef { unsafe { move_library_get_move_by_hash(ptr, hash) } } } crate::handling::cacheable::cacheable!(ItemLibrary); #[cfg(not(feature = "mock_data"))] impl ExternalReferenceType for ItemLibrary { fn from_extern_value(reference: ExternRef) -> Self { Self::new(reference) } } #[cfg(not(feature = "mock_data"))] extern "wasm" { fn move_library_get_move( ptr: ExternRef, name: ExternRef, ) -> ExternRef; fn move_library_get_move_by_hash(ptr: ExternRef, hash: u32) -> ExternRef; } #[cfg(feature = "mock_data")] impl ItemLibrary { pub fn mock() -> Self { Self { inner: Rc::new(ItemLibraryInner { ptr: ExternRef::mock(), cache: Default::default(), }), } } }