use super::*;
pub trait ComponentTrait: std::fmt::Debug + Sized {
fn get_type() -> ComponentType;
fn from_entity(handle: Entity) -> Self;
fn try_from_entity(handle: Entity) -> Option<Self>;
fn entity(&self) -> Entity;
}
#[macro_export]
macro_rules! impl_world_component {
($component_type: ident) => {
impl ComponentTrait for $component_type {
#[inline]
fn get_type() -> ComponentType {
ComponentType::$component_type
}
#[inline]
fn from_entity(handle: Entity) -> Self {
debug_assert!(World::has_component(handle, ComponentType::$component_type), "{:?} lacks component {}", handle, stringify!($component_type));
$component_type { id: handle }
}
#[inline]
fn try_from_entity(handle: Entity) -> Option<Self> {
World::has_component(handle, ComponentType::$component_type)
.then(|| $component_type { id: handle })
}
#[inline]
fn entity(&self) -> Entity {
self.id
}
}
};
}