gridsim/
lib.rs

1//! Gridsim is a library intended to run grid-based simulations.
2//!
3//! The new generics introduced in gridsim 0.2.0 make it possible to implement hex grids,
4//! rhombic dodecahedral honeycombs(in its multiple tight-pack layer patterns), square grids, cube grids,
5//! and even n-dimensional grids, but they are currently not yet implemented.
6
7#![feature(type_alias_impl_trait)]
8#![feature(generic_associated_types)]
9#![allow(incomplete_features)]
10
11mod neumann;
12mod square_grid;
13
14pub use neumann::*;
15pub use square_grid::*;
16
17pub trait Neighborhood {
18    type Neighbors<'a, T: 'a>;
19    type Edges<T>;
20}
21
22/// Defines a simulation for complicated things that have too much state to abandon on the next cycle.
23///
24/// This enforces a rule in that all new cells are only produced from old board state. This prevents the
25/// update order from breaking the simulation.
26pub trait Sim<N>
27where
28    N: Neighborhood,
29{
30    /// The cells of the grid
31    type Cell: 'static;
32    /// Result of the neighbor-observing computation
33    type Diff: 'static;
34    /// The data which flows to each neighbor.
35    type Flow;
36
37    /// At this stage, everything is immutable, and the diff can be computed that
38    /// describes what will change between simulation states.
39    fn compute(&self, cells: N::Neighbors<'_, Self::Cell>) -> Self::Diff;
40
41    /// At this stage, changes are made to the cell based on the diff and then
42    /// any owned state that needs to be moved to neighbors must be returned
43    /// as part of the flow.
44    fn egress(
45        &self,
46        cell: &mut Self::Cell,
47        diffs: N::Neighbors<'_, Self::Diff>,
48    ) -> N::Edges<Self::Flow>;
49
50    /// At this stage, the flow is received from all neighbors, allowing state
51    /// to be added to this cell.
52    fn ingress(&self, cell: &mut Self::Cell, flows: N::Edges<Self::Flow>);
53
54    /// The cell used as padding.
55    fn cell_padding(&self) -> Self::Cell;
56
57    /// The diff used as padding.
58    fn diff_padding(&self) -> Self::Diff;
59
60    /// The flow used as padding.
61    fn flow_padding(&self) -> Self::Flow;
62}