async_ecs/component/mod.rs
1use std::any::Any;
2
3use crate::storage::Storage;
4
5/// Abstract component type.
6/// Doesn't have to be Copy or even Clone.
7///
8/// ## Storages
9///
10/// Components are stored in separated collections for maximum
11/// cache efficiency. The `Storage` associated type allows
12/// to specify which collection should be used.
13/// Depending on how many entities have this component and how
14/// often it is accessed, you will want different storages.
15///
16/// The most common ones are `VecStorage` (use if almost every entity has that
17/// component), `DenseVecStorage` (if you expect many entities to have the
18/// component) and `HashMapStorage` (for very rare components).
19///
20/// ## Examples
21///
22/// ```
23/// use async_ecs::*;
24///
25/// pub struct Position {
26/// pub x: f32,
27/// pub y: f32,
28/// }
29///
30/// impl Component for Position {
31/// type Storage = VecStorage<Self>;
32/// }
33/// ```
34///
35/// ```
36/// use async_ecs::*;
37///
38/// pub enum Light {
39/// // (Variants would have additional data)
40/// Directional,
41/// SpotLight,
42/// }
43///
44/// impl Component for Light {
45/// type Storage = DenseVecStorage<Self>;
46/// }
47/// ```
48///
49/// ```
50/// use async_ecs::*;
51///
52/// pub struct Camera {
53/// // In an ECS, the camera would not itself have a position;
54/// // you would just attach a `Position` component to the same
55/// // entity.
56/// matrix: [f32; 16],
57/// }
58///
59/// impl Component for Camera {
60/// type Storage = HashMapStorage<Self>;
61/// }
62/// ```
63pub trait Component: Any + Sized {
64 /// Associated storage type for this component.
65 type Storage: Storage<Self> + Any + Send + Sync;
66}