rust: time: add Delta::as_jiffies_unsigned()

Delta<Jiffy> is signed, but some C functions, such as
queue_delayed_work_on(), take a span in jiffies as unsigned long. So
each caller has to clamp a negative value to zero and cast it before
it passes the value to C. Add a helper that does both.

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-3-tomo@flapping.org
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
This commit is contained in:
FUJITA Tomonori 2026-10-02 14:04:26 +09:00 committed by Miguel Ojeda
parent cad3f4daef
commit daff0977b2

View File

@ -363,6 +363,27 @@ pub const fn from_jiffies(jiffies: isize) -> Self {
pub const fn as_jiffies(self) -> isize {
self.value
}
/// Return the number of jiffies in the [`Delta`] as an unsigned value.
///
/// If the number of jiffies is negative, zero is returned.
///
/// # Examples
///
/// ```
/// use kernel::time::Delta;
///
/// // A negative span returns zero.
/// assert_eq!(Delta::from_jiffies(-1).as_jiffies_unsigned(), 0);
///
/// // A positive span returns its value.
/// assert_eq!(Delta::from_jiffies(1).as_jiffies_unsigned(), 1);
/// ```
#[inline]
pub fn as_jiffies_unsigned(self) -> usize {
// CAST: `isize::max()` makes the value non-negative, so the cast keeps it.
isize::max(self.value, 0) as usize
}
}
impl ops::Add for Delta {