Module entity

Module entity 

Source
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 EntityStore maintains all registered entities in the form of EntityRecords, EntityRecords track the count of the instances of the Entity (valid EntityId<Entity> values) and owns the PropertyStore<E>, which manages the entity’s properties.
events
EntityCreatedEvent and EntityPropertyChangeEvent types 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 Property is the value type for properties associated to an Entity.
property_list
A PropertyList<E> is just a tuple of distinct properties of the same Entity E. It is used in two distinct places: as an initialization list for a new entity, and as a query.
property_store
A PropertyStore implements the registry pattern for property value stores: A PropertyStore wraps a vector of PropertyValueStores, one for each concrete property type. The implementor of crate::entity::property::Property is the value type. Since there’s a 1-1 correspondence between property types and their value stores, we implement the index method for each property type to make property lookup fast. The PropertyStore stores a list of all properties in the form of boxed PropertyValueStore instances, 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 a PropertyValueStore for a property that is never used is negligible. There’s no need, then, for lazy initialization of the PropertyValueStores themselves.
query

Structs§

EntityId
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 alias pub type MyEntityId = EntityId<MyEntity>.
PopulationIterator
An iterator over the total population of EntityId<E>s at the time of iterator creation.

Enums§

PropertyIndexType

Traits§

Entity
All entities must implement this trait using the define_entity! macro.

Type Aliases§

BxEntity
HashValueType
The type used in the indexing infrastructure. This type alias is public, because it is used by any implementor of Property<E: Entity>.