1
Fork 0

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

View file

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

View file

@ -13,7 +13,8 @@
use std::{os, str}; use std::{os, str};
use std::os::getenv; 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` /// Return path to database entry for `term`
pub fn get_dbpath_for_term(term: &str) -> Option<~Path> { 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` /// 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) { 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)) 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::u64;
use std::rt::io; use std::rt::io;
use std::rt::io::extensions::u64_from_be_bytes;
use std::option; use std::option;
use std::str; use std::str;
use std::vec; 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 index = reader::get_doc(d, tag_index);
let table = reader::get_doc(index, tag_index_table); let table = reader::get_doc(index, tag_index_table);
let hash_pos = table.start + (hash % 256 * 4) as uint; 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 tagged_doc = reader::doc_at(d.data, pos);
let belt = tag_index_buckets_bucket_elt; let belt = tag_index_buckets_bucket_elt;
let mut ret = None; let mut ret = None;
do reader::tagged_docs(tagged_doc.doc, belt) |elt| { 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)) { if eq_fn(elt.data.slice(elt.start + 4, elt.end)) {
ret = Some(reader::doc_at(d.data, pos).doc); ret = Some(reader::doc_at(d.data, pos).doc);
false 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> { pub fn maybe_find_item(item_id: int, items: ebml::Doc) -> Option<ebml::Doc> {
fn eq_item(bytes: &[u8], item_id: int) -> bool { 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 bytes.slice(0u, 4u), 0u, 4u) as int
== item_id; == item_id;
} }
@ -1253,7 +1254,7 @@ fn family_names_type(fam: Family) -> bool {
fn read_path(d: ebml::Doc) -> (~str, uint) { fn read_path(d: ebml::Doc) -> (~str, uint) {
do reader::with_doc_data(d) |desc| { 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 pathbytes = desc.slice(4u, desc.len());
let path = str::from_utf8(pathbytes); 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 default::Default;
pub use from_str::FromStr; pub use from_str::FromStr;
pub use hash::Hash; pub use hash::Hash;
pub use io::{Reader, ReaderUtil, Writer, WriterUtil};
pub use iter::{FromIterator, Extendable}; pub use iter::{FromIterator, Extendable};
pub use iter::{Iterator, DoubleEndedIterator, RandomAccessIterator, ClonableIterator}; pub use iter::{Iterator, DoubleEndedIterator, RandomAccessIterator, ClonableIterator};
pub use iter::{OrdIterator, MutableDoubleEndedIterator, ExactSize}; 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 option::{Option, Some, None};
use unstable::finally::Finally; use unstable::finally::Finally;
use cast; use cast;
use io::{u64_to_le_bytes, u64_to_be_bytes};
pub trait ReaderUtil { pub trait ReaderUtil {
@ -634,6 +633,88 @@ fn extend_sign(val: u64, nbytes: uint) -> i64 {
(val << shift) as i64 >> shift (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)] #[cfg(test)]
mod test { mod test {
use super::ReaderUtil; use super::ReaderUtil;

View file

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

View file

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

View file

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

View file

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