move FloatOrd to bevy_core

This commit is contained in:
Carter Anderson 2020-06-10 15:35:23 -07:00
parent f4b07ec9c7
commit e8e3e3c20f
3 changed files with 43 additions and 32 deletions

View file

@ -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 {}
}

View file

@ -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<H: Hasher>(&self, state: &mut H) {
state.write(self.0.as_bytes());
}
}

View file

@ -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::{