Refactor Box<dyn System> to BoxedSystem (#1191)

Added BoxedSystem
This commit is contained in:
TheRawMeatball 2021-01-03 23:39:30 +03:00 committed by GitHub
parent 60be99859a
commit c69aa98a60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 31 additions and 22 deletions

View File

@ -6,14 +6,14 @@ pub use stage::*;
pub use stage_executor::*; pub use stage_executor::*;
pub use state::*; pub use state::*;
use crate::{IntoSystem, Resources, System, World}; use crate::{BoxedSystem, IntoSystem, Resources, System, World};
use bevy_utils::HashMap; use bevy_utils::HashMap;
#[derive(Default)] #[derive(Default)]
pub struct Schedule { pub struct Schedule {
stages: HashMap<String, Box<dyn Stage>>, stages: HashMap<String, Box<dyn Stage>>,
stage_order: Vec<String>, stage_order: Vec<String>,
run_criteria: Option<Box<dyn System<In = (), Out = ShouldRun>>>, run_criteria: Option<BoxedSystem<(), ShouldRun>>,
run_criteria_initialized: bool, run_criteria_initialized: bool,
} }

View File

@ -1,7 +1,8 @@
use std::{any::TypeId, borrow::Cow}; use std::{any::TypeId, borrow::Cow};
use crate::{ use crate::{
ArchetypeComponent, Resources, System, SystemId, ThreadLocalExecution, TypeAccess, World, ArchetypeComponent, BoxedSystem, Resources, System, SystemId, ThreadLocalExecution, TypeAccess,
World,
}; };
use bevy_utils::HashSet; use bevy_utils::HashSet;
use downcast_rs::{impl_downcast, Downcast}; use downcast_rs::{impl_downcast, Downcast};
@ -24,10 +25,10 @@ pub trait Stage: Downcast + Send + Sync {
impl_downcast!(Stage); impl_downcast!(Stage);
pub struct SystemStage { pub struct SystemStage {
systems: Vec<Box<dyn System<In = (), Out = ()>>>, systems: Vec<BoxedSystem>,
system_ids: HashSet<SystemId>, system_ids: HashSet<SystemId>,
executor: Box<dyn SystemStageExecutor>, executor: Box<dyn SystemStageExecutor>,
run_criteria: Option<Box<dyn System<In = (), Out = ShouldRun>>>, run_criteria: Option<BoxedSystem<(), ShouldRun>>,
run_criteria_initialized: bool, run_criteria_initialized: bool,
uninitialized_systems: Vec<usize>, uninitialized_systems: Vec<usize>,
unexecuted_systems: Vec<usize>, unexecuted_systems: Vec<usize>,
@ -74,7 +75,7 @@ impl SystemStage {
self self
} }
pub fn add_system_boxed(&mut self, system: Box<dyn System<In = (), Out = ()>>) -> &mut Self { pub fn add_system_boxed(&mut self, system: BoxedSystem) -> &mut Self {
if self.system_ids.contains(&system.id()) { if self.system_ids.contains(&system.id()) {
panic!( panic!(
"System with id {:?} ({}) already exists", "System with id {:?} ({}) already exists",

View File

@ -5,12 +5,14 @@ use bevy_utils::tracing::trace;
use downcast_rs::{impl_downcast, Downcast}; use downcast_rs::{impl_downcast, Downcast};
use fixedbitset::FixedBitSet; use fixedbitset::FixedBitSet;
use crate::{ArchetypesGeneration, Resources, System, ThreadLocalExecution, TypeAccess, World}; use crate::{
ArchetypesGeneration, BoxedSystem, Resources, ThreadLocalExecution, TypeAccess, World,
};
pub trait SystemStageExecutor: Downcast + Send + Sync { pub trait SystemStageExecutor: Downcast + Send + Sync {
fn execute_stage( fn execute_stage(
&mut self, &mut self,
systems: &mut [Box<dyn System<In = (), Out = ()>>], systems: &mut [BoxedSystem],
changed_systems: &[usize], changed_systems: &[usize],
world: &mut World, world: &mut World,
resources: &mut Resources, resources: &mut Resources,
@ -25,7 +27,7 @@ pub struct SerialSystemStageExecutor;
impl SystemStageExecutor for SerialSystemStageExecutor { impl SystemStageExecutor for SerialSystemStageExecutor {
fn execute_stage( fn execute_stage(
&mut self, &mut self,
systems: &mut [Box<dyn System<In = (), Out = ()>>], systems: &mut [BoxedSystem],
_changed_systems: &[usize], _changed_systems: &[usize],
world: &mut World, world: &mut World,
resources: &mut Resources, resources: &mut Resources,
@ -109,7 +111,7 @@ impl ParallelSystemStageExecutor {
pub fn prepare_to_next_thread_local( pub fn prepare_to_next_thread_local(
&mut self, &mut self,
world: &World, world: &World,
systems: &mut [Box<dyn System<In = (), Out = ()>>], systems: &mut [BoxedSystem],
stage_changed: bool, stage_changed: bool,
next_thread_local_index: usize, next_thread_local_index: usize,
) -> Range<usize> { ) -> Range<usize> {
@ -291,7 +293,7 @@ impl ParallelSystemStageExecutor {
&self, &self,
world: &World, world: &World,
resources: &Resources, resources: &Resources,
systems: &mut [Box<dyn System<In = (), Out = ()>>], systems: &mut [BoxedSystem],
prepared_system_range: Range<usize>, prepared_system_range: Range<usize>,
compute_pool: &TaskPool, compute_pool: &TaskPool,
) { ) {
@ -387,7 +389,7 @@ impl ParallelSystemStageExecutor {
impl SystemStageExecutor for ParallelSystemStageExecutor { impl SystemStageExecutor for ParallelSystemStageExecutor {
fn execute_stage( fn execute_stage(
&mut self, &mut self,
systems: &mut [Box<dyn System<In = (), Out = ()>>], systems: &mut [BoxedSystem],
changed_systems: &[usize], changed_systems: &[usize],
world: &mut World, world: &mut World,
resources: &mut Resources, resources: &mut Resources,

View File

@ -50,3 +50,5 @@ pub trait System: Send + Sync + 'static {
fn run_thread_local(&mut self, world: &mut World, resources: &mut Resources); fn run_thread_local(&mut self, world: &mut World, resources: &mut Resources);
fn initialize(&mut self, _world: &mut World, _resources: &mut Resources); fn initialize(&mut self, _world: &mut World, _resources: &mut Resources);
} }
pub type BoxedSystem<In = (), Out = ()> = Box<dyn System<In = In, Out = Out>>;

View File

@ -3,7 +3,9 @@ use crate::{
render_graph::uniform, render_graph::uniform,
}; };
use bevy_core::{AsBytes, Byteable}; use bevy_core::{AsBytes, Byteable};
use bevy_ecs::{Commands, IntoSystem, Local, Query, Res, ResMut, Resources, System, World}; use bevy_ecs::{
BoxedSystem, Commands, IntoSystem, Local, Query, Res, ResMut, Resources, System, World,
};
use bevy_render::{ use bevy_render::{
render_graph::{CommandQueue, Node, ResourceSlots, SystemNode}, render_graph::{CommandQueue, Node, ResourceSlots, SystemNode},
renderer::{ renderer::{
@ -51,7 +53,7 @@ struct LightCount {
unsafe impl Byteable for LightCount {} unsafe impl Byteable for LightCount {}
impl SystemNode for LightsNode { impl SystemNode for LightsNode {
fn get_system(&self, commands: &mut Commands) -> Box<dyn System<In = (), Out = ()>> { fn get_system(&self, commands: &mut Commands) -> BoxedSystem {
let system = lights_node_system.system(); let system = lights_node_system.system();
commands.insert_local_resource( commands.insert_local_resource(
system.id(), system.id(),

View File

@ -1,6 +1,6 @@
use super::{Edge, RenderGraphError, ResourceSlotInfo, ResourceSlots}; use super::{Edge, RenderGraphError, ResourceSlotInfo, ResourceSlots};
use crate::renderer::RenderContext; use crate::renderer::RenderContext;
use bevy_ecs::{Commands, Resources, System, World}; use bevy_ecs::{BoxedSystem, Commands, Resources, World};
use bevy_utils::Uuid; use bevy_utils::Uuid;
use downcast_rs::{impl_downcast, Downcast}; use downcast_rs::{impl_downcast, Downcast};
use std::{borrow::Cow, fmt::Debug}; use std::{borrow::Cow, fmt::Debug};
@ -37,7 +37,7 @@ pub trait Node: Downcast + Send + Sync + 'static {
impl_downcast!(Node); impl_downcast!(Node);
pub trait SystemNode: Node { pub trait SystemNode: Node {
fn get_system(&self, commands: &mut Commands) -> Box<dyn System<In = (), Out = ()>>; fn get_system(&self, commands: &mut Commands) -> BoxedSystem;
} }
#[derive(Debug)] #[derive(Debug)]

View File

@ -8,7 +8,9 @@ use crate::{
}; };
use bevy_core::AsBytes; use bevy_core::AsBytes;
use bevy_ecs::{Commands, IntoSystem, Local, Query, Res, ResMut, Resources, System, World}; use bevy_ecs::{
BoxedSystem, Commands, IntoSystem, Local, Query, Res, ResMut, Resources, System, World,
};
use bevy_transform::prelude::*; use bevy_transform::prelude::*;
use std::borrow::Cow; use std::borrow::Cow;
@ -44,7 +46,7 @@ impl Node for CameraNode {
} }
impl SystemNode for CameraNode { impl SystemNode for CameraNode {
fn get_system(&self, commands: &mut Commands) -> Box<dyn System<In = (), Out = ()>> { fn get_system(&self, commands: &mut Commands) -> BoxedSystem {
let system = camera_node_system.system(); let system = camera_node_system.system();
commands.insert_local_resource( commands.insert_local_resource(
system.id(), system.id(),

View File

@ -12,8 +12,8 @@ use crate::{
use bevy_app::{EventReader, Events}; use bevy_app::{EventReader, Events};
use bevy_asset::{Asset, AssetEvent, Assets, Handle, HandleId}; use bevy_asset::{Asset, AssetEvent, Assets, Handle, HandleId};
use bevy_ecs::{ use bevy_ecs::{
Changed, Commands, Entity, IntoSystem, Local, Or, Query, QuerySet, Res, ResMut, Resources, BoxedSystem, Changed, Commands, Entity, IntoSystem, Local, Or, Query, QuerySet, Res, ResMut,
System, With, World, Resources, System, With, World,
}; };
use bevy_utils::HashMap; use bevy_utils::HashMap;
use renderer::{AssetRenderResourceBindings, BufferId, RenderResourceType, RenderResources}; use renderer::{AssetRenderResourceBindings, BufferId, RenderResourceType, RenderResources};
@ -401,7 +401,7 @@ impl<T> SystemNode for RenderResourcesNode<T>
where where
T: renderer::RenderResources, T: renderer::RenderResources,
{ {
fn get_system(&self, commands: &mut Commands) -> Box<dyn System<In = (), Out = ()>> { fn get_system(&self, commands: &mut Commands) -> BoxedSystem {
let system = render_resources_node_system::<T>.system(); let system = render_resources_node_system::<T>.system();
commands.insert_local_resource( commands.insert_local_resource(
system.id(), system.id(),
@ -584,7 +584,7 @@ impl<T> SystemNode for AssetRenderResourcesNode<T>
where where
T: renderer::RenderResources + Asset, T: renderer::RenderResources + Asset,
{ {
fn get_system(&self, commands: &mut Commands) -> Box<dyn System<In = (), Out = ()>> { fn get_system(&self, commands: &mut Commands) -> BoxedSystem {
let system = asset_render_resources_node_system::<T>.system(); let system = asset_render_resources_node_system::<T>.system();
commands.insert_local_resource( commands.insert_local_resource(
system.id(), system.id(),