Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 71 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ derive-visitor = { version = "0.4.0", optional = true }
num-cmp = { version = "0.1.0", optional = true }
num-traits = { version = "0.2.9", default-features = false }
proptest = { version = "1.0.0", optional = true }
rand = { version = "0.8.3", optional = true, default-features = false }
rand = { version = "0.9.2", optional = true, default-features = false }
rkyv = { version = "0.7.41", optional = true, default-features = false, features = ["rend"] }
schemars = { version = "0.8.8", optional = true }
serde = { version = "1.0", optional = true, default-features = false }
Expand All @@ -35,8 +35,8 @@ serde_test = "1.0"
default = ["std"]
std = ["num-traits/std"]
libm = ["num-traits/libm"]
serde = ["dep:serde", "rand?/serde1"]
randtest = ["rand/std", "rand/std_rng"]
serde = ["dep:serde", "rand?/serde"]
randtest = ["rand/default"]
rkyv = ["rkyv_32"]
rkyv_16 = ["dep:rkyv", "rkyv?/size_16"]
rkyv_32 = ["dep:rkyv", "rkyv?/size_32"]
Expand Down
60 changes: 37 additions & 23 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1243,7 +1243,7 @@ impl<T: FloatCore + Num> Num for OrderedFloat<T> {
not(feature = "bytemuck"),
doc = "[`bytemuck`]: https://docs.rs/bytemuck/1/"
)]
#[derive(PartialOrd, PartialEq, Default, Clone, Copy)]
#[derive(PartialEq, Default, Clone, Copy)]
#[repr(transparent)]
pub struct NotNan<T>(T);

Expand Down Expand Up @@ -1312,12 +1312,20 @@ impl Borrow<f64> for NotNan<f64> {
}
}

#[allow(clippy::derive_ord_xor_partial_ord)]
impl<T: FloatCore> PartialOrd for NotNan<T> {
#[inline]
fn partial_cmp(&self, other: &NotNan<T>) -> Option<Ordering> {
Some(self.cmp(other))
}
}

impl<T: FloatCore> Ord for NotNan<T> {
#[inline]
fn cmp(&self, other: &NotNan<T>) -> Ordering {
// Can't use unreachable_unchecked because unsafe code can't depend on FloatCore impl.
// https://github.com/reem/rust-ordered-float/issues/150
self.partial_cmp(other)
self.0
.partial_cmp(&other.0)
.expect("partial_cmp failed for non-NaN value")
}
}
Expand Down Expand Up @@ -2567,8 +2575,8 @@ mod impl_schemars {
#[cfg(feature = "rand")]
mod impl_rand {
use super::{NotNan, OrderedFloat};
use rand::distributions::uniform::*;
use rand::distributions::{Distribution, Open01, OpenClosed01, Standard};
use rand::distr::uniform::*;
use rand::distr::{Distribution, Open01, OpenClosed01, StandardUniform};
use rand::Rng;

macro_rules! impl_distribution {
Expand All @@ -2592,7 +2600,7 @@ mod impl_rand {
}
}

impl_distribution! { Standard, f32, f64 }
impl_distribution! { StandardUniform, f32, f64 }
impl_distribution! { Open01, f32, f64 }
impl_distribution! { OpenClosed01, f32, f64 }

Expand Down Expand Up @@ -2638,14 +2646,17 @@ mod impl_rand {
($f:ty) => {
impl UniformSampler for UniformNotNan<$f> {
type X = NotNan<$f>;
fn new<B1, B2>(low: B1, high: B2) -> Self
fn new<B1, B2>(low: B1, high: B2) -> Result<Self, Error>
where
B1: SampleBorrow<Self::X> + Sized,
B2: SampleBorrow<Self::X> + Sized,
{
UniformNotNan(UniformFloat::<$f>::new(low.borrow().0, high.borrow().0))
Ok(UniformNotNan(UniformFloat::<$f>::new(
low.borrow().0,
high.borrow().0,
)?))
}
fn new_inclusive<B1, B2>(low: B1, high: B2) -> Self
fn new_inclusive<B1, B2>(low: B1, high: B2) -> Result<Self, Error>
where
B1: SampleBorrow<Self::X> + Sized,
B2: SampleBorrow<Self::X> + Sized,
Expand All @@ -2660,14 +2671,17 @@ mod impl_rand {

impl UniformSampler for UniformOrdered<$f> {
type X = OrderedFloat<$f>;
fn new<B1, B2>(low: B1, high: B2) -> Self
fn new<B1, B2>(low: B1, high: B2) -> Result<Self, Error>
where
B1: SampleBorrow<Self::X> + Sized,
B2: SampleBorrow<Self::X> + Sized,
{
UniformOrdered(UniformFloat::<$f>::new(low.borrow().0, high.borrow().0))
Ok(UniformOrdered(UniformFloat::<$f>::new(
low.borrow().0,
high.borrow().0,
)?))
}
fn new_inclusive<B1, B2>(low: B1, high: B2) -> Self
fn new_inclusive<B1, B2>(low: B1, high: B2) -> Result<Self, Error>
where
B1: SampleBorrow<Self::X> + Sized,
B2: SampleBorrow<Self::X> + Sized,
Expand All @@ -2690,19 +2704,19 @@ mod impl_rand {

fn sample_fuzz<T>()
where
Standard: Distribution<NotNan<T>>,
StandardUniform: Distribution<NotNan<T>>,
Open01: Distribution<NotNan<T>>,
OpenClosed01: Distribution<NotNan<T>>,
Standard: Distribution<OrderedFloat<T>>,
StandardUniform: Distribution<OrderedFloat<T>>,
Open01: Distribution<OrderedFloat<T>>,
OpenClosed01: Distribution<OrderedFloat<T>>,
T: crate::Float,
{
let mut rng = rand::thread_rng();
let f1: NotNan<T> = rng.sample(Standard);
let mut rng = rand::rng();
let f1: NotNan<T> = rng.sample(StandardUniform);
let f2: NotNan<T> = rng.sample(Open01);
let f3: NotNan<T> = rng.sample(OpenClosed01);
let _: OrderedFloat<T> = rng.sample(Standard);
let _: OrderedFloat<T> = rng.sample(StandardUniform);
let _: OrderedFloat<T> = rng.sample(Open01);
let _: OrderedFloat<T> = rng.sample(OpenClosed01);
assert!(!f1.into_inner().is_nan());
Expand All @@ -2727,24 +2741,24 @@ mod impl_rand {
NotNan::new(0f64).unwrap(),
NotNan::new(f64::INFINITY).unwrap(),
);
let uniform = Uniform::new(low, high);
let _ = uniform.sample(&mut rand::thread_rng());
let uniform = Uniform::new(low, high).unwrap();
let _ = uniform.sample(&mut rand::rng());
}

#[test]
#[should_panic]
fn uniform_sampling_panic_on_infinity_ordered() {
let (low, high) = (OrderedFloat(0f64), OrderedFloat(f64::INFINITY));
let uniform = Uniform::new(low, high);
let _ = uniform.sample(&mut rand::thread_rng());
let uniform = Uniform::new(low, high).unwrap();
let _ = uniform.sample(&mut rand::rng());
}

#[test]
#[should_panic]
fn uniform_sampling_panic_on_nan_ordered() {
let (low, high) = (OrderedFloat(0f64), OrderedFloat(f64::NAN));
let uniform = Uniform::new(low, high);
let _ = uniform.sample(&mut rand::thread_rng());
let uniform = Uniform::new(low, high).unwrap();
let _ = uniform.sample(&mut rand::rng());
}
}
}
Expand Down