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
/*!
A "person" is represented by a `PersonId` and has multiple `PersonProperty` values
associated with it. Entities generalize this: An `Entity` is analogous to a table in
a relationship database, and the properties of an entity are analogous to the columns
that exist on the table. A row in the table is addressable with an `EntityId<Entity>`
(implemented as a newtype of a `usize`). In this new paradigm, `Person` is a particular
entity, possibly one of several, and `PersonId` is a type alias for `EntityId<Person>`.
Entity property getters and setters exists on `Context` like this:
```rust,ignore
// The `my_entity_id` value is of type `MyEntityId`, which is a type alias for `EntityId<MyEntity>`.
// (The `MyProperty` type knows which entity it belongs to.)
let my_property_value = context.get_property::<MyProperty>(my_entity_id);
// Turbofish-less version of the same call:
let my_property_value: MyProperty = context.get_property(my_entity_id);
// For setters, the property is inferred from the type of the value we are passing in.
context.set_property(my_entity_id, some_property_value);
// ...but if you want to be super-explicit, you could use a turbofish version:
context.set_property::<MyProperty>(my_entity_id, some_property_value);
```
This implementation of entities relies heavily on the "registry pattern" for efficient
lookup of entities and properties. The idea is that concrete types implementing `Entity` (and
separately, `Property<Entity>`) have a `ctor` that initializes a global (per concrete type)
static variable `index`. Each concrete `Entity` type is thus assigned a unique index ranging from
`0` to `ENTITY_COUNT - 1`. Then instances of container types like `EntityStore` (respectively
`PropertyStore`) use this index to look up the corresponding instances in a vector it owns.
*/
pub
pub
pub
pub
// Flatten the module hierarchy.
pub use ContextEntitiesExt;
pub use *;
pub use EntitySetIterator;
pub use PropertyIndexType;
pub use EntityPropertyTuple;
pub use QueryInternal;