Re: [PATCH 2/2] rust: time: Implement basic arithmetic operations for Delta
From: Alexandre Courbot
Date: Thu Jul 24 2025 - 21:20:27 EST
On Fri Jul 25, 2025 at 3:54 AM JST, Lyude Paul wrote:
> While rvkms is only going to be using a few of these, since Deltas are
> basically the same as i64 it's easy enough to just implement all of the
> basic arithmetic operations for Delta types.
>
> Note that for division and remainders, we currently limit these operations
> to CONFIG_64BIT as u64 / u64 and u64 % u64 is not supported on all 32 bit
> platforms natively. The correct solution we want to aim for here in the
> future is to use the kernel's math library for performing these operations
> so they're emulated on 32 bit platforms.
>
> Signed-off-by: Lyude Paul <lyude@xxxxxxxxxx>
> ---
> rust/kernel/time.rs | 86 +++++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 86 insertions(+)
>
> diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs
> index ac5cab62070c6..8ece5a5d5a11b 100644
> --- a/rust/kernel/time.rs
> +++ b/rust/kernel/time.rs
> @@ -251,6 +251,92 @@ pub struct Delta {
> nanos: i64,
> }
>
> +impl ops::Add for Delta {
> + type Output = Self;
> +
> + fn add(self, rhs: Self) -> Self {
> + Self {
> + nanos: self.nanos + rhs.nanos,
Should we use saturating ops here as well?