diff options
| author | FUJITA Tomonori <fujita.tomonori@gmail.com> | 2026-10-02 14:04:25 +0900 |
|---|---|---|
| committer | Miguel Ojeda <ojeda@kernel.org> | 2026-10-12 07:31:20 +0200 |
| commit | cad3f4daef8a8793a953c632c2fcb0a6560da46a (patch) | |
| tree | 5d3be257f05c7ad6715df89348af9a1b0f9dd366 /rust | |
| parent | a12f22b2c9398541d75d86d18be0a9ec92909fe0 (diff) | |
rust: time: make Delta::ZERO generic over the time unit
Delta::ZERO is defined only for Delta<Nsec>, so code that needs a zero
Delta<Jiffy> has to write Delta::from_jiffies(0).
Define ZERO for every Delta<U> instead. An integer literal cannot be
used for a generic U::Repr, so add ZERO to TimeUnit.
Suggested-by: Markus Probst <markus.probst@posteo.de>
Suggested-by: Gary Guo <gary@garyguo.net>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
Acked-by: Andreas Hindborg <a.hindborg@kernel.org>
Reviewed-by: Gary Guo <gary@garyguo.net>
Link: https://patch.msgid.link/20261002050428.2746277-2-tomo@flapping.org
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
Diffstat (limited to 'rust')
| -rw-r--r-- | rust/kernel/time.rs | 13 |
1 files changed, 10 insertions, 3 deletions
diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs index 5ae377e70dd8..9f231bf25ead 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -306,6 +306,9 @@ mod private { pub trait TimeUnit: private::Sealed { /// The underlying representation of the time unit. type Repr: Copy + Clone + PartialEq + PartialOrd + Eq + Ord + core::fmt::Debug; + + /// The zero value of [`Self::Repr`]. + const ZERO: Self::Repr; } /// A time unit of nanoseconds. @@ -317,6 +320,7 @@ pub enum Nsec {} impl TimeUnit for Nsec { type Repr = i64; + const ZERO: Self::Repr = 0; } /// A time unit of jiffies. @@ -328,6 +332,7 @@ pub enum Jiffy {} impl TimeUnit for Jiffy { type Repr = isize; + const ZERO: Self::Repr = 0; } /// A span of time. @@ -341,6 +346,11 @@ pub struct Delta<U: TimeUnit = Nsec> { value: U::Repr, } +impl<U: TimeUnit> Delta<U> { + /// A span of time equal to zero. + pub const ZERO: Self = Self { value: U::ZERO }; +} + impl Delta<Jiffy> { /// Create a new [`Delta`] from a number of jiffies. #[inline] @@ -428,9 +438,6 @@ impl ops::Div for Delta { } impl Delta { - /// A span of time equal to zero. - pub const ZERO: Self = Self { value: 0 }; - /// Create a new [`Delta`] from a number of nanoseconds. #[inline] pub const fn from_nanos(nanos: i64) -> Self { |
