use std::any::Any;
use std::marker::PhantomData;
use crate::logical::registry::Registry;
use crate::raw::{RegistryEntry as RawRegistryEntry};
use crate::raw::RegistryId;
use crate::{RegistrationId};
#[derive(Copy, Clone)]
pub struct RegistryEntry<R: Registry + ?Sized> {
raw_entry: &'static RawRegistryEntry,
registry: PhantomData<fn(R)>
}
impl<R: Registry + ?Sized> RegistryEntry<R> {
pub(crate) fn new(raw_entry: &'static RawRegistryEntry) -> Self {
assert_eq!(
raw_entry.registry_id(),
RegistryId::of::<R>(),
"registry mismatch"
);
unsafe { Self::new_unchecked(raw_entry) }
}
pub(crate) unsafe fn new_unchecked(raw_entry: &'static RawRegistryEntry) -> Self {
RegistryEntry {
raw_entry,
registry: PhantomData
}
}
pub(crate) fn raw(&self) -> &'static RawRegistryEntry {
self.raw_entry
}
pub fn registry_id(&self) -> RegistryId {
self.raw_entry.registry_id()
}
pub fn registration_id(&self) -> RegistrationId<R> {
unsafe {
RegistrationId::from_raw_entry_unchecked(self.raw_entry)
}
}
pub fn type_info(&self) -> &'static R::TypeInfo {
let any = self.raw_entry.type_info().as_any() as &dyn Any;
any.downcast_ref().expect("protected by generics")
}
}