mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Alice Ryhl <aliceryhl@google.com>
To: lina@asahilina.net
Cc: alex.gaynor@gmail.com, asahi@lists.linux.dev,
	bjorn3_gh@protonmail.com, boqun.feng@gmail.com, gary@garyguo.net,
	gbs@canishe.com, heghedus.razvan@protonmail.com,
	jistone@redhat.com, jstultz@google.com,
	linux-kernel@vger.kernel.org, ojeda@kernel.org,
	rust-for-linux@vger.kernel.org, sboyd@kernel.org,
	tglx@linutronix.de, wedsonaf@gmail.com
Subject: Re: [PATCH v2] rust: time: New module for timekeeping functions
Date: Fri, 14 Jul 2023 10:05:25 +0000	[thread overview]
Message-ID: <20230714100525.2192448-1-aliceryhl@google.com> (raw)
In-Reply-To: <20230714-rust-time-v2-1-f5aed84218c4@asahilina.net>

Asahi Lina <lina@asahilina.net> writes:
> +/// Marker trait for clock sources that represent a calendar (wall clock)
> +/// relative to the UNIX epoch.
> +pub trait WallTime {}

What's the purpose of this trait? Perhaps it should have a method to get
the UNIX epoch as an Instant?

> +    /// Returns the time elapsed since an earlier Instant<t>, or
> +    /// None if the argument is a later Instant.
> +    pub fn since(&self, earlier: Instant<T>) -> Option<Duration> {
> +        if earlier.nanoseconds > self.nanoseconds {
> +            None
> +        } else {
> +            // Casting to u64 and subtracting is guaranteed to give the right
> +            // result for all inputs, as long as the condition we checked above
> +            // holds.
> +            Some(Duration::from_nanos(
> +                self.nanoseconds as u64 - earlier.nanoseconds as u64,
> +            ))

It looks like you intend to use wrapping semantics for this subtraction
so that self=1,earlier=-1 results in a difference of two.

In that case, you should explicitly use `.wrapping_sub` instead to
convey your intent.

I guess you could also use `abs_diff`, which takes two i64s and returns
an u64.

> +/// Contains the various clock source types available to the kernel.
> +pub mod clock {
> +    use super::*;
> +
> +    /// A clock representing the default kernel time source.
> +    ///
> +    /// This is `CLOCK_MONOTONIC` (though it is not the only
> +    /// monotonic clock) and also the default clock used by
> +    /// `ktime_get()` in the C API.
> +    ///
> +    /// This is like `BootTime`, but does not include time
> +    /// spent sleeping.
> +
> +    pub struct KernelTime;
> +
> +    impl Clock for KernelTime {}
> +    impl Monotonic for KernelTime {}
> +    impl Now for KernelTime {
> +        fn now() -> Instant<Self> {
> +            Instant::<Self>::new(unsafe { bindings::ktime_get() })
> +        }
> +    }

We usually add a SAFETY comment even if it is trivial.

// SAFETY: Just an FFI call without any safety requirements.

Alice


  reply	other threads:[~2023-07-14 10:05 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-14  7:55 Asahi Lina
2023-07-14 10:05 ` Alice Ryhl [this message]
2023-07-15 16:59   ` Miguel Ojeda
2023-07-14 13:58 ` Martin Rodriguez Reboredo
2023-07-14 17:15 ` Boqun Feng
2023-07-15  1:17 ` Thomas Gleixner
2023-07-15  8:14   ` Alice Ryhl
2023-07-16  9:20   ` Asahi Lina
2023-07-15 15:33 ` Gary Guo
2023-07-19 12:17 ` Heghedus Razvan

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=20230714100525.2192448-1-aliceryhl@google.com \
    --to=aliceryhl@google.com \
    --cc=alex.gaynor@gmail.com \
    --cc=asahi@lists.linux.dev \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=gary@garyguo.net \
    --cc=gbs@canishe.com \
    --cc=heghedus.razvan@protonmail.com \
    --cc=jistone@redhat.com \
    --cc=jstultz@google.com \
    --cc=lina@asahilina.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=sboyd@kernel.org \
    --cc=tglx@linutronix.de \
    --cc=wedsonaf@gmail.com \
    /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

all inboxes | Powered by JetHome®