use std::cmp::Ordering;
use std::hash::{Hash, Hasher};
#[derive(Clone, Copy, Debug, Eq, PartialEq, thiserror::Error)]
#[error("NaN is not an indexable floating-point value")]
pub struct NotNanError;
#[derive(Clone, Copy, Debug)]
pub struct NotNanF32(f32);
impl NotNanF32 {
pub fn new(value: f32) -> Result<Self, NotNanError> {
if value.is_nan() {
Err(NotNanError)
} else {
Ok(Self(value))
}
}
#[must_use]
pub const fn get(self) -> f32 {
self.0
}
}
impl PartialEq for NotNanF32 {
fn eq(&self, rhs: &Self) -> bool {
self.0.to_bits() == rhs.0.to_bits()
}
}
impl Eq for NotNanF32 {}
impl PartialOrd for NotNanF32 {
fn partial_cmp(&self, rhs: &Self) -> Option<Ordering> {
Some(self.cmp(rhs))
}
}
impl Ord for NotNanF32 {
fn cmp(&self, rhs: &Self) -> Ordering {
self.0.total_cmp(&rhs.0)
}
}
impl Hash for NotNanF32 {
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.to_bits().hash(state);
}
}
#[derive(Clone, Copy, Debug)]
pub struct NotNanF64(f64);
impl NotNanF64 {
pub fn new(value: f64) -> Result<Self, NotNanError> {
if value.is_nan() {
Err(NotNanError)
} else {
Ok(Self(value))
}
}
#[must_use]
pub const fn get(self) -> f64 {
self.0
}
}
impl PartialEq for NotNanF64 {
fn eq(&self, rhs: &Self) -> bool {
self.0.to_bits() == rhs.0.to_bits()
}
}
impl Eq for NotNanF64 {}
impl PartialOrd for NotNanF64 {
fn partial_cmp(&self, rhs: &Self) -> Option<Ordering> {
Some(self.cmp(rhs))
}
}
impl Ord for NotNanF64 {
fn cmp(&self, rhs: &Self) -> Ordering {
self.0.total_cmp(&rhs.0)
}
}
impl Hash for NotNanF64 {
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.to_bits().hash(state);
}
}