Skip to content

Commit

Permalink
BitsEq trait for working around float Nan==Nan
Browse files Browse the repository at this point in the history
  • Loading branch information
rroelke committed Mar 22, 2024
1 parent 1f64d5f commit f58de44
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions tiledb/api/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,52 @@ impl<T: CAPISameRepr> CAPIConverter for T {
*value
}
}

/// Trait for comparisons based on value bits.
/// This exists to work around float NaN which is not equal to itself,
/// but we want it to be for generic operations with TileDB structures.
pub trait BitsEq: PartialEq {
fn bits_eq(&self, other: &Self) -> bool;
}

macro_rules! derive_reflexive_eq {
($typename:ty) => {
impl BitsEq for $typename {
fn bits_eq(&self, other: &Self) -> bool {
<Self as PartialEq>::eq(self, other)
}
}
};
}

derive_reflexive_eq!(bool);
derive_reflexive_eq!(u8);
derive_reflexive_eq!(u16);
derive_reflexive_eq!(u32);
derive_reflexive_eq!(u64);
derive_reflexive_eq!(i8);
derive_reflexive_eq!(i16);
derive_reflexive_eq!(i32);
derive_reflexive_eq!(i64);

impl BitsEq for f32 {
fn bits_eq(&self, other: &Self) -> bool {
self.to_bits() == other.to_bits()
}
}

impl BitsEq for f64 {
fn bits_eq(&self, other: &Self) -> bool {
self.to_bits() == other.to_bits()
}
}

impl<T1, T2> BitsEq for (T1, T2)
where
T1: BitsEq,
T2: BitsEq,
{
fn bits_eq(&self, other: &Self) -> bool {
self.0.bits_eq(&other.0) && self.1.bits_eq(&other.1)
}
}

0 comments on commit f58de44

Please sign in to comment.