about summary refs log tree commit diff
path: root/src/libstd/to_bytes.rs
diff options
context:
space:
mode:
authorgareth <gareth@gareth-N56VM.(none)>2013-06-15 17:58:38 +0100
committergareth <gareth@gareth-N56VM.(none)>2013-06-15 17:58:38 +0100
commitd22f417c74bf23c5a9c34d10ef5a327d2ab6ebab (patch)
tree1b2de8e661eb0a95094eed7eb741a577831055d8 /src/libstd/to_bytes.rs
parenteac0200f18bfe19646ae5dce56acc325629224cb (diff)
Add IterBytes impls for float/f32/f64. This allows creating
HashMaps with floats as keys.
Diffstat (limited to 'src/libstd/to_bytes.rs')
-rw-r--r--src/libstd/to_bytes.rs30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/libstd/to_bytes.rs b/src/libstd/to_bytes.rs
index c0c8b729f9e..6d7820ffea5 100644
--- a/src/libstd/to_bytes.rs
+++ b/src/libstd/to_bytes.rs
@@ -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 {