From 6f6378b2eade830a0fded6bd51c3c636f2c7412a Mon Sep 17 00:00:00 2001 From: ying tong Date: Thu, 8 Oct 2020 12:23:16 +0800 Subject: [PATCH] More idiomatic implementation of Ord for Field Co-authored-by: str4d --- src/arithmetic/fields/fp.rs | 17 ++++++++--------- src/arithmetic/fields/fq.rs | 17 ++++++++--------- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/src/arithmetic/fields/fp.rs b/src/arithmetic/fields/fp.rs index fd02695..1b99f04 100644 --- a/src/arithmetic/fields/fp.rs +++ b/src/arithmetic/fields/fp.rs @@ -66,15 +66,14 @@ impl std::cmp::Ord for Fp { fn cmp(&self, other: &Self) -> std::cmp::Ordering { let left = self.to_bytes(); let right = other.to_bytes(); - for (left_byte, right_byte) in left.iter().zip(right.iter()).rev() { - let tmp = left_byte.cmp(right_byte); - if let std::cmp::Ordering::Equal = tmp { - continue; - } else { - return tmp; - } - } - std::cmp::Ordering::Equal + left.iter() + .zip(right.iter()) + .rev() + .find_map(|(left_byte, right_byte)| match left_byte.cmp(right_byte) { + std::cmp::Ordering::Equal => None, + res => Some(res), + }) + .unwrap_or(std::cmp::Ordering::Equal) } } diff --git a/src/arithmetic/fields/fq.rs b/src/arithmetic/fields/fq.rs index 0f99629..11026de 100644 --- a/src/arithmetic/fields/fq.rs +++ b/src/arithmetic/fields/fq.rs @@ -66,15 +66,14 @@ impl std::cmp::Ord for Fq { fn cmp(&self, other: &Self) -> std::cmp::Ordering { let left = self.to_bytes(); let right = other.to_bytes(); - for (left_byte, right_byte) in left.iter().zip(right.iter()).rev() { - let tmp = left_byte.cmp(right_byte); - if let std::cmp::Ordering::Equal = tmp { - continue; - } else { - return tmp; - } - } - std::cmp::Ordering::Equal + left.iter() + .zip(right.iter()) + .rev() + .find_map(|(left_byte, right_byte)| match left_byte.cmp(right_byte) { + std::cmp::Ordering::Equal => None, + res => Some(res), + }) + .unwrap_or(std::cmp::Ordering::Equal) } }