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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//! Components and their storage
//!
//! All components live within a storage, which may be
//! an enum, a boxed trait, etc..., anything that implements
//! the [`Storage`] trait.
//! All types that can live within a storage must implement
//! [`Component`] of that storage.
//!
//! # Example
//!
//! ```rust
//!
//! mecs::impl_enum_storage! {
//! enum Components {
//! A(i32),
//! B(&'static str),
//! C(f32),
//! }
//! }
//! ```
//!
//! # Manual Implementation
//!
//! ```rust
//! use std::any::Any;
//! use mecs::{Storage, Component};
//!
//! enum Components<'a> {
//! A(i32),
//! B(&'a str),
//! }
//!
//! impl<'a> Storage<'a> for Components<'a>
//! {
//! type Id = usize;
//!
//! fn id(&self) -> Self::Id {
//! match self {
//! Self::A(_) => <i32 as Component<Self>>::id(),
//! Self::B(_) => <&'a str as Component<Self>>::id(),
//! }
//! }
//! }
//!
//! impl<'a> Component<'a, Components<'a>> for i32 {
//! fn id() -> <Components<'a> as Storage<'a>>::Id { 0 }
//!
//! fn get<'b>(storage: &'b Components<'a>) -> Option<&'b Self> {
//! if let Components::A(num) = storage { Some( num ) } else { None }
//! }
//!
//! fn get_mut<'b>(storage: &'b mut Components<'a>) -> Option<&'b mut Self> {
//! if let Components::A(num) = storage { Some( num ) } else { None }
//! }
//! }
//!
//! impl<'a> Component<'a, Components<'a>> for &'a str {
//! fn id() -> <Components<'a> as Storage<'a>>::Id { 1 }
//!
//! fn get<'b>(storage: &'b Components<'a>) -> Option<&'b Self> {
//! if let Components::B(name) = storage { Some( name ) } else { None }
//! }
//!
//! fn get_mut<'b>(storage: &'b mut Components<'a>) -> Option<&'b mut Self> {
//! if let Components::B(name) = storage { Some( name ) } else { None }
//! }
//! }
//! ```
// Modules
// Exports
pub use DynStorage;
// Traits
//--------------------------------------------------------------------------------------------------
/// Storage for any number of components
/// Trait implemented by all types within a storage.
///
/// # Visitor
/// This trait uses the visitor pattern to provide getters for
/// the storage.
//--------------------------------------------------------------------------------------------------