summaryrefslogtreecommitdiff
path: root/rust
diff options
context:
space:
mode:
authorFUJITA Tomonori <fujita.tomonori@gmail.com>2026-10-02 14:04:26 +0900
committerMiguel Ojeda <ojeda@kernel.org>2026-10-12 07:31:20 +0200
commitdaff0977b2e36c777a00b4ca9fabc019834e60aa (patch)
tree309bab60707af83e608647b2076aafe73dfed825 /rust
parentcad3f4daef8a8793a953c632c2fcb0a6560da46a (diff)
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>
Diffstat (limited to 'rust')
-rw-r--r--rust/kernel/time.rs21
1 files changed, 21 insertions, 0 deletions
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 @@ impl Delta<Jiffy> {
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 {