[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20250702.180922.1185854079687770335.fujita.tomonori@gmail.com>
Date: Wed, 02 Jul 2025 18:09:22 +0900 (JST)
From: FUJITA Tomonori <fujita.tomonori@...il.com>
To: a.hindborg@...nel.org, ojeda@...nel.org
Cc: fujita.tomonori@...il.com, alex.gaynor@...il.com, aliceryhl@...gle.com,
anna-maria@...utronix.de, bjorn3_gh@...tonmail.com, boqun.feng@...il.com,
dakr@...nel.org, frederic@...nel.org, gary@...yguo.net,
jstultz@...gle.com, linux-kernel@...r.kernel.org, lossin@...nel.org,
lyude@...hat.com, rust-for-linux@...r.kernel.org, sboyd@...nel.org,
tglx@...utronix.de, tmgross@...ch.edu
Subject: Re: [PATCH v1] rust: time: Add examples with doctest for Delta
On Wed, 02 Jul 2025 10:36:19 +0200
Andreas Hindborg <a.hindborg@...nel.org> wrote:
> "FUJITA Tomonori" <fujita.tomonori@...il.com> writes:
>
>> Add examples with doctest for Delta methods and associated
>> functions. These examples explicitly test overflow and saturation
>> behavior.
>>
>> Signed-off-by: FUJITA Tomonori <fujita.tomonori@...il.com>
>> ---
>> rust/kernel/time.rs | 67 +++++++++++++++++++++++++++++++++++++++++++++
>> 1 file changed, 67 insertions(+)
>>
>> diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs
>> index 64c8dcf548d6..c6322275115a 100644
>> --- a/rust/kernel/time.rs
>> +++ b/rust/kernel/time.rs
>> @@ -228,11 +228,34 @@ impl Delta {
>> /// A span of time equal to zero.
>> pub const ZERO: Self = Self { nanos: 0 };
>>
>> + /// Create a new [`Delta`] from a number of nanoseconds.
>> + ///
>> + /// The `nanos` can range from [`i64::MIN`] to [`i64::MAX`].
>> + #[inline]
>> + pub const fn from_nanos(nanos: i64) -> Self {
>> + Self { nanos }
>> + }
>> +
>> /// Create a new [`Delta`] from a number of microseconds.
>> ///
>> /// The `micros` can range from -9_223_372_036_854_775 to 9_223_372_036_854_775.
>> /// If `micros` is outside this range, `i64::MIN` is used for negative values,
>> /// and `i64::MAX` is used for positive values due to saturation.
>> + ///
>> + /// # Examples
>> + ///
>> + /// ```
>> + /// let delta = kernel::time::Delta::from_micros(5);
>> + /// assert_eq!(delta.as_nanos(), 5_000);
>> + /// let delta = kernel::time::Delta::from_micros(9_223_372_036_854_775);
>> + /// assert_eq!(delta.as_nanos(), 9_223_372_036_854_775_000);
>> + /// let delta = kernel::time::Delta::from_micros(9_223_372_036_854_776);
>> + /// assert_eq!(delta.as_nanos(), i64::MAX);
>> + /// let delta = kernel::time::Delta::from_micros(-9_223_372_036_854_775);
>> + /// assert_eq!(delta.as_nanos(), -9_223_372_036_854_775_000);
>> + /// let delta = kernel::time::Delta::from_micros(-9_223_372_036_854_776);
>> + /// assert_eq!(delta.as_nanos(), i64::MIN);
>> + /// ```
>
>
> I think this kind of test would be better suited for the new `#[test]`
> macro. Would you agree?
I think that Miguel suggested adding examples but either is fine by me:
https://lore.kernel.org/lkml/CANiq72kiTwpcH6S0XaTEBnLxqyJ6EDVLoZPi9X+MWkanK5wq=w@mail.gmail.com/
Powered by blists - more mailing lists