From e8e3e3c20f913a2c628924d05d93baa0701593da Mon Sep 17 00:00:00 2001 From: Carter Anderson Date: Wed, 10 Jun 2020 15:35:23 -0700 Subject: [PATCH] move FloatOrd to bevy_core --- crates/bevy_app/src/schedule_plan.rs | 33 +--------------------- crates/bevy_core/src/float_ord.rs | 41 ++++++++++++++++++++++++++++ crates/bevy_core/src/lib.rs | 1 + 3 files changed, 43 insertions(+), 32 deletions(-) create mode 100644 crates/bevy_core/src/float_ord.rs diff --git a/crates/bevy_app/src/schedule_plan.rs b/crates/bevy_app/src/schedule_plan.rs index c252ed9c3e..a7d00ce421 100644 --- a/crates/bevy_app/src/schedule_plan.rs +++ b/crates/bevy_app/src/schedule_plan.rs @@ -2,7 +2,6 @@ use crate::System; use legion::prelude::Schedule; use std::{ borrow::Cow, - cmp::Ordering, collections::{HashMap, HashSet}, }; @@ -105,34 +104,4 @@ impl SchedulePlan { self } -} - -// working around the famous "rust float ordering" problem -#[derive(PartialOrd)] -struct FloatOrd(f32); - -impl Ord for FloatOrd { - fn cmp(&self, other: &Self) -> Ordering { - self.0.partial_cmp(&other.0).unwrap_or_else(|| { - if self.0.is_nan() && !other.0.is_nan() { - Ordering::Less - } else if !self.0.is_nan() && other.0.is_nan() { - Ordering::Greater - } else { - Ordering::Equal - } - }) - } -} - -impl PartialEq for FloatOrd { - fn eq(&self, other: &Self) -> bool { - if self.0.is_nan() && other.0.is_nan() { - true - } else { - self.0 == other.0 - } - } -} - -impl Eq for FloatOrd {} +} \ No newline at end of file diff --git a/crates/bevy_core/src/float_ord.rs b/crates/bevy_core/src/float_ord.rs new file mode 100644 index 0000000000..7a02e85fc7 --- /dev/null +++ b/crates/bevy_core/src/float_ord.rs @@ -0,0 +1,41 @@ +use crate::bytes::AsBytes; +use std::{ + cmp::Ordering, + hash::{Hash, Hasher}, +}; + +// working around the famous "rust float ordering" problem +#[derive(Debug, Copy, Clone, PartialOrd)] +pub struct FloatOrd(pub f32); + +impl Ord for FloatOrd { + fn cmp(&self, other: &Self) -> Ordering { + self.0.partial_cmp(&other.0).unwrap_or_else(|| { + if self.0.is_nan() && !other.0.is_nan() { + Ordering::Less + } else if !self.0.is_nan() && other.0.is_nan() { + Ordering::Greater + } else { + Ordering::Equal + } + }) + } +} + +impl PartialEq for FloatOrd { + fn eq(&self, other: &Self) -> bool { + if self.0.is_nan() && other.0.is_nan() { + true + } else { + self.0 == other.0 + } + } +} + +impl Eq for FloatOrd {} + +impl Hash for FloatOrd { + fn hash(&self, state: &mut H) { + state.write(self.0.as_bytes()); + } +} diff --git a/crates/bevy_core/src/lib.rs b/crates/bevy_core/src/lib.rs index a9ddc544db..178716fa56 100644 --- a/crates/bevy_core/src/lib.rs +++ b/crates/bevy_core/src/lib.rs @@ -1,6 +1,7 @@ pub mod bytes; pub mod time; pub mod transform; +pub mod float_ord; use bevy_app::{stage, AppBuilder, AppPlugin}; use bevy_transform::{