From 66c4e73ae177982cf2ac37c111cb2f2c14fc37c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Onur=20=C3=96zkan?= Date: Sun, 19 Jul 2026 22:39:55 +0300 Subject: [PATCH] libs/kaptan-time: add monotonic timestamp contract --- libs/kaptan-time/Cargo.toml | 10 ++++++++++ libs/kaptan-time/src/lib.rs | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 libs/kaptan-time/Cargo.toml create mode 100644 libs/kaptan-time/src/lib.rs diff --git a/libs/kaptan-time/Cargo.toml b/libs/kaptan-time/Cargo.toml new file mode 100644 index 0000000..c6d4443 --- /dev/null +++ b/libs/kaptan-time/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "kaptan-time" +edition.workspace = true +license.workspace = true +repository.workspace = true +rust-version.workspace = true +version.workspace = true + +[lints] +workspace = true diff --git a/libs/kaptan-time/src/lib.rs b/libs/kaptan-time/src/lib.rs new file mode 100644 index 0000000..b9ee254 --- /dev/null +++ b/libs/kaptan-time/src/lib.rs @@ -0,0 +1,32 @@ +//! Monotonic flight-controller time values and clock contract. + +#![no_std] + +/// Monotonic microsecond instant since boot. +/// +/// Platform clocks must extend narrower counters before constructing this type. +/// A value never wraps during one power cycle. +#[derive(Clone, Copy, Debug, Default, Eq, Ord, PartialEq, PartialOrd)] +pub struct Timestamp(u64); + +impl Timestamp { + /// Creates an instant from monotonic microseconds since boot. + pub const fn from_microseconds(microseconds: u64) -> Self { + Self(microseconds) + } + + /// Returns the instant in monotonic microseconds since boot. + pub const fn as_microseconds(self) -> u64 { + self.0 + } +} + +/// Supplies monotonically increasing instants to reusable runtime services. +/// +/// Implementations may extend a hardware counter internally. `now` takes a +/// mutable receiver so an implementation can record wrap state without hidden +/// interior mutability or synchronization. +pub trait MonotonicClock { + /// Captures the current monotonic instant since boot. + fn now(&mut self) -> Timestamp; +}