Skip to main content

GridModel

Trait GridModel 

Source
pub trait GridModel:
    Send
    + Sync
    + 'static {
    type Params: Send + Sync;

    const NAME: &'static str;
    const ID: &'static str;
    const DESCRIPTION: &'static str;
    const PALETTE: &'static [[u8; 4]];
    const NEIGHBORHOOD: NeighborhoodKind;
    const STATS: &'static [StatDescriptor];
    const ACTIONS: &'static [ActionDescriptor] = _;

    // Required methods
    fn param_descriptors() -> Vec<ParamDescriptor>;
    fn from_params(params: &[ParamValue]) -> Self::Params;
    fn init(grid: &mut Grid2D<u8>, params: &[ParamValue], rng: &mut u64);
    fn step_cell(
        cell: u8,
        neighbors: &[u8],
        params: &Self::Params,
        rng: &mut u64,
    ) -> u8;
    fn stats(grid: &Grid2D<u8>) -> Vec<StatValue>;

    // Provided method
    fn act(
        _action: usize,
        _grid: &mut Grid2D<u8>,
        _params: &[ParamValue],
        _rng: &mut u64,
    ) { ... }
}
Expand description

A cellular automaton over u8 cells.

The engine owns grid allocation, double buffering, chunking, tick counting, the views, and the whole SimState impl.

Required Associated Constants§

Source

const NAME: &'static str

Name shown in the UI.

Source

const ID: &'static str

Stable id that identifies the model in a model set, on the command line and in a spec file.

Source

const DESCRIPTION: &'static str

One-line description shown in the UI.

Source

const PALETTE: &'static [[u8; 4]]

Cell colours, indexed by cell value.

Source

const NEIGHBORHOOD: NeighborhoodKind

Neighbourhood whose cells Self::step_cell receives.

Source

const STATS: &'static [StatDescriptor]

Stat series for the history chart. Declared once, so stats returns bare values.

Provided Associated Constants§

Source

const ACTIONS: &'static [ActionDescriptor] = _

One-off steps the user can trigger. Each gets a button in the Parameters panel.

Required Associated Types§

Source

type Params: Send + Sync

Hot parameters, extracted once per tick to keep enum matching out of the inner loop.

Required Methods§

Source

fn param_descriptors() -> Vec<ParamDescriptor>

Model parameters. The engine prepends grid width and height, but never shows them here.

Source

fn from_params(params: &[ParamValue]) -> Self::Params

Extracts the hot parameters for one tick. params is this model’s own slice, so its indices are 0 based.

Source

fn init(grid: &mut Grid2D<u8>, params: &[ParamValue], rng: &mut u64)

Fills the grid for a new run.

params is this model’s own slice, as for Self::from_params.

Source

fn step_cell( cell: u8, neighbors: &[u8], params: &Self::Params, rng: &mut u64, ) -> u8

Returns the next value of cell, given its neighbours.

neighbors follows the order of MOORE_ROW_MAJOR or VON_NEUMANN, as Self::NEIGHBORHOOD picks. The function must be pure apart from rng, since the engine steps rows in parallel.

Source

fn stats(grid: &Grid2D<u8>) -> Vec<StatValue>

Current statistics, in Self::STATS order.

A step never calls it. The engine calls it when it publishes a snapshot or takes a sample.

Provided Methods§

Source

fn act( _action: usize, _grid: &mut Grid2D<u8>, _params: &[ParamValue], _rng: &mut u64, )

Runs Self::ACTIONS entry action over the current cells.

Receives the arguments of init. An action is a setup step that the user requests mid run. rng is a separate stream, so a press leaves the tick’s draws where they were.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§