#[cfg_attr(
not(feature = "unstable-v2"),
allow(dead_code, reason = "only the tag 0x02 float encodings use these")
)]
pub trait FloatValue: Copy {
type Bits: Copy + Eq + std::hash::Hash;
fn key(self) -> Self::Bits;
fn widen(self) -> f64;
fn narrow(value: f64) -> Self;
fn same_bits(self, other: Self) -> bool;
}
impl FloatValue for f32 {
type Bits = u32;
fn key(self) -> u32 {
self.to_bits()
}
fn widen(self) -> f64 {
f64::from(self)
}
#[expect(
clippy::cast_possible_truncation,
reason = "narrowing is the point; the caller verifies the value returns bit-for-bit"
)]
fn narrow(value: f64) -> Self {
value as Self
}
fn same_bits(self, other: Self) -> bool {
self.to_bits() == other.to_bits()
}
}
impl FloatValue for f64 {
type Bits = u64;
fn key(self) -> u64 {
self.to_bits()
}
fn widen(self) -> Self {
self
}
fn narrow(value: f64) -> Self {
value
}
fn same_bits(self, other: Self) -> bool {
self.to_bits() == other.to_bits()
}
}