aterm/bad_idea.rs
1use std::mem;
2use std::hash;
3
4/// An f32 with broken Eq and Hash implementations.
5/// To quote shepmaster (except this is an f32):
6/// > Basically, if you want to treat a f64 as a set of bits that have no meaning, well, we can
7/// > treat them as an equivalently sized bag of bits that know how to be hashed and
8/// > bitwise-compared.
9/// > Don't be surprised when one of the 16 million NaN values doesn't equal another one.
10/// http://stackoverflow.com/q/39638363/859279
11#[derive(Debug, Clone, Copy)]
12pub struct BrokenF32(pub f32);
13
14impl BrokenF32 {
15 fn key(&self) -> u32 {
16 unsafe { mem::transmute(self.0) }
17 }
18}
19
20impl hash::Hash for BrokenF32 {
21 fn hash<H>(&self, state: &mut H)
22 where
23 H: hash::Hasher,
24 {
25 self.key().hash(state)
26 }
27}
28
29impl PartialEq for BrokenF32 {
30 fn eq(&self, other: &Self) -> bool {
31 self.key() == other.key()
32 }
33}
34
35impl Eq for BrokenF32 {}