Re: [PATCH v8 3/7] rust: time: Introduce Instant type
From: Alice Ryhl
Date: Thu Jan 16 2025 - 04:33:11 EST
On Thu, Jan 16, 2025 at 5:42 AM FUJITA Tomonori
<fujita.tomonori@xxxxxxxxx> wrote:
>
> Introduce a type representing a specific point in time. We could use
> the Ktime type but C's ktime_t is used for both timestamp and
> timedelta. To avoid confusion, introduce a new Instant type for
> timestamp.
>
> Rename Ktime to Instant and modify their methods for timestamp.
>
> Implement the subtraction operator for Instant:
>
> Delta = Instant A - Instant B
>
> The operation never overflows (Instant ranges from 0 to
> `KTIME_MAX`).
>
> Reviewed-by: Boqun Feng <boqun.feng@xxxxxxxxx>
> Signed-off-by: FUJITA Tomonori <fujita.tomonori@xxxxxxxxx>
> ---
> rust/kernel/time.rs | 48 +++++++++++++++------------------------------
> 1 file changed, 16 insertions(+), 32 deletions(-)
>
> diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs
> index 55a365af85a3..da54a70f8f1f 100644
> --- a/rust/kernel/time.rs
> +++ b/rust/kernel/time.rs
> @@ -31,59 +31,43 @@ pub fn msecs_to_jiffies(msecs: Msecs) -> Jiffies {
> unsafe { bindings::__msecs_to_jiffies(msecs) }
> }
>
> -/// A Rust wrapper around a `ktime_t`.
> +/// A specific point in time.
> #[repr(transparent)]
> #[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord)]
> -pub struct Ktime {
> +pub struct Instant {
> + // Range from 0 to `KTIME_MAX`.
> inner: bindings::ktime_t,
> }
>
> -impl Ktime {
> - /// Create a `Ktime` from a raw `ktime_t`.
> +impl Instant {
> + /// Create a `Instant` from a raw `ktime_t`.
> #[inline]
> - pub fn from_raw(inner: bindings::ktime_t) -> Self {
> + fn from_raw(inner: bindings::ktime_t) -> Self {
> Self { inner }
> }
Please keep this function public.
> /// Get the current time using `CLOCK_MONOTONIC`.
> #[inline]
> - pub fn ktime_get() -> Self {
> + pub fn now() -> Self {
> // SAFETY: It is always safe to call `ktime_get` outside of NMI context.
> Self::from_raw(unsafe { bindings::ktime_get() })
> }
>
> - /// Divide the number of nanoseconds by a compile-time constant.
> #[inline]
> - fn divns_constant<const DIV: i64>(self) -> i64 {
> - self.to_ns() / DIV
> - }
> -
> - /// Returns the number of nanoseconds.
> - #[inline]
> - pub fn to_ns(self) -> i64 {
> - self.inner
> - }
> -
> - /// Returns the number of milliseconds.
> - #[inline]
> - pub fn to_ms(self) -> i64 {
> - self.divns_constant::<NSEC_PER_MSEC>()
> + /// Return the amount of time elapsed since the `Instant`.
> + pub fn elapsed(&self) -> Delta {
Nit: This places the #[inline] marker before the documentation. Please
move it after to be consistent.
Alice