Expand description
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:
// 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.
Re-exports§
pub use context_extension::ContextEntitiesExt;pub use entity_set::EntitySetIterator;pub use query::EntityPropertyTuple;
Modules§
- context_
extension - entity_
set - entity_
store - The
EntityStoremaintains all registered entities in the form ofEntityRecords,EntityRecords track the count of the instances of theEntity(validEntityId<Entity>values) and owns thePropertyStore<E>, which manages the entity’s properties. - events
EntityCreatedEventandEntityPropertyChangeEventtypes are emitted when an entity is created or an entity’s property value is changed.- multi_
property - Utilities for managing and querying multi-properties.
- property
- A
Propertyis the value type for properties associated to anEntity. - property_
list - A
PropertyList<E>is just a tuple of distinct properties of the sameEntityE. It is used in two distinct places: as an initialization list for a new entity, and as a query. - property_
store - A
PropertyStoreimplements the registry pattern for property value stores: APropertyStorewraps a vector ofPropertyValueStores, one for each concrete property type. The implementor ofcrate::entity::property::Propertyis the value type. Since there’s a 1-1 correspondence between property types and their value stores, we implement theindexmethod for each property type to make property lookup fast. ThePropertyStorestores a list of all properties in the form of boxedPropertyValueStoreinstances, which provide a type-erased interface to the backing storage (including index) of the property. Storage is only allocated as-needed, so the instantiation of aPropertyValueStorefor a property that is never used is negligible. There’s no need, then, for lazy initialization of thePropertyValueStores themselves. - query
Structs§
- Entity
Id - A type that can be named and used (copied, cloned) but not created outside of this crate.
In the
define_entity!macro we define the aliaspub type MyEntityId = EntityId<MyEntity>. - Population
Iterator - An iterator over the total population of
EntityId<E>s at the time of iterator creation.
Enums§
Traits§
- Entity
- All entities must implement this trait using the
define_entity!macro.
Type Aliases§
- BxEntity
- Hash
Value Type - The type used in the indexing infrastructure. This type alias is
public, because it is used by any implementor of
Property<E: Entity>.