From daff0977b2e36c777a00b4ca9fabc019834e60aa Mon Sep 17 00:00:00 2001 From: FUJITA Tomonori Date: Fri, 2 Oct 2026 14:04:26 +0900 Subject: [PATCH] rust: time: add Delta::as_jiffies_unsigned() Delta 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 Signed-off-by: FUJITA Tomonori Acked-by: Andreas Hindborg Reviewed-by: Gary Guo Link: https://patch.msgid.link/20261002050428.2746277-3-tomo@flapping.org Signed-off-by: Miguel Ojeda --- rust/kernel/time.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs index 9f231bf25ead..b6a2aab12a50 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -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 {