Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Create a Struct

The first thing to define when creating a component is to create the structs that define the component.

All components should contain an Entity which is used to configure the logging and also to give a unique location within the model hierarchy. The Entity will be wrapped in std::rc::Rc so that it can be shared.

GWR components run on the engine’s single-threaded async executor, so shared component state is normally built from Rc, RefCell, and Cell rather than Arc and locks. This is intentional: it lets setup code, spawned tasks, and small helper objects share local mutable state without adding the overhead of thread synchronisation to the simulation model.

use std::marker::PhantomData;
use std::rc::Rc;
use gwr_engine::traits::SimObject;
use gwr_model_builder::{EntityGet, EntityDisplay};
use gwr_track::entity::Entity;

#[derive(EntityGet, EntityDisplay)]
struct MyComponent<T>
where
    T: SimObject
{
    entity: Rc<Entity>,

    // Any component-specific state
  phantom: PhantomData<T>
}
fn main() {}