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` and `extraction`; the renderer fills
25/// `preparation` and `recording`, and a host with a UI fills `editor`.
26#[derive(Resource, Clone, Copy, Debug, Default, PartialEq, Eq)]
27pub struct CpuFrameTimings {
28    pub physics: Duration,
29    pub extraction: Duration,
30    pub preparation: Duration,
31    pub recording: Duration,
32    pub editor: Duration,
33}
34
35#[cfg(test)]
36mod tests {
37    use std::fmt::Debug;
38    use std::hash::Hash;
39    use std::time::Duration;
40
41    use bevy_ecs::prelude::World;
42
43    use super::{CpuFrameTimings, FrameReport, ScheduleStage};
44
45    #[test]
46    fn frame_report_default_has_no_work_or_exit_request() {
47        assert_eq!(
48            FrameReport::default(),
49            FrameReport {
50                fixed_steps: 0,
51                exit_requested: false,
52            }
53        );
54    }
55
56    #[test]
57    fn schedule_stage_variants_are_distinct_and_debuggable() {
58        let stages = [
59            ScheduleStage::Startup,
60            ScheduleStage::FixedUpdate,
61            ScheduleStage::Update,
62            ScheduleStage::PostUpdate,
63            ScheduleStage::RenderExtract,
64        ];
65
66        for (index, stage) in stages.iter().enumerate() {
67            assert!(!stages[..index].contains(stage));
68        }
69        assert_eq!(
70            format!("{:?}", ScheduleStage::RenderExtract),
71            "RenderExtract"
72        );
73    }
74
75    #[test]
76    fn schedule_types_preserve_their_public_trait_shape() {
77        fn assert_stage_traits<T: Clone + Copy + Debug + Eq + Hash>() {}
78        fn assert_report_traits<T: Clone + Copy + Debug + Default + Eq>() {}
79
80        assert_stage_traits::<ScheduleStage>();
81        assert_report_traits::<FrameReport>();
82    }
83
84    #[test]
85    fn frame_report_fields_are_publicly_constructible_and_readable() {
86        let report = FrameReport {
87            fixed_steps: 3,
88            exit_requested: true,
89        };
90
91        assert_eq!(report.fixed_steps, 3);
92        assert!(report.exit_requested);
93    }
94
95    #[test]
96    fn cpu_frame_timings_can_be_used_as_a_world_resource() {
97        let mut world = World::new();
98        world.init_resource::<CpuFrameTimings>();
99
100        assert_eq!(world.resource::<CpuFrameTimings>().physics, Duration::ZERO);
101        world.resource_mut::<CpuFrameTimings>().physics =
102            Duration::from_millis(2);
103        assert_eq!(
104            world.resource::<CpuFrameTimings>().physics,
105            Duration::from_millis(2)
106        );
107    }
108}