#[cfg(not(target_arch = "wasm32"))]
pub(crate) mod hot_reload;
pub mod loadable;
pub mod loader;
#[doc(hidden)]
pub mod source;
use std::rc::Rc;
use hashbrown::HashMap;
use smol_str::SmolStr;
use self::loadable::Loadable;
pub type Id = SmolStr;
pub(crate) struct AssetManager<T: Loadable> {
assets: HashMap<Id, Rc<T>>,
}
impl<T: Loadable> AssetManager<T> {
#[inline]
pub(crate) fn get(&self, id: &Id) -> Option<Rc<T>> {
self.assets.get(id).cloned()
}
#[inline]
pub(crate) fn contains(&self, id: &Id) -> bool {
self.assets.contains_key(id)
}
#[inline]
pub(crate) fn insert(&mut self, id: Id, asset: T) -> Rc<T> {
let asset = Rc::new(asset);
self.assets.insert(id, Rc::clone(&asset));
asset
}
#[cfg(not(target_arch = "wasm32"))]
#[inline]
pub(crate) fn remove(&mut self, id: &Id) {
self.assets.remove(id);
}
}
impl<T: Loadable> Default for AssetManager<T> {
fn default() -> Self {
let assets = HashMap::new();
Self { assets }
}
}
pub(crate) struct CustomAssetManager {
assets: HashMap<Id, Rc<dyn Loadable + 'static>>,
}
impl CustomAssetManager {
#[inline]
pub(crate) fn get<T>(&self, id: &str) -> Option<Rc<T>>
where
T: Loadable,
{
let dyn_asset = self.assets.get(id)?;
Rc::clone(dyn_asset)
.downcast_rc::<T>()
.map_or_else(|_| panic!("Could downcast asset with ID '{id}', loaded type is different from requested type"), Some)
}
#[inline]
pub(crate) fn insert<T>(&mut self, id: Id, asset: T) -> Rc<T>
where
T: Loadable,
{
let asset: Rc<dyn Loadable> = Rc::new(asset);
self.assets.insert(id, Rc::clone(&asset));
asset
.downcast_rc::<T>()
.unwrap_or_else(|_| panic!("Error downcasting type"))
}
#[cfg(not(target_arch = "wasm32"))]
#[inline]
pub(crate) fn remove(&mut self, id: &Id) {
self.assets.remove(id);
}
}
impl Default for CustomAssetManager {
fn default() -> Self {
let assets = HashMap::new();
Self { assets }
}
}