sparkles_core/timestamp/
mod.rs

1//! Timestamps adaptively choose implementation depending on architecture, std support and `cortex-m` feature
2//!
3//! Priority order:
4//! 1. If your CPU architecture is x86 or x86_64, `X86Timestamp` is used
5//! 1. If your CPU architecture is aarch64, `Aarch64Timestamp` is used
6//! 2. Otherwise, if you're in std environment, `std::time::Instant` is selected as timestamp provider.
7//! 3. If feature `cortex-m` is active, `CortexMTimestamp` is used.
8//! 4. If none of above is true, compile error is emitted.
9
10#[cfg(any(target_arch="x86", target_arch="x86_64"))]
11pub mod x86;
12#[cfg(any(target_arch="x86", target_arch="x86_64"))]
13pub use x86::X86Timestamp as Timestamp;
14
15#[cfg(target_arch="aarch64")]
16pub mod aarch64;
17#[cfg(target_arch="aarch64")]
18pub use aarch64::AArch64Timestamp as Timestamp;
19
20#[cfg(all(not(target_os="none"), not(any(target_arch="x86", target_arch="x86_64", target_arch="aarch64"))))]
21pub mod std;
22#[cfg(all(not(target_os="none"), not(any(target_arch="x86", target_arch="x86_64", target_arch="aarch64"))))]
23pub use std::StdTimestamp as Timestamp;
24
25#[cfg(feature="cortex-m")]
26pub mod cortex_m;
27
28#[cfg(feature="cortex-m")]
29pub use cortex_m::CortexMTimestamp as Timestamp;
30
31#[cfg(not(any(target_arch="x86", target_arch="x86_64", target_arch="aarch64", feature="cortex-m", not(target_os="none"))))]
32compile_error!("Unsupported platform! Either std or cortex-m are currently supported");
33
34/// TimestampProvider is a source for relatively stable timestamp, which wraps around after reaching maximum value.
35///
36/// Maximum value is defined as unsigned integer composed of TIMESTAMP_VALID_BITS binary ones.
37pub trait TimestampProvider {
38    /// Numeric timestamp type, can be either u32 or u64.
39    type TimestampType: Copy + Sized + From<u64>;
40
41    /// Returns current timestamp from provider.
42    fn now() -> Self::TimestampType;
43
44    /// Define how many bits are valid in timestamp, returned from now(). Other bits are zeroed.
45    const TIMESTAMP_VALID_BITS: u8 = (size_of::<Self::TimestampType>() as u8) << 3;
46    /// Max timestamp value. After reaching this value, next tick will be 0.
47    const MAX_VALUE: u64 = ((1u128 << Self::TIMESTAMP_VALID_BITS) - 1) as u64;
48}