pub trait System: Send + 'static {
type Request: Request;
// Required method
fn run(&mut self, resp: Response<'_, Self::Request>);
// Provided methods
fn on_transition(&mut self, from: SystemState, to: SystemState) { ... }
fn name() -> &'static str { ... }
}Expand description
System is a type that accesses components, entities, or resoruces.
Fn, FnMut, and FnOnce with certain parameters implement this
trait by the crate. Of course struct also can implement this trait. It’s
useful when you need some data for a system.
§Examples
Here’s an example of system declaration.
#[derive(Component)] struct Ca;
#[derive(Component)] struct Cb;
#[derive(Component)] struct Cc;
#[derive(Component)] struct Cd;
#[derive(Resource)] struct Ra(i32);
#[derive(Resource)] struct Rb(i32);
#[derive(Resource)] struct Rc(i32);
#[derive(Resource)] struct Rd(i32);
filter!(Fa, Target = Ca); // All, Any, None are ommited for simplicity.
filter!(Fb, Target = Cb);
filter!(Fc, Target = Cc);
filter!(Fd, Target = Cd);
filter!(Fe, All = (Ca, Cb)); // Any and None are ommited for simplicity.
filter!(Ff, All = (Cc, Cd)); // Any and None are ommited for simplicity.
// Function system declaration.
fn system_a(
r: Read<(Fa, Fb)>, // Read request for the filters.
w: Write<(Fc, Fd)>, // Write request for the filters.
rr: ResRead<(Ra, Rb)>, // Read request for the resources.
rw: ResWrite<(Rc, Rd)>, // Write request for the resources.
ew: EntWrite<(Fe, Ff)>, // Write request for the filters.
) { /* ... */ }
// Struct system declaration.
request!(Req,
Read = (Fa, Fb),
Write = (Fc, Fd),
ResRead = (Ra, Rb),
ResWrite = (Rc, Rd),
EntWrite = (Fe, Ff)
);
struct SystemB;
impl System for SystemB {
type Request = Req;
fn run(&mut self, resp: Response<'_, Self::Request>) { /* ... */ }
}This is another example to show how to add systems to an ECS instance.
use my_ecs::prelude::*;
// Systems do nothing for simplicity.
struct StructSystem {
data: String,
}
impl System for StructSystem {
type Request = ();
fn run(&mut self, resp: Response<'_, Self::Request>) { /* ... */ }
}
let struct_system = StructSystem { data: "".to_owned() };
let fn_system = || {};
let s: String = "".to_owned();
let fn_once_system = move || { drop(s); };
Ecs::default(WorkerPool::new(), [])
.add_systems((struct_system, fn_system))
.add_once_system(fn_once_system)
.unwrap();Required Associated Types§
Required Methods§
Provided Methods§
Sourcefn on_transition(&mut self, from: SystemState, to: SystemState)
fn on_transition(&mut self, from: SystemState, to: SystemState)
Does a certain behavior on transitions of system state.
See SystemState for more details.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.