alloy/cmp.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
//! A module containing comparator definitions used throughout Alloy.
//!
//! This is analogous to the [`std::cmp`] crate, with extensions that are
//! only sensible for graphics management.
/// Trait for if something is "near" within a specified tolerance.
///
/// # Examples
///
/// Basic use:
///
/// ```rust
/// # use alloy::cmp::Near;
/// pub struct MyFloat(f32);
///
/// impl Near for MyFloat {
/// fn near(&self, other: &MyFloat, tolerance: &f32) -> bool {
/// self.0.near(&other.0, tolerance)
/// }
/// }
///
/// # fn test() {
/// assert!(&MyFloat(0.0001).near(&MyFloat(0.00011), &0.0001));
/// assert!(!&MyFloat(0.0001).near(&MyFloat(0.0011), &0.000001));
/// # }
/// ```
pub trait Near<Rhs: ?Sized = Self, Tolerance = f32> {
/// Tests whether `self` is "near" `other` relative top the specified
/// `tolerance`
///
/// # Parameters
///
/// * `other` - the other value to test against
/// * `tolerance` - the tolerance being tested
fn near(&self, other: &Rhs, tolerance: &Tolerance) -> bool;
}
impl Near for f32 {
fn near(&self, other: &Self, distance: &f32) -> bool {
(self - other).abs() <= *distance
}
}
impl<T, U> Near<[T], U> for [T]
where
T: Near<T, U>,
{
fn near(&self, other: &Self, tolerance: &U) -> bool {
if self.len() != other.len() {
return false;
}
for (lhs, rhs) in self.iter().zip(other.iter()) {
if !lhs.near(rhs, tolerance) {
return false;
}
}
true
}
}
impl<const N: usize, T, U> Near<[T; N], U> for [T; N]
where
T: Near<T, U>,
{
fn near(&self, other: &Self, tolerance: &U) -> bool {
for i in 0..N {
// SAFETY: The size is bound between [0, N)
if !unsafe {
self
.get_unchecked(i)
.near(other.get_unchecked(i), tolerance)
} {
return false;
}
}
true
}
}
impl Near<f32, f64> for f32 {
fn near(&self, other: &Self, distance: &f64) -> bool {
(self - other).abs() as f64 <= *distance
}
}
impl Near<f64, f64> for f64 {
fn near(&self, other: &Self, distance: &f64) -> bool {
(self - other).abs() <= *distance
}
}
impl Near<f64, f32> for f64 {
fn near(&self, other: &Self, distance: &f32) -> bool {
(self - other).abs() <= *distance as f64
}
}
/// Trait for comparing objects for near-equality relative to a default tolerance.
///
/// This is effectively a short-hand for `Near` with a default `f32` tolerance.
pub trait AlmostEq<Rhs: ?Sized = Self>: Near<Rhs> {
/// Tests whether `self` is almost equal to `other`
///
/// # Parameters
///
/// * `other` - the other value to compare against
fn almost_eq(&self, other: &Rhs) -> bool;
}
impl AlmostEq for f32 {
fn almost_eq(&self, other: &Self) -> bool {
self.near(other, &f32::EPSILON)
}
}
impl AlmostEq for f64 {
fn almost_eq(&self, other: &Self) -> bool {
self.near(other, &f64::EPSILON)
}
}
impl<T> AlmostEq<[T]> for [T]
where
T: AlmostEq<T>,
{
#[inline]
fn almost_eq(&self, other: &[T]) -> bool {
if self.len() != other.len() {
return false;
}
for i in 0..self.len() {
if !self[i].almost_eq(&other[i]) {
return false;
}
}
true
}
}
impl<const N: usize, T> AlmostEq<[T; N]> for [T; N]
where
T: AlmostEq<T>,
{
#[inline]
fn almost_eq(&self, other: &[T; N]) -> bool {
for i in 0..N {
// SAFETY: the index is bound between [0, N)
if !unsafe { self.get_unchecked(i).almost_eq(other.get_unchecked(i)) } {
return false;
}
}
true
}
}