[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20250416.181050.897424811905103869.fujita.tomonori@gmail.com>
Date: Wed, 16 Apr 2025 18:10:50 +0900 (JST)
From: FUJITA Tomonori <fujita.tomonori@...il.com>
To: lyude@...hat.com
Cc: rust-for-linux@...r.kernel.org, a.hindborg@...nel.org,
linux-kernel@...r.kernel.org, boqun.feng@...il.com,
fujita.tomonori@...il.com, frederic@...nel.org, tglx@...utronix.de,
anna-maria@...utronix.de, jstultz@...gle.com, sboyd@...nel.org,
ojeda@...nel.org, alex.gaynor@...il.com, gary@...yguo.net,
bjorn3_gh@...tonmail.com, benno.lossin@...ton.me, aliceryhl@...gle.com,
tmgross@...ch.edu, dakr@...nel.org
Subject: Re: [PATCH v2 5/8] rust: time: Add Instant::from_nanos()
On Tue, 15 Apr 2025 15:48:26 -0400
Lyude Paul <lyude@...hat.com> wrote:
> For implementing Rust bindings which can return a point in time.
>
> Signed-off-by: Lyude Paul <lyude@...hat.com>
> ---
> rust/kernel/time.rs | 8 ++++++++
> 1 file changed, 8 insertions(+)
>
> diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs
> index 8d6aa88724ad8..545963140f180 100644
> --- a/rust/kernel/time.rs
> +++ b/rust/kernel/time.rs
> @@ -83,6 +83,14 @@ pub fn elapsed(&self) -> Delta {
> pub(crate) fn as_nanos(self) -> i64 {
> self.inner
> }
> +
> + #[expect(unused)]
> + #[inline]
> + pub(crate) fn from_nanos(nanos: i64) -> Self {
> + Self {
> + inner: nanos as bindings::ktime_t,
> + }
> + }
> }
We need to guarantee the following Invariants.
/// A specific point in time.
///
/// # Invariants
///
/// The `inner` value is in the range from 0 to `KTIME_MAX`.
#[repr(transparent)]
#[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord)]
pub struct Instant {
inner: bindings::ktime_t,
}
Otherwise, The method of the Sub trait may cause an overflow
By the way, what are some use cases for creating an Instant from
driver's input?
Powered by blists - more mailing lists