From 1d6a514e85673b03bcb793dfc76155fd0f1b4791 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodolphe=20Br=C3=A9ard?= Date: Sun, 27 Oct 2024 20:02:42 +0100 Subject: [PATCH] Allow to compare and sort tasks --- src/task.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/task.rs b/src/task.rs index 8fac2b2..3b4c2e6 100644 --- a/src/task.rs +++ b/src/task.rs @@ -1,9 +1,10 @@ use pbkdf2::pbkdf2_hmac; use sha2::Sha256; +use std::cmp::Ordering; const DUMMY_SALT: &[u8] = b"some dummy salt"; -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Eq, PartialEq)] pub struct Task { pub data: &'static [u8], pub cost: u32, @@ -19,3 +20,15 @@ impl Task { pbkdf2_hmac::(self.data, DUMMY_SALT, self.cost, &mut key1); } } + +impl Ord for Task { + fn cmp(&self, other: &Self) -> Ordering { + self.cost.cmp(&other.cost) + } +} + +impl PartialOrd for Task { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +}