From: FUJITA Tomonori <tomo@flapping.org>
To: a.hindborg@kernel.org, boqun@kernel.org, ojeda@kernel.org
Cc: acourbot@nvidia.com, aliceryhl@google.com,
anna-maria@linutronix.de, bjorn3_gh@protonmail.com,
dakr@kernel.org, daniel.almeida@collabora.com,
frederic@kernel.org, gary@garyguo.net, jstultz@google.com,
lossin@kernel.org, lyude@redhat.com, mike@fireburn.co.uk,
sboyd@kernel.org, tamird@kernel.org, tglx@kernel.org,
tmgross@umich.edu, work@onurozkan.dev,
rust-for-linux@vger.kernel.org,
FUJITA Tomonori <fujita.tomonori@gmail.com>
Subject: [PATCH v1 1/2] rust: time: make Instant generic over the time unit
Date: Sat, 31 Oct 2026 09:33:33 +0900 [thread overview]
Message-ID: <20261031003334.4161188-2-tomo@flapping.org> (raw)
In-Reply-To: <20261031003334.4161188-1-tomo@flapping.org>
From: FUJITA Tomonori <fujita.tomonori@gmail.com>
Add a type parameter `U` to `Instant`, with `Nsec` as the default, so
that an `Instant` in seconds can be added later. The value is stored as
`U::Repr`, like `Delta<U>`.
`U` is bound by a new trait `InstantUnit` rather than `TimeUnit`. It
reads the clock in its unit, and jiffies is not a clock, so
`Instant<C, Jiffy>` must not exist.
`now()` stays one function on `Instant<C, U>`. With two inherent `now()`,
one per unit, the existing `Instant::now()` calls do not compile (E0034,
multiple applicable items). The other methods and operators are
implemented for `Instant<C, Nsec>` only. `Instant<C>` still means
`Instant<C, Nsec>`, so no user changes.
No functional change.
Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
---
rust/kernel/time.rs | 44 ++++++++++++++++++++++++++++++++------------
1 file changed, 32 insertions(+), 12 deletions(-)
diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs
index b6a2aab12a50..8232880bffe7 100644
--- a/rust/kernel/time.rs
+++ b/rust/kernel/time.rs
@@ -179,36 +179,43 @@ fn ktime_get() -> bindings::ktime_t {
/// A specific point in time.
///
+/// The time is read from the clock given by the type parameter `C` (see
+/// [`ClockId`]) and stored in the unit given by `U` (see [`InstantUnit`]); its
+/// value has type `U::Repr`. `U` defaults to [`Nsec`], so a plain
+/// [`Instant<C>`] is a point in time in nanoseconds.
+///
/// # Invariants
///
/// The `inner` value is in the range from 0 to `KTIME_MAX`.
#[repr(transparent)]
#[derive(PartialEq, PartialOrd, Eq, Ord)]
-pub struct Instant<C: ClockId> {
- inner: bindings::ktime_t,
+pub struct Instant<C: ClockId, U: InstantUnit = Nsec> {
+ inner: U::Repr,
_c: PhantomData<C>,
}
-impl<C: ClockId> Clone for Instant<C> {
+impl<C: ClockId, U: InstantUnit> Clone for Instant<C, U> {
fn clone(&self) -> Self {
*self
}
}
-impl<C: ClockId> Copy for Instant<C> {}
+impl<C: ClockId, U: InstantUnit> Copy for Instant<C, U> {}
-impl<C: ClockId> Instant<C> {
- /// Get the current time from the clock source.
+impl<C: ClockId, U: InstantUnit> Instant<C, U> {
+ /// Get the current time from the clock `C`.
#[inline]
pub fn now() -> Self {
- // INVARIANT: The `ClockId::ktime_get()` function returns a value in the range
- // from 0 to `KTIME_MAX`.
+ // INVARIANT: `InstantUnit` is sealed and its only impl, `Nsec`, calls
+ // `ClockId::ktime_get()`, which returns a value in the range from 0 to `KTIME_MAX`.
Self {
- inner: C::ktime_get(),
+ inner: U::now::<C>(),
_c: PhantomData,
}
}
+}
+impl<C: ClockId> Instant<C, Nsec> {
/// Return the amount of time elapsed since the [`Instant`].
#[inline]
pub fn elapsed(&self) -> Delta {
@@ -243,7 +250,7 @@ pub(crate) unsafe fn from_ktime(ktime: bindings::ktime_t) -> Self {
}
}
-impl<C: ClockId> ops::Sub for Instant<C> {
+impl<C: ClockId> ops::Sub for Instant<C, Nsec> {
type Output = Delta;
// By the type invariant, it never overflows.
@@ -255,7 +262,7 @@ fn sub(self, other: Instant<C>) -> Delta {
}
}
-impl<T: ClockId> ops::Add<Delta> for Instant<T> {
+impl<T: ClockId> ops::Add<Delta> for Instant<T, Nsec> {
type Output = Self;
#[inline]
@@ -275,7 +282,7 @@ fn add(self, rhs: Delta) -> Self::Output {
}
}
-impl<T: ClockId> ops::Sub<Delta> for Instant<T> {
+impl<T: ClockId> ops::Sub<Delta> for Instant<T, Nsec> {
type Output = Self;
#[inline]
@@ -335,6 +342,19 @@ impl TimeUnit for Jiffy {
const ZERO: Self::Repr = 0;
}
+/// A trait for time units that can be used with [`Instant`].
+pub trait InstantUnit: TimeUnit {
+ /// Return the current time of the clock `C` in this unit.
+ fn now<C: ClockId>() -> Self::Repr;
+}
+
+impl InstantUnit for Nsec {
+ #[inline]
+ fn now<C: ClockId>() -> i64 {
+ C::ktime_get()
+ }
+}
+
/// A span of time.
///
/// The span is stored in the unit given by the type parameter `U` (see
--
2.43.0
next prev parent reply other threads:[~2026-10-31 0:33 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-31 0:33 [PATCH v1 0/2] rust: time: seconds based Instant FUJITA Tomonori
2026-10-31 0:33 ` FUJITA Tomonori [this message]
2026-10-31 0:33 ` [PATCH v1 2/2] rust: time: add Instant in seconds FUJITA Tomonori
2026-10-31 3:56 ` [PATCH v1 0/2] rust: time: seconds based Instant Boqun Feng
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261031003334.4161188-2-tomo@flapping.org \
--to=tomo@flapping.org \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=aliceryhl@google.com \
--cc=anna-maria@linutronix.de \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=frederic@kernel.org \
--cc=fujita.tomonori@gmail.com \
--cc=gary@garyguo.net \
--cc=jstultz@google.com \
--cc=lossin@kernel.org \
--cc=lyude@redhat.com \
--cc=mike@fireburn.co.uk \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=sboyd@kernel.org \
--cc=tamird@kernel.org \
--cc=tglx@kernel.org \
--cc=tmgross@umich.edu \
--cc=work@onurozkan.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox