Skip to content

Commit

Permalink
Refactor Box<dyn System> to BoxedSystem (bevyengine#1191)
Browse files Browse the repository at this point in the history
Added BoxedSystem
  • Loading branch information
TheRawMeatball authored Jan 3, 2021
1 parent 60be998 commit c69aa98
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 22 deletions.
4 changes: 2 additions & 2 deletions crates/bevy_ecs/src/schedule/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ pub use stage::*;
pub use stage_executor::*;
pub use state::*;

use crate::{IntoSystem, Resources, System, World};
use crate::{BoxedSystem, IntoSystem, Resources, System, World};
use bevy_utils::HashMap;

#[derive(Default)]
pub struct Schedule {
stages: HashMap<String, Box<dyn Stage>>,
stage_order: Vec<String>,
run_criteria: Option<Box<dyn System<In = (), Out = ShouldRun>>>,
run_criteria: Option<BoxedSystem<(), ShouldRun>>,
run_criteria_initialized: bool,
}

Expand Down
9 changes: 5 additions & 4 deletions crates/bevy_ecs/src/schedule/stage.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::{any::TypeId, borrow::Cow};

use crate::{
ArchetypeComponent, Resources, System, SystemId, ThreadLocalExecution, TypeAccess, World,
ArchetypeComponent, BoxedSystem, Resources, System, SystemId, ThreadLocalExecution, TypeAccess,
World,
};
use bevy_utils::HashSet;
use downcast_rs::{impl_downcast, Downcast};
Expand All @@ -24,10 +25,10 @@ pub trait Stage: Downcast + Send + Sync {
impl_downcast!(Stage);

pub struct SystemStage {
systems: Vec<Box<dyn System<In = (), Out = ()>>>,
systems: Vec<BoxedSystem>,
system_ids: HashSet<SystemId>,
executor: Box<dyn SystemStageExecutor>,
run_criteria: Option<Box<dyn System<In = (), Out = ShouldRun>>>,
run_criteria: Option<BoxedSystem<(), ShouldRun>>,
run_criteria_initialized: bool,
uninitialized_systems: Vec<usize>,
unexecuted_systems: Vec<usize>,
Expand Down Expand Up @@ -74,7 +75,7 @@ impl SystemStage {
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()) {
panic!(
"System with id {:?} ({}) already exists",
Expand Down
14 changes: 8 additions & 6 deletions crates/bevy_ecs/src/schedule/stage_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ use bevy_utils::tracing::trace;
use downcast_rs::{impl_downcast, Downcast};
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 {
fn execute_stage(
&mut self,
systems: &mut [Box<dyn System<In = (), Out = ()>>],
systems: &mut [BoxedSystem],
changed_systems: &[usize],
world: &mut World,
resources: &mut Resources,
Expand All @@ -25,7 +27,7 @@ pub struct SerialSystemStageExecutor;
impl SystemStageExecutor for SerialSystemStageExecutor {
fn execute_stage(
&mut self,
systems: &mut [Box<dyn System<In = (), Out = ()>>],
systems: &mut [BoxedSystem],
_changed_systems: &[usize],
world: &mut World,
resources: &mut Resources,
Expand Down Expand Up @@ -109,7 +111,7 @@ impl ParallelSystemStageExecutor {
pub fn prepare_to_next_thread_local(
&mut self,
world: &World,
systems: &mut [Box<dyn System<In = (), Out = ()>>],
systems: &mut [BoxedSystem],
stage_changed: bool,
next_thread_local_index: usize,
) -> Range<usize> {
Expand Down Expand Up @@ -291,7 +293,7 @@ impl ParallelSystemStageExecutor {
&self,
world: &World,
resources: &Resources,
systems: &mut [Box<dyn System<In = (), Out = ()>>],
systems: &mut [BoxedSystem],
prepared_system_range: Range<usize>,
compute_pool: &TaskPool,
) {
Expand Down Expand Up @@ -387,7 +389,7 @@ impl ParallelSystemStageExecutor {
impl SystemStageExecutor for ParallelSystemStageExecutor {
fn execute_stage(
&mut self,
systems: &mut [Box<dyn System<In = (), Out = ()>>],
systems: &mut [BoxedSystem],
changed_systems: &[usize],
world: &mut World,
resources: &mut Resources,
Expand Down
2 changes: 2 additions & 0 deletions crates/bevy_ecs/src/system/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,5 @@ pub trait System: Send + Sync + 'static {
fn run_thread_local(&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>>;
6 changes: 4 additions & 2 deletions crates/bevy_pbr/src/render_graph/lights_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use crate::{
render_graph::uniform,
};
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::{
render_graph::{CommandQueue, Node, ResourceSlots, SystemNode},
renderer::{
Expand Down Expand Up @@ -51,7 +53,7 @@ struct LightCount {
unsafe impl Byteable for LightCount {}

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();
commands.insert_local_resource(
system.id(),
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_render/src/render_graph/node.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{Edge, RenderGraphError, ResourceSlotInfo, ResourceSlots};
use crate::renderer::RenderContext;
use bevy_ecs::{Commands, Resources, System, World};
use bevy_ecs::{BoxedSystem, Commands, Resources, World};
use bevy_utils::Uuid;
use downcast_rs::{impl_downcast, Downcast};
use std::{borrow::Cow, fmt::Debug};
Expand Down Expand Up @@ -37,7 +37,7 @@ pub trait Node: Downcast + Send + Sync + 'static {
impl_downcast!(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)]
Expand Down
6 changes: 4 additions & 2 deletions crates/bevy_render/src/render_graph/nodes/camera_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use crate::{
};
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 std::borrow::Cow;

Expand Down Expand Up @@ -44,7 +46,7 @@ impl Node 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();
commands.insert_local_resource(
system.id(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use crate::{
use bevy_app::{EventReader, Events};
use bevy_asset::{Asset, AssetEvent, Assets, Handle, HandleId};
use bevy_ecs::{
Changed, Commands, Entity, IntoSystem, Local, Or, Query, QuerySet, Res, ResMut, Resources,
System, With, World,
BoxedSystem, Changed, Commands, Entity, IntoSystem, Local, Or, Query, QuerySet, Res, ResMut,
Resources, System, With, World,
};
use bevy_utils::HashMap;
use renderer::{AssetRenderResourceBindings, BufferId, RenderResourceType, RenderResources};
Expand Down Expand Up @@ -401,7 +401,7 @@ impl<T> SystemNode for RenderResourcesNode<T>
where
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();
commands.insert_local_resource(
system.id(),
Expand Down Expand Up @@ -584,7 +584,7 @@ impl<T> SystemNode for AssetRenderResourcesNode<T>
where
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();
commands.insert_local_resource(
system.id(),
Expand Down

0 comments on commit c69aa98

Please sign in to comment.