1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//! Contains types related to entity components.

use std::{
    any::TypeId,
    fmt::{Display, Formatter},
    hash::Hasher,
};

use super::{packed::PackedStorage, ComponentStorage};

/// A unique ID for a component type.
#[derive(Copy, Clone, Debug, Eq, PartialOrd, Ord)]
pub struct ComponentTypeId {
    pub(crate) type_id: TypeId,
    #[cfg(debug_assertions)]
    name: &'static str,
}

impl ComponentTypeId {
    /// Constructs the component type ID for the given component type.
    pub fn of<T: Component>() -> Self {
        Self {
            type_id: TypeId::of::<T>(),
            #[cfg(debug_assertions)]
            name: std::any::type_name::<T>(),
        }
    }

    /// Returns the internal TypeID of the component.
    pub fn type_id(&self) -> TypeId {
        self.type_id
    }
}

impl std::hash::Hash for ComponentTypeId {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.type_id.hash(state);
    }
}

impl PartialEq for ComponentTypeId {
    fn eq(&self, other: &Self) -> bool {
        self.type_id.eq(&other.type_id)
    }
}

impl Display for ComponentTypeId {
    #[cfg(debug_assertions)]
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.name)
    }

    #[cfg(not(debug_assertions))]
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}", self.type_id)
    }
}

/// A marker trait for all types which can be attached to an entity.
///
/// This trait has a blanket impl for all applicable types.
pub trait Component: 'static + Sized + Send + Sync {
    /// The storage type required to hold all instances of this component in a world.
    type Storage: for<'a> ComponentStorage<'a, Self>;
}

impl<T: 'static + Sized + Send + Sync> Component for T {
    type Storage = PackedStorage<T>;
}