* [PATCH] rust/time: Add Delta::from_nanos()
@ 2025-11-14 18:42 Lyude Paul
2025-11-16 6:38 ` Onur Özkan
2025-11-17 1:39 ` FUJITA Tomonori
0 siblings, 2 replies; 8+ messages in thread
From: Lyude Paul @ 2025-11-14 18:42 UTC (permalink / raw)
To: rust-for-linux, linux-kernel
Cc: Andreas Hindborg, Boqun Feng, FUJITA Tomonori,
Frederic Weisbecker, Thomas Gleixner, Anna-Maria Behnsen,
John Stultz, Stephen Boyd, Miguel Ojeda, Alex Gaynor, Gary Guo,
Björn Roy Baron, Benno Lossin, Alice Ryhl, Trevor Gross,
Danilo Krummrich
Since rvkms is going to need to create its own Delta instances, and we
already have functions for creating Delta with every other unit of time.
Signed-off-by: Lyude Paul <lyude@redhat.com>
---
rust/kernel/time.rs | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs
index 6ea98dfcd0278..2b096e5a61cda 100644
--- a/rust/kernel/time.rs
+++ b/rust/kernel/time.rs
@@ -363,6 +363,12 @@ 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.
+ #[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.
base-commit: 5935461b458463ee51aac8d95c25d7a5e1de8c4d
--
2.51.1
^ permalink raw reply related [flat|nested] 8+ messages in thread* Re: [PATCH] rust/time: Add Delta::from_nanos()
2025-11-14 18:42 [PATCH] rust/time: Add Delta::from_nanos() Lyude Paul
@ 2025-11-16 6:38 ` Onur Özkan
2025-11-16 11:06 ` Miguel Ojeda
2025-11-17 1:39 ` FUJITA Tomonori
1 sibling, 1 reply; 8+ messages in thread
From: Onur Özkan @ 2025-11-16 6:38 UTC (permalink / raw)
To: Lyude Paul
Cc: rust-for-linux, linux-kernel, Andreas Hindborg, Boqun Feng,
FUJITA Tomonori, Frederic Weisbecker, Thomas Gleixner,
Anna-Maria Behnsen, John Stultz, Stephen Boyd, Miguel Ojeda,
Alex Gaynor, Gary Guo, Björn Roy Baron, Benno Lossin,
Alice Ryhl, Trevor Gross, Danilo Krummrich
On Fri, 14 Nov 2025 13:42:06 -0500
Lyude Paul <lyude@redhat.com> wrote:
> Since rvkms is going to need to create its own Delta instances, and we
> already have functions for creating Delta with every other unit of
> time.
>
> Signed-off-by: Lyude Paul <lyude@redhat.com>
> ---
> rust/kernel/time.rs | 6 ++++++
> 1 file changed, 6 insertions(+)
>
> diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs
> index 6ea98dfcd0278..2b096e5a61cda 100644
> --- a/rust/kernel/time.rs
> +++ b/rust/kernel/time.rs
> @@ -363,6 +363,12 @@ 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.
> + #[inline]
> + pub const fn from_nanos(nanos: i64) -> Self {
> + Self { nanos }
> + }
> +
I wonder if it makes sense to remove all the `from_*` functions from
`Delta` and replace them all with something like this:
pub const fn from_duration(duration: Duration) -> Self {
Self {
nanos: duration.as_nanos(),
}
}
What do you think?
> /// 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.
>
> base-commit: 5935461b458463ee51aac8d95c25d7a5e1de8c4d
^ permalink raw reply [flat|nested] 8+ messages in thread* Re: [PATCH] rust/time: Add Delta::from_nanos()
2025-11-16 6:38 ` Onur Özkan
@ 2025-11-16 11:06 ` Miguel Ojeda
2025-11-17 10:21 ` Onur Özkan
0 siblings, 1 reply; 8+ messages in thread
From: Miguel Ojeda @ 2025-11-16 11:06 UTC (permalink / raw)
To: Onur Özkan
Cc: Lyude Paul, rust-for-linux, linux-kernel, Andreas Hindborg,
Boqun Feng, FUJITA Tomonori, Frederic Weisbecker, Thomas Gleixner,
Anna-Maria Behnsen, John Stultz, Stephen Boyd, Miguel Ojeda,
Alex Gaynor, Gary Guo, Björn Roy Baron, Benno Lossin,
Alice Ryhl, Trevor Gross, Danilo Krummrich
On Sun, Nov 16, 2025 at 7:38 AM Onur Özkan <work@onurozkan.dev> wrote:
>
> I wonder if it makes sense to remove all the `from_*` functions from
> `Delta` and replace them all with something like this:
>
> pub const fn from_duration(duration: Duration) -> Self {
> Self {
> nanos: duration.as_nanos(),
> }
> }
>
> What do you think?
`Delta`s can be negative and only store the nanos -- i.e. there are
many `Duration`s that do not fit in a `Delta` (`as_nanos()` above
returns `u128`).
`Delta`s are similar to `Duration` in the sense that they both store a
span of time. It seems reasonable to support similar constructors,
since they perform similar roles and we may not use `Duration`s.
Now, I agree that other types should not start getting things like
nano parameters or `from_nanos` constructors -- they should take
`Delta`s instead.
Cheers,
Miguel
^ permalink raw reply [flat|nested] 8+ messages in thread* Re: [PATCH] rust/time: Add Delta::from_nanos()
2025-11-16 11:06 ` Miguel Ojeda
@ 2025-11-17 10:21 ` Onur Özkan
0 siblings, 0 replies; 8+ messages in thread
From: Onur Özkan @ 2025-11-17 10:21 UTC (permalink / raw)
To: Miguel Ojeda
Cc: Lyude Paul, rust-for-linux, linux-kernel, Andreas Hindborg,
Boqun Feng, FUJITA Tomonori, Frederic Weisbecker, Thomas Gleixner,
Anna-Maria Behnsen, John Stultz, Stephen Boyd, Miguel Ojeda,
Alex Gaynor, Gary Guo, Björn Roy Baron, Benno Lossin,
Alice Ryhl, Trevor Gross, Danilo Krummrich
On Sun, 16 Nov 2025 12:06:41 +0100
Miguel Ojeda <miguel.ojeda.sandonis@gmail.com> wrote:
> On Sun, Nov 16, 2025 at 7:38 AM Onur Özkan <work@onurozkan.dev> wrote:
> >
> > I wonder if it makes sense to remove all the `from_*` functions from
> > `Delta` and replace them all with something like this:
> >
> > pub const fn from_duration(duration: Duration) -> Self {
> > Self {
> > nanos: duration.as_nanos(),
> > }
> > }
> >
> > What do you think?
>
> `Delta`s can be negative and only store the nanos -- i.e. there are
> many `Duration`s that do not fit in a `Delta` (`as_nanos()` above
> returns `u128`).
>
I didn't know, thanks :)
> `Delta`s are similar to `Duration` in the sense that they both store a
> span of time. It seems reasonable to support similar constructors,
> since they perform similar roles and we may not use `Duration`s.
>
> Now, I agree that other types should not start getting things like
> nano parameters or `from_nanos` constructors -- they should take
> `Delta`s instead.
>
Yes, that seems like a good and simple improvement.
Regards,
Onur
> Cheers,
> Miguel
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] rust/time: Add Delta::from_nanos()
2025-11-14 18:42 [PATCH] rust/time: Add Delta::from_nanos() Lyude Paul
2025-11-16 6:38 ` Onur Özkan
@ 2025-11-17 1:39 ` FUJITA Tomonori
2025-11-17 11:15 ` Andreas Hindborg
1 sibling, 1 reply; 8+ messages in thread
From: FUJITA Tomonori @ 2025-11-17 1:39 UTC (permalink / raw)
To: lyude
Cc: rust-for-linux, linux-kernel, a.hindborg, boqun.feng,
fujita.tomonori, frederic, tglx, anna-maria, jstultz, sboyd,
ojeda, alex.gaynor, gary, bjorn3_gh, lossin, aliceryhl, tmgross,
dakr
On Fri, 14 Nov 2025 13:42:06 -0500
Lyude Paul <lyude@redhat.com> wrote:
> Since rvkms is going to need to create its own Delta instances, and we
> already have functions for creating Delta with every other unit of time.
>
> Signed-off-by: Lyude Paul <lyude@redhat.com>
> ---
> rust/kernel/time.rs | 6 ++++++
> 1 file changed, 6 insertions(+)
>
> diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs
> index 6ea98dfcd0278..2b096e5a61cda 100644
> --- a/rust/kernel/time.rs
> +++ b/rust/kernel/time.rs
> @@ -363,6 +363,12 @@ 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.
> + #[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.
For consistency with the other methods, perhaps we should also mention
the valid nanos range in the comment?
^ permalink raw reply [flat|nested] 8+ messages in thread* Re: [PATCH] rust/time: Add Delta::from_nanos()
2025-11-17 1:39 ` FUJITA Tomonori
@ 2025-11-17 11:15 ` Andreas Hindborg
2025-11-17 11:38 ` FUJITA Tomonori
0 siblings, 1 reply; 8+ messages in thread
From: Andreas Hindborg @ 2025-11-17 11:15 UTC (permalink / raw)
To: FUJITA Tomonori, lyude
Cc: rust-for-linux, linux-kernel, boqun.feng, fujita.tomonori,
frederic, tglx, anna-maria, jstultz, sboyd, ojeda, alex.gaynor,
gary, bjorn3_gh, lossin, aliceryhl, tmgross, dakr
"FUJITA Tomonori" <fujita.tomonori@gmail.com> writes:
> On Fri, 14 Nov 2025 13:42:06 -0500
> Lyude Paul <lyude@redhat.com> wrote:
>
>> Since rvkms is going to need to create its own Delta instances, and we
>> already have functions for creating Delta with every other unit of time.
>>
>> Signed-off-by: Lyude Paul <lyude@redhat.com>
>> ---
>> rust/kernel/time.rs | 6 ++++++
>> 1 file changed, 6 insertions(+)
>>
>> diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs
>> index 6ea98dfcd0278..2b096e5a61cda 100644
>> --- a/rust/kernel/time.rs
>> +++ b/rust/kernel/time.rs
>> @@ -363,6 +363,12 @@ 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.
>> + #[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.
>
> For consistency with the other methods, perhaps we should also mention
> the valid nanos range in the comment?
But, please make it a constant instead of an integer literal.
Best regards,
Andreas Hindborg
^ permalink raw reply [flat|nested] 8+ messages in thread* Re: [PATCH] rust/time: Add Delta::from_nanos()
2025-11-17 11:15 ` Andreas Hindborg
@ 2025-11-17 11:38 ` FUJITA Tomonori
2025-11-17 12:26 ` Andreas Hindborg
0 siblings, 1 reply; 8+ messages in thread
From: FUJITA Tomonori @ 2025-11-17 11:38 UTC (permalink / raw)
To: a.hindborg
Cc: fujita.tomonori, lyude, rust-for-linux, linux-kernel, boqun.feng,
frederic, tglx, anna-maria, jstultz, sboyd, ojeda, alex.gaynor,
gary, bjorn3_gh, lossin, aliceryhl, tmgross, dakr
On Mon, 17 Nov 2025 12:15:53 +0100
Andreas Hindborg <a.hindborg@kernel.org> wrote:
>>> diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs
>>> index 6ea98dfcd0278..2b096e5a61cda 100644
>>> --- a/rust/kernel/time.rs
>>> +++ b/rust/kernel/time.rs
>>> @@ -363,6 +363,12 @@ 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.
>>> + #[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.
>>
>> For consistency with the other methods, perhaps we should also mention
>> the valid nanos range in the comment?
>
> But, please make it a constant instead of an integer literal.
Do you mean making the comment of Delta's from_* methods like the
following?
diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs
index 6ea98dfcd027..822746e552fb 100644
--- a/rust/kernel/time.rs
+++ b/rust/kernel/time.rs
@@ -363,9 +363,14 @@ impl Delta {
/// A span of time equal to zero.
pub const ZERO: Self = Self { nanos: 0 };
+ /// The minimum number of microseconds that can be represented by a [`Delta`].
+ pub const MIN_MICROS: i64 = -9_223_372_036_854_775;
+ /// The maximum number of microseconds that can be represented by a [`Delta`].
+ pub const MAX_MICROS: i64 = 9_223_372_036_854_775;
+
/// 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.
+ /// The `micros` can range from [`Delta::MIN_MICROS`] to [`Delta::MAX_MICROS`].
/// If `micros` is outside this range, `i64::MIN` is used for negative values,
/// and `i64::MAX` is used for positive values due to saturation.
#[inline]
^ permalink raw reply related [flat|nested] 8+ messages in thread* Re: [PATCH] rust/time: Add Delta::from_nanos()
2025-11-17 11:38 ` FUJITA Tomonori
@ 2025-11-17 12:26 ` Andreas Hindborg
0 siblings, 0 replies; 8+ messages in thread
From: Andreas Hindborg @ 2025-11-17 12:26 UTC (permalink / raw)
To: FUJITA Tomonori
Cc: fujita.tomonori, lyude, rust-for-linux, linux-kernel, boqun.feng,
frederic, tglx, anna-maria, jstultz, sboyd, ojeda, alex.gaynor,
gary, bjorn3_gh, lossin, aliceryhl, tmgross, dakr
"FUJITA Tomonori" <fujita.tomonori@gmail.com> writes:
> On Mon, 17 Nov 2025 12:15:53 +0100
> Andreas Hindborg <a.hindborg@kernel.org> wrote:
>
>>>> diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs
>>>> index 6ea98dfcd0278..2b096e5a61cda 100644
>>>> --- a/rust/kernel/time.rs
>>>> +++ b/rust/kernel/time.rs
>>>> @@ -363,6 +363,12 @@ 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.
>>>> + #[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.
>>>
>>> For consistency with the other methods, perhaps we should also mention
>>> the valid nanos range in the comment?
>>
>> But, please make it a constant instead of an integer literal.
>
> Do you mean making the comment of Delta's from_* methods like the
> following?
>
> diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs
> index 6ea98dfcd027..822746e552fb 100644
> --- a/rust/kernel/time.rs
> +++ b/rust/kernel/time.rs
> @@ -363,9 +363,14 @@ impl Delta {
> /// A span of time equal to zero.
> pub const ZERO: Self = Self { nanos: 0 };
>
> + /// The minimum number of microseconds that can be represented by a [`Delta`].
> + pub const MIN_MICROS: i64 = -9_223_372_036_854_775;
> + /// The maximum number of microseconds that can be represented by a [`Delta`].
> + pub const MAX_MICROS: i64 = 9_223_372_036_854_775;
> +
> /// 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.
> + /// The `micros` can range from [`Delta::MIN_MICROS`] to [`Delta::MAX_MICROS`].
> /// If `micros` is outside this range, `i64::MIN` is used for negative values,
> /// and `i64::MAX` is used for positive values due to saturation.
> #[inline]
Yes, I would prefer that. The numbers become much more useful like this.
Best regards,
Andreas Hindborg
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2025-11-17 12:26 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-14 18:42 [PATCH] rust/time: Add Delta::from_nanos() Lyude Paul
2025-11-16 6:38 ` Onur Özkan
2025-11-16 11:06 ` Miguel Ojeda
2025-11-17 10:21 ` Onur Özkan
2025-11-17 1:39 ` FUJITA Tomonori
2025-11-17 11:15 ` Andreas Hindborg
2025-11-17 11:38 ` FUJITA Tomonori
2025-11-17 12:26 ` Andreas Hindborg
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).