Crate specs [] [src]

SPECS Parallel ECS

This library provides an ECS variant designed for parallel execution and convenient usage. It is highly flexible when it comes to actual component data and the way it is stored and accessed.


  • depending on chosen features either 0 virtual function calls or one per system
  • parallel iteration over components
  • parallel execution of systems

High-level overview

One could basically split this library up into two parts: The data part and the execution part.

The data

World is where component storages, resources and entities are stored. See the docs of World for more.

Components can be easily implemented like this:

use specs::{Component, VecStorage};

struct MyComp;

impl Component for MyComp {
    type Storage = VecStorage<MyComp>;

You can choose different storages according to your needs.

These storages can be joined together, for example joining a Velocity and a Position storage means you'll only get entities which have both of them. Thanks to rayon, this is even possible in parallel! See ParJoin for more.

System execution

Here we have System and Dispatcher as our core types. Both types are provided by a library called shred.

The Dispatcher can be seen as an optional part here; it allows dispatching the systems in parallel, given a list of systems and their dependencies on other systems.

If you don't like it, you can also execute the systems yourself by using RunNow.

Systems are traits with a run() method and an associated SystemData, allowing type-safe aspects (knowledge about the reads / writes of the systems).


This is a basic example of using Specs:

extern crate specs;

use specs::{DispatcherBuilder, Component, Join, ReadStorage, System, VecStorage, WriteStorage,

// A component contains data
// which is associated with an entity.
struct Vel(f32);
struct Pos(f32);

impl Component for Vel {
    type Storage = VecStorage<Vel>;

impl Component for Pos {
    type Storage = VecStorage<Pos>;

struct SysA;

impl<'a> System<'a> for SysA {
    // These are the resources required for execution.
    // You can also define a struct and `#[derive(SystemData)]`,
    // see the `full` example.
    type SystemData = (WriteStorage<'a, Pos>, ReadStorage<'a, Vel>);

    fn run(&mut self, data: Self::SystemData) {
        // The `.join()` combines multiple components,
        // so we only access those entities which have
        // both of them.

        let (mut pos, vel) = data;

        // This joins the component storages for Position
        // and Velocity together; it's also possible to do this
        // in parallel using rayon's `ParallelIterator`s.
        // See `ParJoin` for more.
        for (pos, vel) in (&mut pos, &vel).join() {
            pos.0 += vel.0;

fn main() {
    // The `World` is our
    // container for components
    // and other resources.

    let mut world = World::new();

    // An entity may or may not contain some component.


    // This entity does not have `Vel`, so it won't be dispatched.

    // This builds a dispatcher.
    // The third parameter of `add` specifies
    // logical dependencies on other systems.
    // Since we only have one, we don't depend on anything.
    // See the `full` example for dependencies.
    let mut dispatcher = DispatcherBuilder::new().add(SysA, "sys_a", &[]).build();

    // This dispatches all the systems in parallel (but blocking).
    dispatcher.dispatch(&mut world.res);

You can also easily create new entities on the fly:

use specs::{Entities, FetchMut, System, WriteStorage};

struct EnemySpawner;

impl<'a> System<'a> for EnemySpawner {
    type SystemData = Entities<'a>;

    fn run(&mut self, entities: Entities<'a>) {
        let enemy = entities.create();

See the repository's examples directory for more examples.



Like, Dispatcher but works asynchronously.


BTreeMap-based storage.


A storage type that iterates entities that have a particular component type, but does not return the component.


An iterator for entity creation. Please note that you have to consume it because iterators are lazy.


An iterator for entity creation. Please note that you have to consume it because iterators are lazy.


Dense vector storage. Has a redirection 2-way table between entities and components, allowing to leave no gaps within the data.


The dispatcher struct, allowing systems to be executed in parallel.


Builder for the Dispatcher.


The entities of this ECS. This is a resource, stored in the World. If you just want to access it in your system, you can also use the Entities type def.


Entity type, as seen by the user.


The entity builder, allowing to build an entity together with its components.


Return value of Resources::fetch.


Return value of Resources::fetch_id.


Return value of Resources::fetch_id_mut.


Return value of Resources::fetch_mut.


Wrapper storage that stores modifications to components in a bitset.


Index generation. When a new entity is placed at an old index, it bumps the Generation by 1. This allows to avoid using components from the entities that were deleted.


HashMap-based storage. Best suited for rare components.


JoinIter is an Iterator over a group of Storages.


JoinParIter is a ParallelIterator over a group of Storages.


A null storage type, used for cases where the component doesn't contain any data and instead works as a simple flag.


A wrapper around the masked storage and the generations vector. Can be used for safe lookup of components, insertions and removes. This is what World::read/write fetches for the user.


Vector storage. Uses a simple Vec. Supposed to have maximum performance for the components mostly present in entities.


The World struct contains the component storages and other resources.



The status of an insert()ion into a storage.




Abstract component type. Doesn't have to be Copy or even Clone.


This is a marker trait which requires you to uphold the following guarantee:


The purpose of the Join trait is to provide a way to access multiple storages at the same time with the merged bit set.


The purpose of the ParJoin trait is to provide a way to access multiple storages in parallel at the same time with the merged bit set.


Trait for fetching data and running systems. Automatically implemented for systems.


A System, executed with a set of required Resources.


A struct implementing system data indicates that it bundles some resources which are required for the execution.


Used by the framework to quickly join components.

Type Definitions


A wrapper for a fetched Entities resource. Note that this is just Fetch<Entities>, so you can easily use it in your system:


An index is basically the id of an Entity.


A storage with read access.


A storage with read and write access.