Skip to content

Commit

Permalink
Auto merge of #22880 - alexcrichton:deprecate-io-extensions, r=huonw
Browse files Browse the repository at this point in the history
The `u64_from_be_bytes` and `u64_to_be_bytes` functions are being deprecated
with no replacement for now.

[breaking-change]
  • Loading branch information
bors committed Mar 1, 2015
2 parents 0eb0ba3 + d79e910 commit 1576142
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 46 deletions.
76 changes: 37 additions & 39 deletions src/librbml/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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 }
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -794,43 +801,34 @@ 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 {
self.wr_tagged_bytes(tag_id, &[v])
}

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 {
Expand Down
19 changes: 12 additions & 7 deletions src/librustc/metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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),
Expand Down
4 changes: 4 additions & 0 deletions src/libstd/old_io/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1576142

Please sign in to comment.