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.
This commit is contained in:
Hanno Braun 2022-02-19 11:03:26 +01:00
parent 3a75c9ef92
commit 8f3d6f2178

View File

@ -1,4 +1,4 @@
use std::{cmp, f64::consts::PI, ops}; use std::{cmp, f64::consts::PI, hash::Hash, ops};
use approx::AbsDiffEq; 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<H: std::hash::Hasher>(&self, state: &mut H) {
self.0.to_bits().hash(state);
}
}
impl From<f32> for Scalar { impl From<f32> for Scalar {
fn from(scalar: f32) -> Self { fn from(scalar: f32) -> Self {
Self::from_f64(scalar as f64) Self::from_f64(scalar as f64)