Rust for Linux List
 help / color / mirror / Atom feed
From: FUJITA Tomonori <tomo@flapping.org>
To: gary@garyguo.net
Cc: tomo@flapping.org, miguel.ojeda.sandonis@gmail.com,
	a.hindborg@kernel.org, ojeda@kernel.org, acourbot@nvidia.com,
	aliceryhl@google.com, anna-maria@linutronix.de,
	bjorn3_gh@protonmail.com, boqun@kernel.org, dakr@kernel.org,
	daniel.almeida@collabora.com, frederic@kernel.org,
	jstultz@google.com, lossin@kernel.org, lyude@redhat.com,
	sboyd@kernel.org, tamird@kernel.org, tglx@kernel.org,
	tmgross@umich.edu, work@onurozkan.dev,
	rust-for-linux@vger.kernel.org, fujita.tomonori@gmail.com
Subject: Re: [PATCH v1] rust: time: fix as_micros_ceil() to round correctly for negative Delta
Date: Tue, 14 Jul 2026 21:17:51 +0900 (JST)	[thread overview]
Message-ID: <20260714.211751.124079232269470730.tomo@flapping.org> (raw)
In-Reply-To: <DJY8NM7TIHZK.15213B4MWETHU@garyguo.net>

On Tue, 14 Jul 2026 11:56:58 +0100
"Gary Guo" <gary@garyguo.net> wrote:

> On Tue Jul 14, 2026 at 11:49 AM BST, FUJITA Tomonori wrote:
>> On Tue, 14 Jul 2026 12:38:03 +0200
>> Miguel Ojeda <miguel.ojeda.sandonis@gmail.com> wrote:
>>
>>> On Tue, Jul 14, 2026 at 12:16 PM FUJITA Tomonori <tomo@flapping.org> wrote:
>>>>
>>>> I can send a patch but as I recall, the last time I posted a patch
>>>> like that, Andreas was opposed to that kind approach?
>>>>
>>>> https://lore.kernel.org/rust-for-linux/20250701001809.496389-1-fujita.tomonori@gmail.com/
>>> 
>>> Ah, that explains why I had the feeling I already mentioned this in
>>> the past... Thanks for linking it! :)
>>> 
>>> My reading at the end of that thread was that he would open to it as
>>> long as the constants had a name or computed relative to something
>>> else, which is a good idea regardless, and I replied with agreement --
>>> so I don't think Andreas was completely opposed to doing so?
>>> 
>>> We should anyway be consistent about this treewide. What we have been
>>> doing (or at least my intention) was to document an example of each
>>> possible return path / edge case. That is, we don't want every single
>>> test in examples, but for instance here at the very least I would
>>> expect to see 1) a normal case, 2) a saturating case, 3) the behavior
>>> of that with negatives; but perhaps we don't want to have every single
>>> test we may have about each of those "groups".
>>
>> As I recall, the last time I posted a patch to make them constants,
>> Gary was opposed?
>>
>> https://lore.kernel.org/rust-for-linux/20260119233621.3465304-1-fujita.tomonori@gmail.com/
> 
> I'm okay having test cases for corner cases but they shouldn't be part of the
> API.

If I understand everyone's positions correctly:

- Andreas wanted the docs to avoid raw magic numbers.
- Gary didn't want to grow the public API with constants purely for
  documentation purposes.
- Miguel suggested doctests could show the boundary behavior, with the
  value declared locally inside the example.   

Here's a concreate attempt at from_micros():

diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs
index 363e93cbb139..a60a248a372b 100644
--- a/rust/kernel/time.rs
+++ b/rust/kernel/time.rs
@@ -388,6 +388,24 @@ pub const fn from_nanos(nanos: i64) -> Self {
     /// 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
+    ///
+    /// ```
+    /// use kernel::time::{Delta, NSEC_PER_USEC};
+    ///
+    /// assert_eq!(Delta::from_micros(1).as_nanos(), 1_000);
+    ///
+    /// // The boundary of the representable range: does not saturate.
+    /// const MAX_MICROS: i64 = 9_223_372_036_854_775;
+    /// assert_eq!(Delta::from_micros(MAX_MICROS).as_nanos(), MAX_MICROS * NSEC_PER_USEC);
+    ///
+    /// // One past the boundary: saturates.
+    /// assert_eq!(Delta::from_micros(MAX_MICROS + 1), Delta::from_nanos(i64::MAX));
+    ///
+    /// // Negative values.
+    /// assert_eq!(Delta::from_micros(-1).as_nanos(), -1_000);
+    /// ```
     #[inline]
     pub const fn from_micros(micros: i64) -> Self {
         Self {


Can we agree on something like this?

  reply	other threads:[~2026-07-14 12:18 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <N3Mcf328j6hi7HCJTp14HUC6-cxRUNgQLjvAdkFhIuZM-yYM91SYBYsW7kEVHRkqTzm1B3zgl5Bat4dpSIfXtA==@protonmail.internalid>
2026-07-13 22:52 ` [PATCH v1] rust: time: fix as_micros_ceil() to round correctly for negative Delta FUJITA Tomonori
2026-07-14  9:09   ` Miguel Ojeda
2026-07-14 10:16     ` FUJITA Tomonori
2026-07-14 10:38       ` Miguel Ojeda
2026-07-14 10:49         ` FUJITA Tomonori
2026-07-14 10:56           ` Gary Guo
2026-07-14 12:17             ` FUJITA Tomonori [this message]
2026-07-14 12:31               ` Miguel Ojeda
2026-07-14 12:57                 ` Alice Ryhl
2026-07-14 14:21                   ` Miguel Ojeda
2026-07-17 20:31                     ` Andreas Hindborg
2026-07-14 12:18           ` Miguel Ojeda
2026-07-17 20:26   ` Andreas Hindborg

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=20260714.211751.124079232269470730.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=miguel.ojeda.sandonis@gmail.com \
    --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