use std::fmt::Debug;
use std::any::{Any, TypeId};
use crate::{Storage, Component};
#[allow(clippy::module_name_repetitions)]
#[derive(Debug)]
pub struct DynStorage(TypeId, Box<dyn Any>);
impl DynStorage
{
pub fn new<T>(value: T) -> Self
where
T: Component<'static, Self> + 'static
{
Self(T::id(), Box::new(value))
}
#[must_use]
pub fn into_inner(self) -> (TypeId, Box<dyn Any>) {
(self.0, self.1)
}
}
impl Storage<'static> for DynStorage
{
type Id = TypeId;
#[must_use]
fn id(&self) -> Self::Id {
self.0
}
}
impl<T> Component<'static, DynStorage> for T
where
T: Debug + 'static
{
#[must_use]
fn id() -> TypeId {
TypeId::of::<Self>()
}
#[must_use]
fn get(storage: &DynStorage) -> Option<&Self> {
storage.1.downcast_ref()
}
#[must_use]
fn get_mut(storage: &mut DynStorage) -> Option<&mut Self> {
storage.1.downcast_mut()
}
}