rust/time: Add Delta::from_nanos()
Subsystems (2)
Lyude Paul
reviewerPATCH 1/1 rust/time: Add Delta::from_nanos()
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.
--- rust/kernel/time.rs | 6 ++++++ 1 file changed, 6 insertions(+)
rust/kernel/time.rs +6 -0
@@ -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
Signature · 1 line
2.51.1
Onur Özkan
reviewerQuoted 14 lines · On Fri, 14 Nov 2025 13:42:06 -0500 Lyude Paul <[email protected]> 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 <[email protected]>--- rust/kernel/time.rs | 6 ++++++ 1 file changed, 6 insertions(+)rust/kernel/time.rs +0
-0
rust/kernel/time.rs impl Delta { · line 371
Quoted 9 lines
rust/kernel/time.rs +6
-0@@ -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?
Quoted 6 lines
rust/kernel/time.rs +0
-0/// 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
Quoted 11 lines · Onur Özkan
I wonder if it makes sense to remove all the
from_*functions fromDeltaand replace them all with something like this:pub const fn from_duration(duration: Duration) -> Self { Self { nanos: duration.as_nanos(), } }What do you think?
Deltas can be negative and only store the nanos -- i.e. there are many Durations that do not fit in a Delta (as_nanos() above returns u128).
Deltas 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 Durations.
Now, I agree that other types should not start getting things like nano parameters or from_nanos constructors -- they should take Deltas instead.
Cheers,
Miguel
Onur Özkan
reviewerQuoted 11 lines · On Sun, 16 Nov 2025 12:06:41 +0100 Miguel Ojeda…
Quoted 10 lines · Onur Özkan
I wonder if it makes sense to remove all the
from_*functions fromDeltaand replace them all with something like this:pub const fn from_duration(duration: Duration) -> Self { Self { nanos: duration.as_nanos(), } }
Quoted 6 lines
Quoted 1 line
What do you think?
Deltas can be negative and only store the nanos -- i.e. there are manyDurations that do not fit in aDelta(as_nanos()above returnsu128).
I didn't know, thanks :)
Quoted 8 lines
Deltas are similar toDurationin 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 useDurations.Now, I agree that other types should not start getting things like nano parameters or
from_nanosconstructors -- they should takeDeltas instead.
Yes, that seems like a good and simple improvement.
Regards,
Onur
Quoted 2 lines
Cheers,
Miguel
FUJITA Tomonori
reviewerQuoted 15 lines · On Fri, 14 Nov 2025 13:42:06 -0500 Lyude Paul <[email protected]> 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 <[email protected]>--- rust/kernel/time.rs | 6 ++++++ 1 file changed, 6 insertions(+)rust/kernel/time.rs +0
-0@@ -363,6 +363,12 @@ impl Delta {/// A span of time equal to zero. pub const ZERO :Self =Self { nanos:0 };
rust/kernel/time.rs impl Delta { · line 374
Quoted 10 lines
rust/kernel/time.rs +6
-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?
Andreas Hindborg
maintainerQuoted 19 lines · FUJITA Tomonori
Quoted 16 lines · On Fri, 14 Nov 2025 13:42:06 -0500 Lyude Paul <[email protected]> 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 <[email protected]>--- rust/kernel/time.rs | 6 ++++++ 1 file changed, 6 insertions(+)rust/kernel/time.rs +0
-0@@ -363,6 +363,12 @@ impl Delta {/// A span of time equal to zero. pub const ZERO :Self =Self { nanos:0 };
Quoted 12 lines
rust/kernel/time.rs impl Delta { · line 374
Quoted 9 lines
rust/kernel/time.rs +6
-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
FUJITA Tomonori
reviewerQuoted 21 lines · On Mon, 17 Nov 2025 12:15:53 +0100 Andreas Hindborg <[email protected]>…
Quoted 8 lines
Quoted 8 lines
rust/kernel/time.rs +0
-0@@ -363,6 +363,12 @@ impl Delta {/// A span of time equal to zero. pub const ZERO :Self =Self { nanos:0 };Quoted 12 lines
rust/kernel/time.rs impl Delta { · line 374
Quoted 9 lines
rust/kernel/time.rs +6
-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?
Quoted 1 line
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?
rust/kernel/time.rs +6 -1
@@ -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]
Andreas Hindborg
maintainerQuoted 39 lines · FUJITA Tomonori
Quoted 21 lines · On Mon, 17 Nov 2025 12:15:53 +0100 Andreas Hindborg <[email protected]>…
Quoted 8 lines
Quoted 8 lines
rust/kernel/time.rs +0
-0@@ -363,6 +363,12 @@ impl Delta {/// A span of time equal to zero. pub const ZERO :Self =Self { nanos:0 };Quoted 12 lines
rust/kernel/time.rs impl Delta { · line 374
Quoted 9 lines
rust/kernel/time.rs +6
-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?
Quoted 1 line
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?
rust/kernel/time.rs +2
-0@@ -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 ;
rust/kernel/time.rs impl Delta { · line 376
Quoted 10 lines
rust/kernel/time.rs +4
-1+/// 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
Andreas Hindborg
maintainerQuoted 19 lines · FUJITA Tomonori
Quoted 16 lines · On Fri, 14 Nov 2025 13:42:06 -0500 Lyude Paul <[email protected]> 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 <[email protected]>--- rust/kernel/time.rs | 6 ++++++ 1 file changed, 6 insertions(+)rust/kernel/time.rs +0
-0@@ -363,6 +363,12 @@ impl Delta {/// A span of time equal to zero. pub const ZERO :Self =Self { nanos:0 };
Quoted 12 lines
rust/kernel/time.rs impl Delta { · line 374
Quoted 9 lines
rust/kernel/time.rs +6
-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?
On second thought, since valid range is any valid i64 value, I don't think this matters.
Best regards,
Andreas Hindborg
Quoted 4 lines · Lyude Paul
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.
Applied, thanks!
[1/1] rust/time: Add Delta::from_nanos()
commit: c51866f65b8ac37b8883a2e80ada13c8cd4d2f7b
Best regards,
Signature · 1 line
Andreas Hindborg <[email protected]>