concinnity_core/ecs/clock.rs
1//! The monotonic clock the frame loop times systems with, published as a
2//! resource by the host that has one.
3//!
4//! Reading a wall clock needs an operating system, so the loop names a function
5//! pointer instead of a platform type. A world running without one (a headless
6//! or embedded host) records zero micros per system rather than losing the
7//! profile entirely.
8
9/// A monotonic microsecond source, installed as a world resource by the host.
10///
11/// `World::step` reads it once per tick and brackets each system with it. Only
12/// differences are used, so the epoch is the host's to choose; it must be
13/// monotonic within a process.
14pub struct Clock(pub fn() -> u64);
15
16impl core::fmt::Debug for Clock {
17 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
18 f.debug_struct("Clock").finish_non_exhaustive()
19 }
20}
21
22#[cfg(test)]
23mod tests {
24 use super::*;
25 use crate::ecs::World;
26
27 fn ticking() -> u64 {
28 7
29 }
30
31 // The clock is an ordinary resource: the host installs it, the loop reads
32 // it back through the same map every other protocol resource uses.
33 #[test]
34 fn a_installed_clock_reads_back() {
35 let mut world = World::new();
36 assert!(world.resource::<Clock>().is_none());
37 world.insert_resource(Clock(ticking));
38 assert_eq!((world.resource::<Clock>().expect("installed").0)(), 7);
39 }
40}