From 8f3d6f21781a1fafff41eadd3f99c7668f2635b6 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Sat, 19 Feb 2022 11:03:26 +0100 Subject: [PATCH] Implement `Eq`, `Ord`, and `Hash` for `Scalar` These implementations aren't available for `f64` and other floating-point values. Providing them is the whole point of why `Scalar` exists. --- src/math/scalar.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/math/scalar.rs b/src/math/scalar.rs index 72a1df265..fecf8bf5b 100644 --- a/src/math/scalar.rs +++ b/src/math/scalar.rs @@ -1,4 +1,4 @@ -use std::{cmp, f64::consts::PI, ops}; +use std::{cmp, f64::consts::PI, hash::Hash, ops}; use approx::AbsDiffEq; @@ -84,6 +84,22 @@ impl Scalar { } } +impl Eq for Scalar {} + +impl Ord for Scalar { + fn cmp(&self, other: &Self) -> cmp::Ordering { + // Should never panic, as `from_f64` checks that the wrapped value is + // finite. + self.partial_cmp(&other).unwrap() + } +} + +impl Hash for Scalar { + fn hash(&self, state: &mut H) { + self.0.to_bits().hash(state); + } +} + impl From for Scalar { fn from(scalar: f32) -> Self { Self::from_f64(scalar as f64)