firespine/
comp.rs

1//! Components
2use crate::*;
3
4/// A component.
5pub trait NodeComponent: Downcast {
6    /// Checks if this component has the given type-id.
7    fn is_of_type(&self, ctype: TypeId) -> bool {
8        self.type_id() == ctype
9    }
10    
11    // /// Returns the type-id for this component.
12    // fn get_component_type_id(&self) -> TypeId {
13    //     self.type_id()
14    // }
15    
16    /// Returns a internal name for the component.
17    fn get_component_name(&self) -> &str;
18}
19
20// Automatic impl for sized components.
21impl<C: Sized + 'static> NodeComponent for C where C: Sized {
22    fn get_component_name(&self) -> &str {
23        std::any::type_name::<C>()
24    }
25}
26
27/// A component that is also Send/Sync.
28pub trait NodeComponentSync: NodeComponent + DowncastSync + Send + Sync {
29    //
30}
31
32// Automatic impl for Send/Sync components.
33impl<C: NodeComponent> NodeComponentSync for C where C: Send + Sync {}
34
35/// A box holding a NodeComponent instance.
36pub type NodeComponentBox = Box<dyn NodeComponent>;
37
38// Downcast-Impl.
39use downcast_rs::impl_downcast;
40impl_downcast!(NodeComponent);
41impl_downcast!(NodeComponentSync);