Add IterBytes impls for float/f32/f64. This allows creating
HashMaps with floats as keys.
This commit is contained in:
parent
eac0200f18
commit
d22f417c74
2 changed files with 41 additions and 0 deletions
|
@ -14,6 +14,7 @@ The `ToBytes` and `IterBytes` traits
|
|||
|
||||
*/
|
||||
|
||||
use cast;
|
||||
use io;
|
||||
use io::Writer;
|
||||
use option::{None, Option, Some};
|
||||
|
@ -190,6 +191,35 @@ impl IterBytes for int {
|
|||
}
|
||||
}
|
||||
|
||||
impl IterBytes for float {
|
||||
#[inline(always)]
|
||||
fn iter_bytes(&self, lsb0: bool, f: Cb) -> bool {
|
||||
(*self as f64).iter_bytes(lsb0, f)
|
||||
}
|
||||
}
|
||||
|
||||
impl IterBytes for f32 {
|
||||
#[inline(always)]
|
||||
fn iter_bytes(&self, lsb0: bool, f: Cb) -> bool {
|
||||
let i: u32 = unsafe {
|
||||
// 0.0 == -0.0 so they should also have the same hashcode
|
||||
cast::transmute(if *self == -0.0 { 0.0 } else { *self })
|
||||
};
|
||||
i.iter_bytes(lsb0, f)
|
||||
}
|
||||
}
|
||||
|
||||
impl IterBytes for f64 {
|
||||
#[inline(always)]
|
||||
fn iter_bytes(&self, lsb0: bool, f: Cb) -> bool {
|
||||
let i: u64 = unsafe {
|
||||
// 0.0 == -0.0 so they should also have the same hashcode
|
||||
cast::transmute(if *self == -0.0 { 0.0 } else { *self })
|
||||
};
|
||||
i.iter_bytes(lsb0, f)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'self,A:IterBytes> IterBytes for &'self [A] {
|
||||
#[inline(always)]
|
||||
fn iter_bytes(&self, lsb0: bool, f: Cb) -> bool {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue