use std::marker::PhantomData;
use crate::{
cell::{RefMut, TrustCell},
world::{FetchMut, Resource, ResourceId},
};
type StdEntry<'a, K, V> = std::collections::hash_map::Entry<'a, K, V>;
pub struct Entry<'a, T: 'a> {
inner: StdEntry<'a, ResourceId, TrustCell<Box<dyn Resource>>>,
marker: PhantomData<T>,
}
impl<'a, T> Entry<'a, T>
where
T: Resource + 'a,
{
pub fn or_insert(self, v: T) -> FetchMut<'a, T> {
self.or_insert_with(move || v)
}
pub fn or_insert_with<F>(self, f: F) -> FetchMut<'a, T>
where
F: FnOnce() -> T,
{
let value = self
.inner
.or_insert_with(move || TrustCell::new(Box::new(f())));
let inner = RefMut::map(value.borrow_mut(), Box::as_mut);
FetchMut {
inner,
phantom: PhantomData,
}
}
}
pub fn create_entry<T>(e: StdEntry<ResourceId, TrustCell<Box<dyn Resource>>>) -> Entry<T> {
Entry {
inner: e,
marker: PhantomData,
}
}
#[cfg(test)]
mod tests {
use crate::world::World;
#[test]
fn test_entry() {
struct Res;
let mut world = World::empty();
world.entry().or_insert(Res);
assert!(world.has_value::<Res>());
}
}