|
| 1 | +use super::{Schedule, StageLabel, SystemDescriptor}; |
| 2 | +use crate::system::Command; |
| 3 | +use crate::world::World; |
| 4 | + |
| 5 | +#[derive(Default)] |
| 6 | +pub struct SchedulerCommandQueue { |
| 7 | + items: Vec<Box<dyn SchedulerCommand>>, |
| 8 | +} |
| 9 | + |
| 10 | +impl SchedulerCommandQueue { |
| 11 | + pub fn push<C>(&mut self, command: C) |
| 12 | + where |
| 13 | + C: SchedulerCommand, |
| 14 | + { |
| 15 | + self.items.push(Box::new(command)); |
| 16 | + } |
| 17 | + |
| 18 | + pub fn apply(&mut self, schedule: &mut Schedule, offset: usize) { |
| 19 | + for command in self.items.drain(offset..) { |
| 20 | + command.write(schedule); |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + pub fn len(&self) -> usize { |
| 25 | + self.items.len() |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +/// A [`Schedule`] mutation. |
| 30 | +pub trait SchedulerCommand: Send + Sync + 'static { |
| 31 | + fn write(self: Box<Self>, schedule: &mut Schedule); |
| 32 | +} |
| 33 | + |
| 34 | +impl<T> Command for T |
| 35 | +where |
| 36 | + T: SchedulerCommand, |
| 37 | +{ |
| 38 | + fn write(self, world: &mut World) { |
| 39 | + world.scheduler_commands.push(self); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +pub struct InsertSystem<S> |
| 44 | +where |
| 45 | + S: StageLabel, |
| 46 | +{ |
| 47 | + pub system: SystemDescriptor, |
| 48 | + pub stage_label: S, |
| 49 | +} |
| 50 | + |
| 51 | +impl<S> SchedulerCommand for InsertSystem<S> |
| 52 | +where |
| 53 | + S: StageLabel, |
| 54 | +{ |
| 55 | + fn write(self: Box<Self>, schedule: &mut Schedule) { |
| 56 | + schedule.add_system_to_stage(self.stage_label, self.system); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +#[cfg(test)] |
| 61 | +mod tests { |
| 62 | + use crate::{ |
| 63 | + prelude::ResMut, |
| 64 | + schedule::{ |
| 65 | + InsertSystem, IntoSystemDescriptor, Schedule, SchedulerCommandQueue, SystemStage, |
| 66 | + }, |
| 67 | + system::Commands, |
| 68 | + world::World, |
| 69 | + }; |
| 70 | + |
| 71 | + #[test] |
| 72 | + fn insert_system() { |
| 73 | + fn sample_system(mut _commands: Commands) {} |
| 74 | + let mut schedule = Schedule::default(); |
| 75 | + schedule.add_stage("test", SystemStage::parallel()); |
| 76 | + let mut queue = SchedulerCommandQueue::default(); |
| 77 | + queue.push(InsertSystem { |
| 78 | + system: sample_system.into_descriptor(), |
| 79 | + stage_label: "test", |
| 80 | + }); |
| 81 | + queue.apply(&mut schedule, 0); |
| 82 | + |
| 83 | + let stage = schedule.get_stage::<SystemStage>(&"test").unwrap(); |
| 84 | + assert_eq!(stage.parallel_systems().len(), 1); |
| 85 | + } |
| 86 | + |
| 87 | + #[derive(Debug, Default, PartialEq)] |
| 88 | + struct TestResource(Option<()>); |
| 89 | + |
| 90 | + #[test] |
| 91 | + fn insert_system_from_system() { |
| 92 | + fn sample_system(mut commands: Commands) { |
| 93 | + commands.insert_system( |
| 94 | + |mut res: ResMut<TestResource>| { |
| 95 | + res.0 = Some(()); |
| 96 | + }, |
| 97 | + "test", |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + let mut world = World::default(); |
| 102 | + world.insert_resource(TestResource(None)); |
| 103 | + |
| 104 | + let mut schedule = Schedule::default(); |
| 105 | + schedule.add_stage("test", SystemStage::parallel()); |
| 106 | + schedule.add_system_to_stage("test", sample_system); |
| 107 | + schedule.run_once(&mut world); |
| 108 | + |
| 109 | + let stage = schedule.get_stage::<SystemStage>(&"test").unwrap(); |
| 110 | + assert_eq!(stage.parallel_systems().len(), 2); |
| 111 | + |
| 112 | + schedule.run_once(&mut world); |
| 113 | + assert_eq!( |
| 114 | + world.get_resource::<TestResource>(), |
| 115 | + Some(&TestResource(Some(()))) |
| 116 | + ); |
| 117 | + } |
| 118 | + |
| 119 | + #[test] |
| 120 | + fn insert_with_nested_schedule() { |
| 121 | + fn sample_system(mut commands: Commands) { |
| 122 | + commands.insert_system(|| {}, "test"); |
| 123 | + } |
| 124 | + |
| 125 | + let mut world = World::default(); |
| 126 | + |
| 127 | + let nested_schedule = Schedule::default(); |
| 128 | + let mut schedule = Schedule::default(); |
| 129 | + schedule.add_stage("test", SystemStage::parallel()); |
| 130 | + schedule.add_system_to_stage("test", sample_system); |
| 131 | + schedule.add_stage("nested", nested_schedule); |
| 132 | + schedule.run_once(&mut world); |
| 133 | + |
| 134 | + let stage = schedule.get_stage::<SystemStage>(&"test").unwrap(); |
| 135 | + assert_eq!(stage.parallel_systems().len(), 2); |
| 136 | + } |
| 137 | +} |
0 commit comments