Remove std::io once and for all!

This commit is contained in:
Alex Crichton 2013-10-21 23:06:12 -07:00
parent c4907cfd14
commit 6bb1df9251
11 changed files with 123 additions and 1854 deletions

View File

@ -97,6 +97,7 @@ pub mod reader {
use std::cast::transmute;
use std::int;
use std::option::{None, Option, Some};
use std::rt::io::extensions::u64_from_be_bytes;
// ebml reading
@ -258,17 +259,17 @@ pub mod reader {
pub fn doc_as_u16(d: Doc) -> u16 {
assert_eq!(d.end, d.start + 2u);
::std::io::u64_from_be_bytes(*d.data, d.start, 2u) as u16
u64_from_be_bytes(*d.data, d.start, 2u) as u16
}
pub fn doc_as_u32(d: Doc) -> u32 {
assert_eq!(d.end, d.start + 4u);
::std::io::u64_from_be_bytes(*d.data, d.start, 4u) as u32
u64_from_be_bytes(*d.data, d.start, 4u) as u32
}
pub fn doc_as_u64(d: Doc) -> u64 {
assert_eq!(d.end, d.start + 8u);
::std::io::u64_from_be_bytes(*d.data, d.start, 8u)
u64_from_be_bytes(*d.data, d.start, 8u)
}
pub fn doc_as_i8(d: Doc) -> i8 { doc_as_u8(d) as i8 }
@ -610,6 +611,7 @@ pub mod writer {
use std::rt::io;
use std::rt::io::{Writer, Seek};
use std::rt::io::mem::MemWriter;
use std::rt::io::extensions::u64_to_be_bytes;
// ebml writing
pub struct Encoder {
@ -693,19 +695,19 @@ pub mod writer {
}
pub fn wr_tagged_u64(&mut self, tag_id: uint, v: u64) {
do ::std::io::u64_to_be_bytes(v, 8u) |v| {
do u64_to_be_bytes(v, 8u) |v| {
self.wr_tagged_bytes(tag_id, v);
}
}
pub fn wr_tagged_u32(&mut self, tag_id: uint, v: u32) {
do ::std::io::u64_to_be_bytes(v as u64, 4u) |v| {
do u64_to_be_bytes(v as u64, 4u) |v| {
self.wr_tagged_bytes(tag_id, v);
}
}
pub fn wr_tagged_u16(&mut self, tag_id: uint, v: u16) {
do ::std::io::u64_to_be_bytes(v as u64, 2u) |v| {
do u64_to_be_bytes(v as u64, 2u) |v| {
self.wr_tagged_bytes(tag_id, v);
}
}
@ -715,19 +717,19 @@ pub mod writer {
}
pub fn wr_tagged_i64(&mut self, tag_id: uint, v: i64) {
do ::std::io::u64_to_be_bytes(v as u64, 8u) |v| {
do u64_to_be_bytes(v as u64, 8u) |v| {
self.wr_tagged_bytes(tag_id, v);
}
}
pub fn wr_tagged_i32(&mut self, tag_id: uint, v: i32) {
do ::std::io::u64_to_be_bytes(v as u64, 4u) |v| {
do u64_to_be_bytes(v as u64, 4u) |v| {
self.wr_tagged_bytes(tag_id, v);
}
}
pub fn wr_tagged_i16(&mut self, tag_id: uint, v: i16) {
do ::std::io::u64_to_be_bytes(v as u64, 2u) |v| {
do u64_to_be_bytes(v as u64, 2u) |v| {
self.wr_tagged_bytes(tag_id, v);
}
}

View File

@ -14,8 +14,9 @@
use std::{vec, str};
use std::io::Reader;
use std::hashmap::HashMap;
use std::rt::io;
use std::rt::io::extensions::{ReaderByteConversions, ReaderUtil};
use super::super::TermInfo;
// These are the orders ncurses uses in its compiled format (as of 5.9). Not sure if portable.
@ -160,7 +161,8 @@ pub static stringnames: &'static[&'static str] = &'static[ "cbt", "_", "cr", "cs
"box1"];
/// Parse a compiled terminfo entry, using long capability names if `longnames` is true
pub fn parse(file: @Reader, longnames: bool) -> Result<~TermInfo, ~str> {
pub fn parse(mut file: &mut io::Reader,
longnames: bool) -> Result<~TermInfo, ~str> {
let bnames;
let snames;
let nnames;
@ -176,17 +178,17 @@ pub fn parse(file: @Reader, longnames: bool) -> Result<~TermInfo, ~str> {
}
// Check magic number
let magic = file.read_le_u16();
let magic = file.read_le_u16_();
if (magic != 0x011A) {
return Err(format!("invalid magic number: expected {:x} but found {:x}",
0x011A, magic as uint));
}
let names_bytes = file.read_le_i16() as int;
let bools_bytes = file.read_le_i16() as int;
let numbers_count = file.read_le_i16() as int;
let string_offsets_count = file.read_le_i16() as int;
let string_table_bytes = file.read_le_i16() as int;
let names_bytes = file.read_le_i16_() as int;
let bools_bytes = file.read_le_i16_() as int;
let numbers_count = file.read_le_i16_() as int;
let string_offsets_count = file.read_le_i16_() as int;
let string_table_bytes = file.read_le_i16_() as int;
assert!(names_bytes > 0);
@ -224,7 +226,7 @@ pub fn parse(file: @Reader, longnames: bool) -> Result<~TermInfo, ~str> {
let mut bools_map = HashMap::new();
if bools_bytes != 0 {
for i in range(0, bools_bytes) {
let b = file.read_byte();
let b = file.read_byte().unwrap();
if b < 0 {
error!("EOF reading bools after {} entries", i);
return Err(~"error: expected more bools but hit EOF");
@ -245,7 +247,7 @@ pub fn parse(file: @Reader, longnames: bool) -> Result<~TermInfo, ~str> {
let mut numbers_map = HashMap::new();
if numbers_count != 0 {
for i in range(0, numbers_count) {
let n = file.read_le_u16();
let n = file.read_le_u16_();
if n != 0xFFFF {
debug!("{}\\#{}", nnames[i], n);
numbers_map.insert(nnames[i].to_owned(), n);
@ -260,7 +262,7 @@ pub fn parse(file: @Reader, longnames: bool) -> Result<~TermInfo, ~str> {
if string_offsets_count != 0 {
let mut string_offsets = vec::with_capacity(10);
for _ in range(0, string_offsets_count) {
string_offsets.push(file.read_le_u16());
string_offsets.push(file.read_le_u16_());
}
debug!("offsets: {:?}", string_offsets);

View File

@ -13,7 +13,8 @@
use std::{os, str};
use std::os::getenv;
use std::io::{file_reader, Reader};
use std::rt::io;
use std::rt::io::file::FileInfo;
/// Return path to database entry for `term`
pub fn get_dbpath_for_term(term: &str) -> Option<~Path> {
@ -73,9 +74,9 @@ pub fn get_dbpath_for_term(term: &str) -> Option<~Path> {
}
/// Return open file for `term`
pub fn open(term: &str) -> Result<@Reader, ~str> {
pub fn open(term: &str) -> Result<@mut io::Reader, ~str> {
match get_dbpath_for_term(term) {
Some(x) => file_reader(x),
Some(x) => Ok(@mut x.open_reader(io::Open).unwrap() as @mut io::Reader),
None => Err(format!("could not find terminfo entry for {}", term))
}
}

View File

@ -28,6 +28,7 @@ use middle::astencode::vtable_decoder_helpers;
use std::u64;
use std::rt::io;
use std::rt::io::extensions::u64_from_be_bytes;
use std::option;
use std::str;
use std::vec;
@ -55,14 +56,14 @@ fn lookup_hash(d: ebml::Doc, eq_fn: &fn(x:&[u8]) -> bool, hash: u64) ->
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 = ::std::io::u64_from_be_bytes(*d.data, hash_pos, 4) as uint;
let pos = u64_from_be_bytes(*d.data, hash_pos, 4) as uint;
let tagged_doc = reader::doc_at(d.data, pos);
let belt = tag_index_buckets_bucket_elt;
let mut ret = None;
do reader::tagged_docs(tagged_doc.doc, belt) |elt| {
let pos = ::std::io::u64_from_be_bytes(*elt.data, elt.start, 4) as uint;
let pos = u64_from_be_bytes(*elt.data, elt.start, 4) as uint;
if eq_fn(elt.data.slice(elt.start + 4, elt.end)) {
ret = Some(reader::doc_at(d.data, pos).doc);
false
@ -77,7 +78,7 @@ pub type GetCrateDataCb<'self> = &'self fn(ast::CrateNum) -> Cmd;
pub fn maybe_find_item(item_id: int, items: ebml::Doc) -> Option<ebml::Doc> {
fn eq_item(bytes: &[u8], item_id: int) -> bool {
return ::std::io::u64_from_be_bytes(
return u64_from_be_bytes(
bytes.slice(0u, 4u), 0u, 4u) as int
== item_id;
}
@ -1253,7 +1254,7 @@ fn family_names_type(fam: Family) -> bool {
fn read_path(d: ebml::Doc) -> (~str, uint) {
do reader::with_doc_data(d) |desc| {
let pos = ::std::io::u64_from_be_bytes(desc, 0u, 4u) as uint;
let pos = u64_from_be_bytes(desc, 0u, 4u) as uint;
let pathbytes = desc.slice(4u, desc.len());
let path = str::from_utf8(pathbytes);

File diff suppressed because it is too large Load Diff

View File

@ -53,7 +53,6 @@ pub use container::{Container, Mutable, Map, MutableMap, Set, MutableSet};
pub use default::Default;
pub use from_str::FromStr;
pub use hash::Hash;
pub use io::{Reader, ReaderUtil, Writer, WriterUtil};
pub use iter::{FromIterator, Extendable};
pub use iter::{Iterator, DoubleEndedIterator, RandomAccessIterator, ClonableIterator};
pub use iter::{OrdIterator, MutableDoubleEndedIterator, ExactSize};

View File

@ -22,7 +22,6 @@ use rt::io::{io_error, standard_error, EndOfFile, DEFAULT_BUF_SIZE};
use option::{Option, Some, None};
use unstable::finally::Finally;
use cast;
use io::{u64_to_le_bytes, u64_to_be_bytes};
pub trait ReaderUtil {
@ -634,6 +633,88 @@ fn extend_sign(val: u64, nbytes: uint) -> i64 {
(val << shift) as i64 >> shift
}
pub fn u64_to_le_bytes<T>(n: u64, size: uint,
f: &fn(v: &[u8]) -> T) -> T {
assert!(size <= 8u);
match size {
1u => f(&[n as u8]),
2u => f(&[n as u8,
(n >> 8) as u8]),
4u => f(&[n as u8,
(n >> 8) as u8,
(n >> 16) as u8,
(n >> 24) as u8]),
8u => f(&[n as u8,
(n >> 8) as u8,
(n >> 16) as u8,
(n >> 24) as u8,
(n >> 32) as u8,
(n >> 40) as u8,
(n >> 48) as u8,
(n >> 56) as u8]),
_ => {
let mut bytes: ~[u8] = ~[];
let mut i = size;
let mut n = n;
while i > 0u {
bytes.push((n & 255_u64) as u8);
n >>= 8_u64;
i -= 1u;
}
f(bytes)
}
}
}
pub fn u64_to_be_bytes<T>(n: u64, size: uint,
f: &fn(v: &[u8]) -> T) -> T {
assert!(size <= 8u);
match size {
1u => f(&[n as u8]),
2u => f(&[(n >> 8) as u8,
n as u8]),
4u => f(&[(n >> 24) as u8,
(n >> 16) as u8,
(n >> 8) as u8,
n as u8]),
8u => f(&[(n >> 56) as u8,
(n >> 48) as u8,
(n >> 40) as u8,
(n >> 32) as u8,
(n >> 24) as u8,
(n >> 16) as u8,
(n >> 8) as u8,
n as u8]),
_ => {
let mut bytes: ~[u8] = ~[];
let mut i = size;
while i > 0u {
let shift = ((i - 1u) * 8u) as u64;
bytes.push((n >> shift) as u8);
i -= 1u;
}
f(bytes)
}
}
}
pub fn u64_from_be_bytes(data: &[u8],
start: uint,
size: uint)
-> u64 {
let mut sz = size;
assert!((sz <= 8u));
let mut val = 0_u64;
let mut pos = start;
while sz > 0u {
sz -= 1u;
val += (data[pos] as u64) << ((sz * 8u) as u64);
pos += 1u;
}
return val;
}
#[cfg(test)]
mod test {
use super::ReaderUtil;

View File

@ -148,7 +148,6 @@ pub mod iter;
pub mod to_str;
pub mod to_bytes;
pub mod clone;
pub mod io;
pub mod hash;
pub mod container;
pub mod default;

View File

@ -16,8 +16,6 @@ The `ToBytes` and `IterBytes` traits
use cast;
use container::Container;
use io;
use io::Writer;
use iter::Iterator;
use option::{None, Option, Some};
use str::{Str, StrSlice};
@ -360,7 +358,10 @@ pub trait ToBytes {
impl<A:IterBytes> ToBytes for A {
fn to_bytes(&self, lsb0: bool) -> ~[u8] {
do io::with_bytes_writer |wr| {
use rt::io::mem;
use rt::io::Writer;
do mem::with_mem_writer |wr| {
do self.iter_bytes(lsb0) |bytes| {
wr.write(bytes);
true

View File

@ -10,8 +10,6 @@
// Tests that auto-ref can't create mutable aliases to immutable memory.
use std::io;
struct Foo {
x: int
}

View File

@ -15,14 +15,16 @@ extern mod extra;
use extra::glob::glob;
use extra::tempfile::TempDir;
use std::unstable::finally::Finally;
use std::{io, os, unstable};
use std::{os, unstable};
use std::rt::io;
use std::rt::io::file::FileInfo;
pub fn main() {
fn mk_file(path: &str, directory: bool) {
if directory {
os::make_dir(&Path::new(path), 0xFFFF);
} else {
io::mk_file_writer(&Path::new(path), [io::Create]);
Path::new(path).open_writer(io::Create);
}
}