Skip to main content

rusting_core/
schedule.rs

1use std::time::Duration;
2
3use bevy_ecs::prelude::Resource;
4
5/// The ordered stages executed by an application's `update` method.
6#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
7pub enum ScheduleStage {
8    Startup,
9    FixedUpdate,
10    Update,
11    PostUpdate,
12    RenderExtract,
13}
14
15/// Summary of work performed during a single application update.
16#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
17pub struct FrameReport {
18    pub fixed_steps: u32,
19    pub exit_requested: bool,
20}
21
22/// CPU time spent in each part of the last frame, for the profiler.
23///
24/// The application fills `physics` (every fixed step: physics and the
25/// game's fixed systems), `update`, `post_update` (transform propagation
26/// and other engine work after the game's systems) and `extraction`; the
27/// renderer fills `preparation` and `recording`, and a host with a UI fills
28/// `editor`.
29#[derive(Resource, Clone, Copy, Debug, Default, PartialEq, Eq)]
30pub struct CpuFrameTimings {
31    pub physics: Duration,
32    pub update: Duration,
33    pub post_update: Duration,
34    pub extraction: Duration,
35    pub preparation: Duration,
36    pub recording: Duration,
37    pub editor: Duration,
38}
39
40#[cfg(test)]
41mod tests {
42    use std::fmt::Debug;
43    use std::hash::Hash;
44    use std::time::Duration;
45
46    use bevy_ecs::prelude::World;
47
48    use super::{CpuFrameTimings, FrameReport, ScheduleStage};
49
50    #[test]
51    fn frame_report_default_has_no_work_or_exit_request() {
52        assert_eq!(
53            FrameReport::default(),
54            FrameReport {
55                fixed_steps: 0,
56                exit_requested: false,
57            }
58        );
59    }
60
61    #[test]
62    fn schedule_stage_variants_are_distinct_and_debuggable() {
63        let stages = [
64            ScheduleStage::Startup,
65            ScheduleStage::FixedUpdate,
66            ScheduleStage::Update,
67            ScheduleStage::PostUpdate,
68            ScheduleStage::RenderExtract,
69        ];
70
71        for (index, stage) in stages.iter().enumerate() {
72            assert!(!stages[..index].contains(stage));
73        }
74        assert_eq!(
75            format!("{:?}", ScheduleStage::RenderExtract),
76            "RenderExtract"
77        );
78    }
79
80    #[test]
81    fn schedule_types_preserve_their_public_trait_shape() {
82        fn assert_stage_traits<T: Clone + Copy + Debug + Eq + Hash>() {}
83        fn assert_report_traits<T: Clone + Copy + Debug + Default + Eq>() {}
84
85        assert_stage_traits::<ScheduleStage>();
86        assert_report_traits::<FrameReport>();
87    }
88
89    #[test]
90    fn frame_report_fields_are_publicly_constructible_and_readable() {
91        let report = FrameReport {
92            fixed_steps: 3,
93            exit_requested: true,
94        };
95
96        assert_eq!(report.fixed_steps, 3);
97        assert!(report.exit_requested);
98    }
99
100    #[test]
101    fn cpu_frame_timings_can_be_used_as_a_world_resource() {
102        let mut world = World::new();
103        world.init_resource::<CpuFrameTimings>();
104
105        assert_eq!(world.resource::<CpuFrameTimings>().physics, Duration::ZERO);
106        world.resource_mut::<CpuFrameTimings>().physics =
107            Duration::from_millis(2);
108        assert_eq!(
109            world.resource::<CpuFrameTimings>().physics,
110            Duration::from_millis(2)
111        );
112    }
113}