about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2015-03-01 14:22:58 +0000
committerbors <bors@rust-lang.org>2015-03-01 14:22:58 +0000
commit157614249594f187f421cd97f928e64c5ab5c1fa (patch)
treec54ac5a56b9f7040e48e8a079cb6042bbcb5f1e7
parent0eb0ba38d0893f711e68321d3e6fe28a929cd00e (diff)
parentd79e91033770fc5a760d34511769f5ba6c872c77 (diff)
downloadrust-157614249594f187f421cd97f928e64c5ab5c1fa.tar.gz
rust-157614249594f187f421cd97f928e64c5ab5c1fa.zip
Auto merge of #22880 - alexcrichton:deprecate-io-extensions, r=huonw
The `u64_from_be_bytes` and `u64_to_be_bytes` functions are being deprecated
with no replacement for now.

[breaking-change]
-rw-r--r--src/librbml/lib.rs76
-rw-r--r--src/librustc/metadata/decoder.rs19
-rw-r--r--src/libstd/old_io/extensions.rs4
3 files changed, 53 insertions, 46 deletions
diff --git a/src/librbml/lib.rs b/src/librbml/lib.rs
index c48dd7a6ee8..05cd24de736 100644
--- a/src/librbml/lib.rs
+++ b/src/librbml/lib.rs
@@ -132,11 +132,9 @@ pub mod reader {
     use std::char;
 
     use std::isize;
-    use std::old_io::extensions::u64_from_be_bytes;
     use std::mem::transmute;
     use std::num::Int;
-    use std::option::Option;
-    use std::option::Option::{None, Some};
+    use std::slice::bytes;
 
     use serialize;
 
@@ -199,20 +197,24 @@ pub mod reader {
             return vuint_at_slow(data, start);
         }
 
-        // Lookup table for parsing EBML Element IDs as per http://ebml.sourceforge.net/specs/
-        // The Element IDs are parsed by reading a big endian u32 positioned at data[start].
-        // Using the four most significant bits of the u32 we lookup in the table below how the
-        // element ID should be derived from it.
+        // Lookup table for parsing EBML Element IDs as per
+        // http://ebml.sourceforge.net/specs/ The Element IDs are parsed by
+        // reading a big endian u32 positioned at data[start].  Using the four
+        // most significant bits of the u32 we lookup in the table below how
+        // the element ID should be derived from it.
         //
-        // The table stores tuples (shift, mask) where shift is the number the u32 should be right
-        // shifted with and mask is the value the right shifted value should be masked with.
-        // If for example the most significant bit is set this means it's a class A ID and the u32
-        // should be right shifted with 24 and masked with 0x7f. Therefore we store (24, 0x7f) at
-        // index 0x8 - 0xF (four bit numbers where the most significant bit is set).
+        // The table stores tuples (shift, mask) where shift is the number the
+        // u32 should be right shifted with and mask is the value the right
+        // shifted value should be masked with.  If for example the most
+        // significant bit is set this means it's a class A ID and the u32
+        // should be right shifted with 24 and masked with 0x7f. Therefore we
+        // store (24, 0x7f) at index 0x8 - 0xF (four bit numbers where the most
+        // significant bit is set).
         //
-        // By storing the number of shifts and masks in a table instead of checking in order if
-        // the most significant bit is set, the second most significant bit is set etc. we can
-        // replace up to three "and+branch" with a single table lookup which gives us a measured
+        // By storing the number of shifts and masks in a table instead of
+        // checking in order if the most significant bit is set, the second
+        // most significant bit is set etc. we can replace up to three
+        // "and+branch" with a single table lookup which gives us a measured
         // speedup of around 2x on x86_64.
         static SHIFT_MASK_TABLE: [(uint, u32); 16] = [
             (0, 0x0), (0, 0x0fffffff),
@@ -318,17 +320,23 @@ pub mod reader {
 
     pub fn doc_as_u16(d: Doc) -> u16 {
         assert_eq!(d.end, d.start + 2);
-        u64_from_be_bytes(d.data, d.start, 2) as u16
+        let mut b = [0; 2];
+        bytes::copy_memory(&mut b, &d.data[d.start..d.end]);
+        unsafe { (*(b.as_ptr() as *const u16)).to_be() }
     }
 
     pub fn doc_as_u32(d: Doc) -> u32 {
         assert_eq!(d.end, d.start + 4);
-        u64_from_be_bytes(d.data, d.start, 4) as u32
+        let mut b = [0; 4];
+        bytes::copy_memory(&mut b, &d.data[d.start..d.end]);
+        unsafe { (*(b.as_ptr() as *const u32)).to_be() }
     }
 
     pub fn doc_as_u64(d: Doc) -> u64 {
         assert_eq!(d.end, d.start + 8);
-        u64_from_be_bytes(d.data, d.start, 8)
+        let mut b = [0; 8];
+        bytes::copy_memory(&mut b, &d.data[d.start..d.end]);
+        unsafe { (*(b.as_ptr() as *const u64)).to_be() }
     }
 
     pub fn doc_as_i8(d: Doc) -> i8 { doc_as_u8(d) as i8 }
@@ -689,11 +697,10 @@ pub mod reader {
 }
 
 pub mod writer {
-    use std::clone::Clone;
-    use std::old_io::extensions::u64_to_be_bytes;
+    use std::mem;
+    use std::num::Int;
     use std::old_io::{Writer, Seek};
     use std::old_io;
-    use std::mem;
 
     use super::{ EsVec, EsMap, EsEnum, EsVecLen, EsVecElt, EsMapLen, EsMapKey,
         EsEnumVid, EsU64, EsU32, EsU16, EsU8, EsInt, EsI64, EsI32, EsI16, EsI8,
@@ -794,21 +801,18 @@ pub mod writer {
         }
 
         pub fn wr_tagged_u64(&mut self, tag_id: uint, v: u64) -> EncodeResult {
-            u64_to_be_bytes(v, 8, |v| {
-                self.wr_tagged_bytes(tag_id, v)
-            })
+            let bytes: [u8; 8] = unsafe { mem::transmute(v.to_be()) };
+            self.wr_tagged_bytes(tag_id, &bytes)
         }
 
         pub fn wr_tagged_u32(&mut self, tag_id: uint, v: u32)  -> EncodeResult{
-            u64_to_be_bytes(v as u64, 4, |v| {
-                self.wr_tagged_bytes(tag_id, v)
-            })
+            let bytes: [u8; 4] = unsafe { mem::transmute(v.to_be()) };
+            self.wr_tagged_bytes(tag_id, &bytes)
         }
 
         pub fn wr_tagged_u16(&mut self, tag_id: uint, v: u16) -> EncodeResult {
-            u64_to_be_bytes(v as u64, 2, |v| {
-                self.wr_tagged_bytes(tag_id, v)
-            })
+            let bytes: [u8; 2] = unsafe { mem::transmute(v.to_be()) };
+            self.wr_tagged_bytes(tag_id, &bytes)
         }
 
         pub fn wr_tagged_u8(&mut self, tag_id: uint, v: u8) -> EncodeResult {
@@ -816,21 +820,15 @@ pub mod writer {
         }
 
         pub fn wr_tagged_i64(&mut self, tag_id: uint, v: i64) -> EncodeResult {
-            u64_to_be_bytes(v as u64, 8, |v| {
-                self.wr_tagged_bytes(tag_id, v)
-            })
+            self.wr_tagged_u64(tag_id, v as u64)
         }
 
         pub fn wr_tagged_i32(&mut self, tag_id: uint, v: i32) -> EncodeResult {
-            u64_to_be_bytes(v as u64, 4, |v| {
-                self.wr_tagged_bytes(tag_id, v)
-            })
+            self.wr_tagged_u32(tag_id, v as u32)
         }
 
         pub fn wr_tagged_i16(&mut self, tag_id: uint, v: i16) -> EncodeResult {
-            u64_to_be_bytes(v as u64, 2, |v| {
-                self.wr_tagged_bytes(tag_id, v)
-            })
+            self.wr_tagged_u16(tag_id, v as u16)
         }
 
         pub fn wr_tagged_i8(&mut self, tag_id: uint, v: i8) -> EncodeResult {
diff --git a/src/librustc/metadata/decoder.rs b/src/librustc/metadata/decoder.rs
index 0503045ac6e..251c5e6eac7 100644
--- a/src/librustc/metadata/decoder.rs
+++ b/src/librustc/metadata/decoder.rs
@@ -34,10 +34,11 @@ use middle::astencode::vtable_decoder_helpers;
 
 use std::collections::HashMap;
 use std::hash::{self, Hash, SipHasher};
-use std::old_io::extensions::u64_from_be_bytes;
-use std::old_io;
 use std::num::FromPrimitive;
+use std::num::Int;
+use std::old_io;
 use std::rc::Rc;
+use std::slice::bytes;
 use std::str;
 
 use rbml::reader;
@@ -60,20 +61,26 @@ pub type Cmd<'a> = &'a crate_metadata;
 // what crate that's in and give us a def_id that makes sense for the current
 // build.
 
+fn u32_from_be_bytes(bytes: &[u8]) -> u32 {
+    let mut b = [0; 4];
+    bytes::copy_memory(&mut b, &bytes[..4]);
+    unsafe { (*(b.as_ptr() as *const u32)).to_be() }
+}
+
 fn lookup_hash<'a, F>(d: rbml::Doc<'a>, mut eq_fn: F, hash: u64) -> Option<rbml::Doc<'a>> where
     F: FnMut(&[u8]) -> bool,
 {
     let index = reader::get_doc(d, tag_index);
     let table = reader::get_doc(index, tag_index_table);
     let hash_pos = table.start + (hash % 256 * 4) as uint;
-    let pos = u64_from_be_bytes(d.data, hash_pos, 4) as uint;
+    let pos = u32_from_be_bytes(&d.data[hash_pos..]) as uint;
     let tagged_doc = reader::doc_at(d.data, pos).unwrap();
 
     let belt = tag_index_buckets_bucket_elt;
 
     let mut ret = None;
     reader::tagged_docs(tagged_doc.doc, belt, |elt| {
-        let pos = u64_from_be_bytes(elt.data, elt.start, 4) as uint;
+        let pos = u32_from_be_bytes(&elt.data[elt.start..]) as uint;
         if eq_fn(&elt.data[elt.start + 4 .. elt.end]) {
             ret = Some(reader::doc_at(d.data, pos).unwrap().doc);
             false
@@ -87,9 +94,7 @@ fn lookup_hash<'a, F>(d: rbml::Doc<'a>, mut eq_fn: F, hash: u64) -> Option<rbml:
 pub fn maybe_find_item<'a>(item_id: ast::NodeId,
                            items: rbml::Doc<'a>) -> Option<rbml::Doc<'a>> {
     fn eq_item(bytes: &[u8], item_id: ast::NodeId) -> bool {
-        return u64_from_be_bytes(
-            &bytes[0..4], 0, 4) as ast::NodeId
-            == item_id;
+        u32_from_be_bytes(bytes) == item_id
     }
     lookup_hash(items,
                 |a| eq_item(a, item_id),
diff --git a/src/libstd/old_io/extensions.rs b/src/libstd/old_io/extensions.rs
index 8bd19f063f0..45a86a9fde7 100644
--- a/src/libstd/old_io/extensions.rs
+++ b/src/libstd/old_io/extensions.rs
@@ -11,6 +11,10 @@
 //! Utility mixins that apply to all Readers and Writers
 
 #![allow(missing_docs)]
+#![unstable(feature = "old_io")]
+#![deprecated(since = "1.0.0",
+              reason = "functionality will be removed with no immediate \
+                        replacement")]
 
 // FIXME: Not sure how this should be structured
 // FIXME: Iteration should probably be considered separately