pub mod prelude;
use std::marker::PhantomData;
use swamp_slot_map::SlotMap;
pub trait Asset: 'static + Send + Sync {}
#[derive(Default)]
pub struct Assets<A: Asset> {
storage: SlotMap<A>,
}
#[derive(PartialEq, Eq, Hash, Clone, Copy)]
pub struct Id<A: Asset> {
slot_map_id: swamp_slot_map::Id,
_phantom_data: PhantomData<A>,
}
impl<A: Asset> Assets<A> {
#[must_use]
pub const fn new() -> Self {
Self {
storage: SlotMap::new(),
}
}
#[must_use]
pub fn insert(&mut self, asset: A) -> Id<A> {
Id {
slot_map_id: self.storage.insert(asset),
_phantom_data: PhantomData,
}
}
pub fn remove(&mut self, id: &Id<A>) {
self.storage.remove(&id.slot_map_id);
}
#[must_use]
pub fn get(&self, id: &Id<A>) -> Option<&A> {
self.storage.get(&id.slot_map_id)
}
#[must_use]
pub fn get_mut(&mut self, id: &Id<A>) -> Option<&mut A> {
self.storage.get_mut(&id.slot_map_id)
}
#[must_use]
pub fn contains(&self, id: &Id<A>) -> bool {
self.get(id).is_some()
}
pub fn iter(&self) -> impl Iterator<Item = (Id<A>, &A)> {
self.storage.iter().map(|(id, asset)| {
(
Id {
slot_map_id: id,
_phantom_data: Default::default(),
},
asset,
)
})
}
#[must_use]
pub fn len(&self) -> usize {
self.storage.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.storage.is_empty()
}
}