pkecs 9.0.0

Another ECS implementation.
Documentation
pub mod into;

use std::marker::PhantomData;
use crate::ecs::world::UnsafeWorldCell;
use super::param::SystemParameter;

/// Defines behavior by enacting mutation upon entities.
///
/// Systems contain no state on their own and can often be represented by a function.
pub trait SystemHandler {
    /// Executes the system.
    fn run(&self, cell: &UnsafeWorldCell);
}

/// A function which can be executed as a [`System`].
pub struct SystemFunction<F, P> {
    pub func: F,
    _input: PhantomData<P>,
}

impl<F, P> SystemFunction<F, P> {
    /// Initializes [`Self`] from a function.
    pub const fn from_func(func: F) -> Self {
        let _input = PhantomData;

        Self { func, _input }
    }
}

// TODO: Generate all trait implementations with a macro!

impl<F, P1> SystemHandler for SystemFunction<F, P1>
    where
        F: Fn(P1),
        P1: SystemParameter,
{
    fn run(&self, cell: &UnsafeWorldCell) {
        let p1 = P1::fetch(cell);

        (self.func)(p1);
    }
}

impl<F, P1, P2> SystemHandler for SystemFunction<F, (P1, P2)>
    where
        F: Fn(P1, P2),
        P1: SystemParameter,
        P2: SystemParameter,
{
    fn run(&self, cell: &UnsafeWorldCell) {
        let p1 = P1::fetch(cell);
        let p2 = P2::fetch(cell);

        (self.func)(p1, p2);
    }
}

impl<F, P1, P2, P3> SystemHandler for SystemFunction<F, (P1, P2, P3)>
    where
        F: Fn(P1, P2, P3),
        P1: SystemParameter,
        P2: SystemParameter,
        P3: SystemParameter,
{
    fn run(&self, cell: &UnsafeWorldCell) {
        let p1 = P1::fetch(cell);
        let p2 = P2::fetch(cell);
        let p3 = P3::fetch(cell);

        (self.func)(p1, p2, p3);
    }
}

impl<F, P1, P2, P3, P4> SystemHandler for SystemFunction<F, (P1, P2, P3, P4)>
    where
        F: Fn(P1, P2, P3, P4),
        P1: SystemParameter,
        P2: SystemParameter,
        P3: SystemParameter,
        P4: SystemParameter,
{
    fn run(&self, cell: &UnsafeWorldCell) {
        let p1 = P1::fetch(cell);
        let p2 = P2::fetch(cell);
        let p3 = P3::fetch(cell);
        let p4 = P4::fetch(cell);

        (self.func)(p1, p2, p3, p4);
    }
}

impl<F, P1, P2, P3, P4, P5> SystemHandler for SystemFunction<F, (P1, P2, P3, P4, P5)>
    where
        F: Fn(P1, P2, P3, P4, P5),
        P1: SystemParameter,
        P2: SystemParameter,
        P3: SystemParameter,
        P4: SystemParameter,
        P5: SystemParameter,
{
    fn run(&self, cell: &UnsafeWorldCell) {
        let p1 = P1::fetch(cell);
        let p2 = P2::fetch(cell);
        let p3 = P3::fetch(cell);
        let p4 = P4::fetch(cell);
        let p5 = P5::fetch(cell);

        (self.func)(p1, p2, p3, p4, p5);
    }
}

impl<F, P1, P2, P3, P4, P5, P6> SystemHandler for SystemFunction<F, (P1, P2, P3, P4, P5, P6)>
    where
        F: Fn(P1, P2, P3, P4, P5, P6),
        P1: SystemParameter,
        P2: SystemParameter,
        P3: SystemParameter,
        P4: SystemParameter,
        P5: SystemParameter,
        P6: SystemParameter,
{
    fn run(&self, cell: &UnsafeWorldCell) {
        let p1 = P1::fetch(cell);
        let p2 = P2::fetch(cell);
        let p3 = P3::fetch(cell);
        let p4 = P4::fetch(cell);
        let p5 = P5::fetch(cell);
        let p6 = P6::fetch(cell);

        (self.func)(p1, p2, p3, p4, p5, p6);
    }
}

impl<F, P1, P2, P3, P4, P5, P6, P7> SystemHandler for SystemFunction<F, (P1, P2, P3, P4, P5, P6, P7)>
    where
        F: Fn(P1, P2, P3, P4, P5, P6, P7),
        P1: SystemParameter,
        P2: SystemParameter,
        P3: SystemParameter,
        P4: SystemParameter,
        P5: SystemParameter,
        P6: SystemParameter,
        P7: SystemParameter,
{
    fn run(&self, cell: &UnsafeWorldCell) {
        let p1 = P1::fetch(cell);
        let p2 = P2::fetch(cell);
        let p3 = P3::fetch(cell);
        let p4 = P4::fetch(cell);
        let p5 = P5::fetch(cell);
        let p6 = P6::fetch(cell);
        let p7 = P7::fetch(cell);

        (self.func)(p1, p2, p3, p4, p5, p6, p7);
    }
}

impl<F, P1, P2, P3, P4, P5, P6, P7, P8> SystemHandler for SystemFunction<F, (P1, P2, P3, P4, P5, P6, P7, P8)>
    where
        F: Fn(P1, P2, P3, P4, P5, P6, P7, P8),
        P1: SystemParameter,
        P2: SystemParameter,
        P3: SystemParameter,
        P4: SystemParameter,
        P5: SystemParameter,
        P6: SystemParameter,
        P7: SystemParameter,
        P8: SystemParameter,
{
    fn run(&self, cell: &UnsafeWorldCell) {
        let p1 = P1::fetch(cell);
        let p2 = P2::fetch(cell);
        let p3 = P3::fetch(cell);
        let p4 = P4::fetch(cell);
        let p5 = P5::fetch(cell);
        let p6 = P6::fetch(cell);
        let p7 = P7::fetch(cell);
        let p8 = P8::fetch(cell);

        (self.func)(p1, p2, p3, p4, p5, p6, p7, p8);
    }
}