Skip to main content

System

Trait System 

Source
pub trait System {
    type Filter;
    type Views<'a>: Views<'a>;
    type ResourceViews<'a>;
    type EntryViews<'a>: Views<'a>;

    // Required method
    fn run<'a, R, S, I, E>(
        &mut self,
        query_result: Result<'a, R, S, I, Self::ResourceViews<'a>, Self::EntryViews<'a>, E>,
    )
       where R: ContainsViews<'a, Self::EntryViews<'a>, E>,
             I: Iterator<Item = Self::Views<'a>>;
}
Expand description

An executable type which operates over the entities within a World.

Systems can be passed to a World to be executed. When executed, the query specified by the Filter and Views associated types is performed and the result is passed to the run method.

It is advised to define a new struct for each System you wish to write. System structs can contain internal state, which can be used after running the system to execute post-processing logic.

§Example

use brood::{
    query::{
        filter,
        filter::Filter,
        result,
        Result,
        Views,
    },
    registry,
    system::System,
};

// Define components.
struct Foo(usize);
struct Bar(bool);

// Define system to operate on those components.
struct MySystem;

impl System for MySystem {
    type Views<'a> = Views!(&'a mut Foo, &'a Bar);
    type Filter = filter::None;
    type ResourceViews<'a> = Views!();
    type EntryViews<'a> = Views!();

    fn run<'a, R, S, I, E>(
        &mut self,
        query_results: Result<R, S, I, Self::ResourceViews<'a>, Self::EntryViews<'a>, E>,
    ) where
        R: registry::Registry,
        I: Iterator<Item = Self::Views<'a>>,
    {
        for result!(foo, bar) in query_results.iter {
            if bar.0 {
                foo.0 += 1;
            }
        }
    }
}

Required Associated Types§

Source

type Filter

The filter to apply to queries run by this system.

Source

type Views<'a>: Views<'a>

The views on components this system should operate on.

Source

type ResourceViews<'a>

Views on resources.

The system will have access to the resources requested here when run.

Source

type EntryViews<'a>: Views<'a>

Entry views.

These views specify which components are accessible in entry lookups.

The views here must be Disjoint with Self::Views

Required Methods§

Source

fn run<'a, R, S, I, E>( &mut self, query_result: Result<'a, R, S, I, Self::ResourceViews<'a>, Self::EntryViews<'a>, E>, )
where R: ContainsViews<'a, Self::EntryViews<'a>, E>, I: Iterator<Item = Self::Views<'a>>,

Logic to be run over the query result.

Any action performed using the query result should be performed here. If any modifications to the World itself are desired based on the query result, they should be performed after running the system.

§Example
use brood::{
    query::{
        filter,
        filter::Filter,
        result,
        Result,
        Views,
    },
    registry,
    system::System,
};

// Define components.
struct Foo(usize);
struct Bar(bool);

// Define system to operate on those components.
struct MySystem;

impl System for MySystem {
    type Views<'a> = Views!(&'a mut Foo, &'a Bar);
    type Filter = filter::None;
    type ResourceViews<'a> = Views!();
    type EntryViews<'a> = Views!();

    fn run<'a, R, S, I, E>(
        &mut self,
        query_results: Result<R, S, I, Self::ResourceViews<'a>, Self::EntryViews<'a>, E>,
    ) where
        R: registry::Registry,
        I: Iterator<Item = Self::Views<'a>>,
    {
        for result!(foo, bar) in query_results.iter {
            if bar.0 {
                foo.0 += 1;
            }
        }
    }
}

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§