Re: [PATCH net-next v2 1/6] rust: time: Implement PartialEq and PartialOrd for Ktime
From: Fiona Behrens
Date: Sun Oct 06 2024 - 06:29:33 EST
On 5 Oct 2024, at 14:25, FUJITA Tomonori wrote:
> Implement PartialEq and PartialOrd trait for Ktime by using C's
> ktime_compare function so two Ktime instances can be compared to
> determine whether a timeout is met or not.
Why is this only PartialEq/PartialOrd? Could we either document why or implement Eq/Ord as well?
>
> Signed-off-by: FUJITA Tomonori <fujita.tomonori@xxxxxxxxx>
> ---
> rust/helpers/helpers.c | 1 +
> rust/helpers/time.c | 8 ++++++++
> rust/kernel/time.rs | 22 ++++++++++++++++++++++
> 3 files changed, 31 insertions(+)
> create mode 100644 rust/helpers/time.c
>
> diff --git a/rust/helpers/helpers.c b/rust/helpers/helpers.c
> index 30f40149f3a9..c274546bcf78 100644
> --- a/rust/helpers/helpers.c
> +++ b/rust/helpers/helpers.c
> @@ -21,6 +21,7 @@
> #include "slab.c"
> #include "spinlock.c"
> #include "task.c"
> +#include "time.c"
> #include "uaccess.c"
> #include "wait.c"
> #include "workqueue.c"
> diff --git a/rust/helpers/time.c b/rust/helpers/time.c
> new file mode 100644
> index 000000000000..d6f61affb2c3
> --- /dev/null
> +++ b/rust/helpers/time.c
> @@ -0,0 +1,8 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +#include <linux/ktime.h>
> +
> +int rust_helper_ktime_compare(const ktime_t cmp1, const ktime_t cmp2)
> +{
> + return ktime_compare(cmp1, cmp2);
> +}
> diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs
> index e3bb5e89f88d..c40105941a2c 100644
> --- a/rust/kernel/time.rs
> +++ b/rust/kernel/time.rs
> @@ -81,3 +81,25 @@ fn sub(self, other: Ktime) -> Ktime {
> }
> }
> }
> +
> +impl PartialEq for Ktime {
> + #[inline]
> + fn eq(&self, other: &Self) -> bool {
> + // SAFETY: FFI call.
> + let ret = unsafe { bindings::ktime_compare(self.inner, other.inner) };
> + ret == 0
> + }
> +}
> +
> +impl PartialOrd for Ktime {
> + #[inline]
> + fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
> + // SAFETY: FFI call.
> + let ret = unsafe { bindings::ktime_compare(self.inner, other.inner) };
> + match ret {
> + 0 => Some(core::cmp::Ordering::Equal),
> + x if x < 0 => Some(core::cmp::Ordering::Less),
> + _ => Some(core::cmp::Ordering::Greater),
> + }
> + }
> +}
> --
> 2.34.1