rust: time: add jiffies time unit for Delta

Add a Jiffy time unit with isize as its representation and provide
Delta<Jiffy>::from_jiffies() and as_jiffies() as the unit-specific
constructor and accessor, mirroring from_nanos()/as_nanos() on the
nanosecond Delta.

Represent the jiffies span as isize: Delta is a signed span (nanoseconds
use i64) and, as a timeout, the value only needs to reach
MAX_JIFFY_OFFSET ((LONG_MAX >> 1) - 1). isize is signed and matches the
kernel's c_long, so it meets both.

Reviewed-by: Gary Guo <gary@garyguo.net>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
Reviewed-by: Andreas Hindborg <a.hindborg@kernel.org>
Link: https://patch.msgid.link/20260808062839.1159990-3-tomo@flapping.org
[ Added intra-doc links. - Miguel ]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
This commit is contained in:
FUJITA Tomonori 2026-08-08 15:28:34 +09:00 committed by Miguel Ojeda
parent 2e2203d885
commit 2e0d41002a

View File

@ -295,6 +295,7 @@ mod private {
pub trait Sealed {}
impl Sealed for super::Nsec {}
impl Sealed for super::Jiffy {}
}
/// A trait for time units.
@ -314,6 +315,17 @@ impl TimeUnit for Nsec {
type Repr = i64;
}
/// A time unit of jiffies.
///
/// A [`Delta<Jiffy>`] stores its value as [`isize`] jiffies and can represent
/// any [`isize`] value, including negative, zero, and positive numbers.
#[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Debug)]
pub enum Jiffy {}
impl TimeUnit for Jiffy {
type Repr = isize;
}
/// A span of time.
///
/// The span is stored in the unit given by the type parameter `U` (see
@ -325,6 +337,20 @@ pub struct Delta<U: TimeUnit = Nsec> {
value: U::Repr,
}
impl Delta<Jiffy> {
/// Create a new [`Delta`] from a number of jiffies.
#[inline]
pub const fn from_jiffies(jiffies: isize) -> Self {
Self { value: jiffies }
}
/// Return the number of jiffies in the [`Delta`].
#[inline]
pub const fn as_jiffies(self) -> isize {
self.value
}
}
impl ops::Add for Delta {
type Output = Self;