2020-11-17 02:18:00 +00:00
|
|
|
use crate::{
|
|
|
|
ArchetypeComponent, IntoSystem, Resources, System, SystemId, ThreadLocalExecution, TypeAccess,
|
|
|
|
World,
|
|
|
|
};
|
|
|
|
use std::{any::TypeId, borrow::Cow};
|
|
|
|
|
|
|
|
pub struct ChainSystem<SystemA, SystemB> {
|
|
|
|
system_a: SystemA,
|
|
|
|
system_b: SystemB,
|
|
|
|
name: Cow<'static, str>,
|
|
|
|
id: SystemId,
|
|
|
|
pub(crate) archetype_component_access: TypeAccess<ArchetypeComponent>,
|
|
|
|
pub(crate) resource_access: TypeAccess<TypeId>,
|
|
|
|
}
|
|
|
|
|
2020-12-13 02:04:42 +00:00
|
|
|
impl<SystemA: System, SystemB: System<In = SystemA::Out>> System for ChainSystem<SystemA, SystemB> {
|
|
|
|
type In = SystemA::In;
|
|
|
|
type Out = SystemB::Out;
|
2020-11-17 02:18:00 +00:00
|
|
|
|
|
|
|
fn name(&self) -> Cow<'static, str> {
|
|
|
|
self.name.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn id(&self) -> SystemId {
|
|
|
|
self.id
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update(&mut self, world: &World) {
|
|
|
|
self.archetype_component_access.clear();
|
|
|
|
self.resource_access.clear();
|
|
|
|
self.system_a.update(world);
|
|
|
|
self.system_b.update(world);
|
|
|
|
|
|
|
|
self.archetype_component_access
|
|
|
|
.union(self.system_a.archetype_component_access());
|
|
|
|
self.resource_access.union(self.system_b.resource_access());
|
|
|
|
}
|
|
|
|
|
|
|
|
fn archetype_component_access(&self) -> &TypeAccess<ArchetypeComponent> {
|
|
|
|
&self.archetype_component_access
|
|
|
|
}
|
|
|
|
|
|
|
|
fn resource_access(&self) -> &TypeAccess<TypeId> {
|
|
|
|
&self.resource_access
|
|
|
|
}
|
|
|
|
|
|
|
|
fn thread_local_execution(&self) -> ThreadLocalExecution {
|
|
|
|
ThreadLocalExecution::NextFlush
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn run_unsafe(
|
|
|
|
&mut self,
|
2020-12-13 02:04:42 +00:00
|
|
|
input: Self::In,
|
2020-11-17 02:18:00 +00:00
|
|
|
world: &World,
|
|
|
|
resources: &Resources,
|
2020-12-13 02:04:42 +00:00
|
|
|
) -> Option<Self::Out> {
|
2020-11-17 02:18:00 +00:00
|
|
|
let out = self.system_a.run_unsafe(input, world, resources).unwrap();
|
|
|
|
self.system_b.run_unsafe(out, world, resources)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run_thread_local(&mut self, world: &mut World, resources: &mut Resources) {
|
|
|
|
self.system_a.run_thread_local(world, resources);
|
|
|
|
self.system_b.run_thread_local(world, resources);
|
|
|
|
}
|
2020-11-18 01:06:47 +00:00
|
|
|
|
|
|
|
fn initialize(&mut self, world: &mut World, resources: &mut Resources) {
|
|
|
|
self.system_a.initialize(world, resources);
|
|
|
|
self.system_b.initialize(world, resources);
|
|
|
|
}
|
2020-11-17 02:18:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait IntoChainSystem<AParams, BParams, IntoB, SystemA, SystemB>:
|
|
|
|
IntoSystem<AParams, SystemA> + Sized
|
|
|
|
where
|
|
|
|
IntoB: IntoSystem<BParams, SystemB>,
|
|
|
|
SystemA: System,
|
2020-12-13 02:04:42 +00:00
|
|
|
SystemB: System<In = SystemA::Out>,
|
2020-11-17 02:18:00 +00:00
|
|
|
{
|
|
|
|
fn chain(self, system: IntoB) -> ChainSystem<SystemA, SystemB>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<AParams, BParams, IntoA, IntoB, SystemA, SystemB>
|
|
|
|
IntoChainSystem<AParams, BParams, IntoB, SystemA, SystemB> for IntoA
|
|
|
|
where
|
|
|
|
SystemA: System,
|
2020-12-13 02:04:42 +00:00
|
|
|
SystemB: System<In = SystemA::Out>,
|
2020-11-17 02:18:00 +00:00
|
|
|
IntoA: IntoSystem<AParams, SystemA>,
|
|
|
|
IntoB: IntoSystem<BParams, SystemB>,
|
|
|
|
{
|
|
|
|
fn chain(self, system: IntoB) -> ChainSystem<SystemA, SystemB> {
|
|
|
|
let system_a = self.system();
|
|
|
|
let system_b = system.system();
|
|
|
|
ChainSystem {
|
|
|
|
name: Cow::Owned(format!("Chain({}, {})", system_a.name(), system_b.name())),
|
|
|
|
system_a,
|
|
|
|
system_b,
|
|
|
|
archetype_component_access: Default::default(),
|
|
|
|
resource_access: Default::default(),
|
|
|
|
id: SystemId::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|