Skip to content

Commit d79e910

Browse files
committed
std: Deprecated the old_io::extensions module
The `u64_from_be_bytes` and `u64_to_be_bytes` functions are being deprecated with no replacement for now. [breaking-change]
1 parent bd0d8e4 commit d79e910

File tree

3 files changed

+53
-46
lines changed

3 files changed

+53
-46
lines changed

src/librbml/lib.rs

+37-39
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,9 @@ pub mod reader {
132132
use std::char;
133133

134134
use std::isize;
135-
use std::old_io::extensions::u64_from_be_bytes;
136135
use std::mem::transmute;
137136
use std::num::Int;
138-
use std::option::Option;
139-
use std::option::Option::{None, Some};
137+
use std::slice::bytes;
140138

141139
use serialize;
142140

@@ -199,20 +197,24 @@ pub mod reader {
199197
return vuint_at_slow(data, start);
200198
}
201199

202-
// Lookup table for parsing EBML Element IDs as per http://ebml.sourceforge.net/specs/
203-
// The Element IDs are parsed by reading a big endian u32 positioned at data[start].
204-
// Using the four most significant bits of the u32 we lookup in the table below how the
205-
// element ID should be derived from it.
200+
// Lookup table for parsing EBML Element IDs as per
201+
// http://ebml.sourceforge.net/specs/ The Element IDs are parsed by
202+
// reading a big endian u32 positioned at data[start]. Using the four
203+
// most significant bits of the u32 we lookup in the table below how
204+
// the element ID should be derived from it.
206205
//
207-
// The table stores tuples (shift, mask) where shift is the number the u32 should be right
208-
// shifted with and mask is the value the right shifted value should be masked with.
209-
// If for example the most significant bit is set this means it's a class A ID and the u32
210-
// should be right shifted with 24 and masked with 0x7f. Therefore we store (24, 0x7f) at
211-
// index 0x8 - 0xF (four bit numbers where the most significant bit is set).
206+
// The table stores tuples (shift, mask) where shift is the number the
207+
// u32 should be right shifted with and mask is the value the right
208+
// shifted value should be masked with. If for example the most
209+
// significant bit is set this means it's a class A ID and the u32
210+
// should be right shifted with 24 and masked with 0x7f. Therefore we
211+
// store (24, 0x7f) at index 0x8 - 0xF (four bit numbers where the most
212+
// significant bit is set).
212213
//
213-
// By storing the number of shifts and masks in a table instead of checking in order if
214-
// the most significant bit is set, the second most significant bit is set etc. we can
215-
// replace up to three "and+branch" with a single table lookup which gives us a measured
214+
// By storing the number of shifts and masks in a table instead of
215+
// checking in order if the most significant bit is set, the second
216+
// most significant bit is set etc. we can replace up to three
217+
// "and+branch" with a single table lookup which gives us a measured
216218
// speedup of around 2x on x86_64.
217219
static SHIFT_MASK_TABLE: [(uint, u32); 16] = [
218220
(0, 0x0), (0, 0x0fffffff),
@@ -318,17 +320,23 @@ pub mod reader {
318320

319321
pub fn doc_as_u16(d: Doc) -> u16 {
320322
assert_eq!(d.end, d.start + 2);
321-
u64_from_be_bytes(d.data, d.start, 2) as u16
323+
let mut b = [0; 2];
324+
bytes::copy_memory(&mut b, &d.data[d.start..d.end]);
325+
unsafe { (*(b.as_ptr() as *const u16)).to_be() }
322326
}
323327

324328
pub fn doc_as_u32(d: Doc) -> u32 {
325329
assert_eq!(d.end, d.start + 4);
326-
u64_from_be_bytes(d.data, d.start, 4) as u32
330+
let mut b = [0; 4];
331+
bytes::copy_memory(&mut b, &d.data[d.start..d.end]);
332+
unsafe { (*(b.as_ptr() as *const u32)).to_be() }
327333
}
328334

329335
pub fn doc_as_u64(d: Doc) -> u64 {
330336
assert_eq!(d.end, d.start + 8);
331-
u64_from_be_bytes(d.data, d.start, 8)
337+
let mut b = [0; 8];
338+
bytes::copy_memory(&mut b, &d.data[d.start..d.end]);
339+
unsafe { (*(b.as_ptr() as *const u64)).to_be() }
332340
}
333341

334342
pub fn doc_as_i8(d: Doc) -> i8 { doc_as_u8(d) as i8 }
@@ -689,11 +697,10 @@ pub mod reader {
689697
}
690698

691699
pub mod writer {
692-
use std::clone::Clone;
693-
use std::old_io::extensions::u64_to_be_bytes;
700+
use std::mem;
701+
use std::num::Int;
694702
use std::old_io::{Writer, Seek};
695703
use std::old_io;
696-
use std::mem;
697704

698705
use super::{ EsVec, EsMap, EsEnum, EsVecLen, EsVecElt, EsMapLen, EsMapKey,
699706
EsEnumVid, EsU64, EsU32, EsU16, EsU8, EsInt, EsI64, EsI32, EsI16, EsI8,
@@ -794,43 +801,34 @@ pub mod writer {
794801
}
795802

796803
pub fn wr_tagged_u64(&mut self, tag_id: uint, v: u64) -> EncodeResult {
797-
u64_to_be_bytes(v, 8, |v| {
798-
self.wr_tagged_bytes(tag_id, v)
799-
})
804+
let bytes: [u8; 8] = unsafe { mem::transmute(v.to_be()) };
805+
self.wr_tagged_bytes(tag_id, &bytes)
800806
}
801807

802808
pub fn wr_tagged_u32(&mut self, tag_id: uint, v: u32) -> EncodeResult{
803-
u64_to_be_bytes(v as u64, 4, |v| {
804-
self.wr_tagged_bytes(tag_id, v)
805-
})
809+
let bytes: [u8; 4] = unsafe { mem::transmute(v.to_be()) };
810+
self.wr_tagged_bytes(tag_id, &bytes)
806811
}
807812

808813
pub fn wr_tagged_u16(&mut self, tag_id: uint, v: u16) -> EncodeResult {
809-
u64_to_be_bytes(v as u64, 2, |v| {
810-
self.wr_tagged_bytes(tag_id, v)
811-
})
814+
let bytes: [u8; 2] = unsafe { mem::transmute(v.to_be()) };
815+
self.wr_tagged_bytes(tag_id, &bytes)
812816
}
813817

814818
pub fn wr_tagged_u8(&mut self, tag_id: uint, v: u8) -> EncodeResult {
815819
self.wr_tagged_bytes(tag_id, &[v])
816820
}
817821

818822
pub fn wr_tagged_i64(&mut self, tag_id: uint, v: i64) -> EncodeResult {
819-
u64_to_be_bytes(v as u64, 8, |v| {
820-
self.wr_tagged_bytes(tag_id, v)
821-
})
823+
self.wr_tagged_u64(tag_id, v as u64)
822824
}
823825

824826
pub fn wr_tagged_i32(&mut self, tag_id: uint, v: i32) -> EncodeResult {
825-
u64_to_be_bytes(v as u64, 4, |v| {
826-
self.wr_tagged_bytes(tag_id, v)
827-
})
827+
self.wr_tagged_u32(tag_id, v as u32)
828828
}
829829

830830
pub fn wr_tagged_i16(&mut self, tag_id: uint, v: i16) -> EncodeResult {
831-
u64_to_be_bytes(v as u64, 2, |v| {
832-
self.wr_tagged_bytes(tag_id, v)
833-
})
831+
self.wr_tagged_u16(tag_id, v as u16)
834832
}
835833

836834
pub fn wr_tagged_i8(&mut self, tag_id: uint, v: i8) -> EncodeResult {

src/librustc/metadata/decoder.rs

+12-7
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@ use middle::astencode::vtable_decoder_helpers;
3434

3535
use std::collections::HashMap;
3636
use std::hash::{self, Hash, SipHasher};
37-
use std::old_io::extensions::u64_from_be_bytes;
38-
use std::old_io;
3937
use std::num::FromPrimitive;
38+
use std::num::Int;
39+
use std::old_io;
4040
use std::rc::Rc;
41+
use std::slice::bytes;
4142
use std::str;
4243

4344
use rbml::reader;
@@ -60,20 +61,26 @@ pub type Cmd<'a> = &'a crate_metadata;
6061
// what crate that's in and give us a def_id that makes sense for the current
6162
// build.
6263

64+
fn u32_from_be_bytes(bytes: &[u8]) -> u32 {
65+
let mut b = [0; 4];
66+
bytes::copy_memory(&mut b, &bytes[..4]);
67+
unsafe { (*(b.as_ptr() as *const u32)).to_be() }
68+
}
69+
6370
fn lookup_hash<'a, F>(d: rbml::Doc<'a>, mut eq_fn: F, hash: u64) -> Option<rbml::Doc<'a>> where
6471
F: FnMut(&[u8]) -> bool,
6572
{
6673
let index = reader::get_doc(d, tag_index);
6774
let table = reader::get_doc(index, tag_index_table);
6875
let hash_pos = table.start + (hash % 256 * 4) as uint;
69-
let pos = u64_from_be_bytes(d.data, hash_pos, 4) as uint;
76+
let pos = u32_from_be_bytes(&d.data[hash_pos..]) as uint;
7077
let tagged_doc = reader::doc_at(d.data, pos).unwrap();
7178

7279
let belt = tag_index_buckets_bucket_elt;
7380

7481
let mut ret = None;
7582
reader::tagged_docs(tagged_doc.doc, belt, |elt| {
76-
let pos = u64_from_be_bytes(elt.data, elt.start, 4) as uint;
83+
let pos = u32_from_be_bytes(&elt.data[elt.start..]) as uint;
7784
if eq_fn(&elt.data[elt.start + 4 .. elt.end]) {
7885
ret = Some(reader::doc_at(d.data, pos).unwrap().doc);
7986
false
@@ -87,9 +94,7 @@ fn lookup_hash<'a, F>(d: rbml::Doc<'a>, mut eq_fn: F, hash: u64) -> Option<rbml:
8794
pub fn maybe_find_item<'a>(item_id: ast::NodeId,
8895
items: rbml::Doc<'a>) -> Option<rbml::Doc<'a>> {
8996
fn eq_item(bytes: &[u8], item_id: ast::NodeId) -> bool {
90-
return u64_from_be_bytes(
91-
&bytes[0..4], 0, 4) as ast::NodeId
92-
== item_id;
97+
u32_from_be_bytes(bytes) == item_id
9398
}
9499
lookup_hash(items,
95100
|a| eq_item(a, item_id),

src/libstd/old_io/extensions.rs

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
//! Utility mixins that apply to all Readers and Writers
1212
1313
#![allow(missing_docs)]
14+
#![unstable(feature = "old_io")]
15+
#![deprecated(since = "1.0.0",
16+
reason = "functionality will be removed with no immediate \
17+
replacement")]
1418

1519
// FIXME: Not sure how this should be structured
1620
// FIXME: Iteration should probably be considered separately

0 commit comments

Comments
 (0)