Skip to content

Commit 34d7f05

Browse files
committed
Major clean-up of std::io
Use ifaces instead of objs, stop wrapping everything in two (or three) layers of no-value-added indirection, and remove some of the more pointless/outdated idioms from the code.
1 parent 807592e commit 34d7f05

31 files changed

+310
-417
lines changed

src/cargo/cargo.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import rustc::util::filesearch::get_cargo_root;
1010
import std::fs;
1111
import std::generic_os;
1212
import std::io;
13+
import io::writer_util;
1314
import std::json;
1415
import option;
1516
import option::{none, some};

src/comp/driver/driver.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11

2-
32
// -*- rust -*-
43
import metadata::{creader, cstore};
54
import syntax::parse::{parser};
@@ -13,6 +12,7 @@ import back::link;
1312
import core::{option, str, int, result};
1413
import result::{ok, err};
1514
import std::{fs, io, getopts};
15+
import io::reader_util;
1616
import option::{some, none};
1717
import getopts::{optopt, optmulti, optflag, optflagopt, opt_present};
1818
import back::{x86, x86_64};
@@ -84,7 +84,7 @@ fn parse_input(sess: session::session, cfg: ast::crate_cfg, input: str) ->
8484

8585
fn parse_input_src(sess: session::session, cfg: ast::crate_cfg, infile: str)
8686
-> {crate: @ast::crate, src: str} {
87-
let srcbytes = if infile != "-" {
87+
let src_stream = if infile != "-" {
8888
alt io::file_reader(infile) {
8989
result::ok(reader) { reader }
9090
result::err(e) {
@@ -93,7 +93,8 @@ fn parse_input_src(sess: session::session, cfg: ast::crate_cfg, infile: str)
9393
}
9494
} else {
9595
io::stdin()
96-
}.read_whole_stream();
96+
};
97+
let srcbytes = src_stream.read_whole_stream();
9798
let src = str::unsafe_from_bytes(srcbytes);
9899
let crate =
99100
parser::parse_crate_from_source_str(infile, src, cfg,

src/comp/driver/rustc.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use rustc;
55
import core::{option, str, vec, result};
66
import result::{ok, err};
77
import std::{io, getopts};
8+
import io::writer_util;
89
import option::{some, none};
910
import getopts::{opt_present};
1011
import rustc::driver::driver::*;

src/comp/metadata/creader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import front::attr;
77
import syntax::visit;
88
import syntax::codemap::span;
99
import util::{filesearch};
10-
import core::{either, vec, str, option};
1110
import std::{io, fs};
11+
import io::writer_util;
1212
import option::{none, some};
1313
import std::map::{hashmap, new_int_hash};
1414
import syntax::print::pprust;

src/comp/metadata/decoder.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Decoding metadata from a single crate's metadata
22

33
import std::{ebml, io};
4+
import io::writer_util;
45
import syntax::{ast, ast_util};
56
import front::attr;
67
import middle::ty;

src/comp/metadata/encoder.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Metadata encoding
22

3-
import core::{vec, str, uint};
43
import std::{io, ebml, map};
4+
import io::writer_util;
55
import syntax::ast::*;
66
import syntax::ast_util;
77
import syntax::ast_util::local_def;
@@ -191,7 +191,7 @@ fn encode_type_param_bounds(ebml_w: ebml::writer, ecx: @encode_ctxt,
191191
for param in params {
192192
ebml::start_tag(ebml_w, tag_items_data_item_ty_param_bounds);
193193
let bs = ecx.ccx.tcx.ty_param_bounds.get(param.id);
194-
tyencode::enc_bounds(io::new_writer(ebml_w.writer), ty_str_ctxt, bs);
194+
tyencode::enc_bounds(ebml_w.writer, ty_str_ctxt, bs);
195195
ebml::end_tag(ebml_w);
196196
}
197197
}
@@ -207,7 +207,7 @@ fn write_type(ecx: @encode_ctxt, ebml_w: ebml::writer, typ: ty::t) {
207207
@{ds: def_to_str,
208208
tcx: ecx.ccx.tcx,
209209
abbrevs: tyencode::ac_use_abbrevs(ecx.type_abbrevs)};
210-
tyencode::enc_ty(io::new_writer(ebml_w.writer), ty_str_ctxt, typ);
210+
tyencode::enc_ty(ebml_w.writer, ty_str_ctxt, typ);
211211
}
212212

213213
fn encode_type(ecx: @encode_ctxt, ebml_w: ebml::writer, typ: ty::t) {
@@ -522,7 +522,7 @@ fn create_index<T: copy>(index: [entry<T>], hash_fn: fn(T) -> uint) ->
522522

523523
fn encode_index<T>(ebml_w: ebml::writer, buckets: [@[entry<T>]],
524524
write_fn: fn(io::writer, T)) {
525-
let writer = io::new_writer(ebml_w.writer);
525+
let writer = ebml_w.writer;
526526
ebml::start_tag(ebml_w, tag_index);
527527
let bucket_locs: [uint] = [];
528528
ebml::start_tag(ebml_w, tag_index_buckets);
@@ -702,8 +702,8 @@ fn encode_metadata(cx: @crate_ctxt, crate: @crate) -> str {
702702
let abbrevs = ty::new_ty_hash();
703703
let ecx = @{ccx: cx, type_abbrevs: abbrevs};
704704

705-
let string_w = io::string_writer();
706-
let buf_w = string_w.get_writer().get_buf_writer();
705+
let buf = io::mk_mem_buffer();
706+
let buf_w = io::mem_buffer_writer(buf);
707707
let ebml_w = ebml::create_writer(buf_w);
708708

709709
encode_hash(ebml_w, cx.link_meta.extras_hash);
@@ -714,32 +714,31 @@ fn encode_metadata(cx: @crate_ctxt, crate: @crate) -> str {
714714
encode_crate_deps(ebml_w, cx.sess.get_cstore());
715715

716716
// Encode and index the paths.
717-
718717
ebml::start_tag(ebml_w, tag_paths);
719718
let paths_index = encode_item_paths(ebml_w, ecx, crate);
720719
let paths_buckets = create_index(paths_index, hash_path);
721720
encode_index(ebml_w, paths_buckets, write_str);
722721
ebml::end_tag(ebml_w);
723-
// Encode and index the items.
724722

723+
// Encode and index the items.
725724
ebml::start_tag(ebml_w, tag_items);
726725
let items_index = encode_info_for_items(ecx, ebml_w, crate.node.module);
727726
let items_buckets = create_index(items_index, hash_node_id);
728727
encode_index(ebml_w, items_buckets, write_int);
729728
ebml::end_tag(ebml_w);
729+
730730
// Pad this, since something (LLVM, presumably) is cutting off the
731731
// remaining % 4 bytes.
732-
733732
buf_w.write([0u8, 0u8, 0u8, 0u8]);
734-
ret string_w.get_str();
733+
io::mem_buffer_str(buf)
735734
}
736735

737736
// Get the encoded string for a type
738737
fn encoded_ty(tcx: ty::ctxt, t: ty::t) -> str {
739738
let cx = @{ds: def_to_str, tcx: tcx, abbrevs: tyencode::ac_no_abbrevs};
740-
let sw = io::string_writer();
741-
tyencode::enc_ty(sw.get_writer(), cx, t);
742-
ret sw.get_str();
739+
let buf = io::mk_mem_buffer();
740+
tyencode::enc_ty(io::mem_buffer_writer(buf), cx, t);
741+
ret io::mem_buffer_str(buf);
743742
}
744743

745744

src/comp/metadata/tyencode.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Type encoding
22

3-
import core::{int, uint};
43
import std::io;
4+
import io::writer_util;
55
import std::map::hashmap;
66
import option::{some, none};
77
import syntax::ast::*;
@@ -37,25 +37,24 @@ fn cx_uses_abbrevs(cx: @ctxt) -> bool {
3737
fn enc_ty(w: io::writer, cx: @ctxt, t: ty::t) {
3838
alt cx.abbrevs {
3939
ac_no_abbrevs. {
40-
let result_str: @str;
41-
alt cx.tcx.short_names_cache.find(t) {
42-
some(s) { result_str = s; }
40+
let result_str = alt cx.tcx.short_names_cache.find(t) {
41+
some(s) { *s }
4342
none. {
44-
let sw = io::string_writer();
45-
enc_sty(sw.get_writer(), cx, ty::struct(cx.tcx, t));
46-
result_str = @sw.get_str();
47-
cx.tcx.short_names_cache.insert(t, result_str);
43+
let buf = io::mk_mem_buffer();
44+
enc_sty(io::mem_buffer_writer(buf), cx, ty::struct(cx.tcx, t));
45+
cx.tcx.short_names_cache.insert(t, @io::mem_buffer_str(buf));
46+
io::mem_buffer_str(buf)
4847
}
49-
}
50-
w.write_str(*result_str);
48+
};
49+
w.write_str(result_str);
5150
}
5251
ac_use_abbrevs(abbrevs) {
5352
alt abbrevs.find(t) {
5453
some(a) { w.write_str(*a.s); ret; }
5554
none. {
56-
let pos = w.get_buf_writer().tell();
55+
let pos = w.tell();
5756
enc_sty(w, cx, ty::struct(cx.tcx, t));
58-
let end = w.get_buf_writer().tell();
57+
let end = w.tell();
5958
let len = end - pos;
6059
fn estimate_sz(u: uint) -> uint {
6160
let n = u;

src/comp/syntax/codemap.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import core::{vec, uint, str, option, result};
22
import std::{term, io};
3+
import io::writer_util;
34
import option::{some, none};
45

56
type filename = str;
@@ -126,11 +127,11 @@ fn print_diagnostic(topic: str, t: diagnostictype, msg: str) {
126127
io::stdout().write_str(#fmt["%s ", topic]);
127128
}
128129
if term::color_supported() {
129-
term::fg(io::stdout().get_buf_writer(), diagnosticcolor(t));
130+
term::fg(io::stdout(), diagnosticcolor(t));
130131
}
131132
io::stdout().write_str(#fmt["%s:", diagnosticstr(t)]);
132133
if term::color_supported() {
133-
term::reset(io::stdout().get_buf_writer());
134+
term::reset(io::stdout());
134135
}
135136
io::stdout().write_str(#fmt[" %s\n", msg]);
136137
}

src/comp/syntax/ext/log_syntax.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import option;
21
import base::*;
32
import syntax::ast;
3+
import std::io::writer_util;
44

55
fn expand_syntax_ext(cx: ext_ctxt, sp: codemap::span, arg: @ast::expr,
66
_body: option::t<str>) -> @ast::expr {

src/comp/syntax/parse/lexer.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
import core::{vec, str, option, either};
33
import std::io;
4+
import io::reader_util;
45
import option::{some, none};
56
import util::interner;
67
import util::interner::intern;

src/comp/syntax/print/pp.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
import core::{vec, str};
33
import std::io;
4+
import io::writer_util;
45

56
/*
67
* This pretty-printer is a direct reimplementation of Philip Karlton's

src/comp/syntax/print/pprust.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,25 +93,23 @@ fn path_to_str(&&p: @ast::path) -> str {
9393

9494
fn fun_to_str(decl: ast::fn_decl, name: ast::ident,
9595
params: [ast::ty_param]) -> str {
96-
let writer = io::string_writer();
97-
let s = rust_printer(writer.get_writer());
96+
let buffer = io::mk_mem_buffer();
97+
let s = rust_printer(io::mem_buffer_writer(buffer));
9898
print_fn(s, decl, name, params);
9999
eof(s.s);
100-
ret writer.get_str();
100+
io::mem_buffer_str(buffer)
101101
}
102102

103103
fn block_to_str(blk: ast::blk) -> str {
104-
let writer = io::string_writer();
105-
let s = rust_printer(writer.get_writer());
104+
let buffer = io::mk_mem_buffer();
105+
let s = rust_printer(io::mem_buffer_writer(buffer));
106106
// containing cbox, will be closed by print-block at }
107-
108107
cbox(s, indent_unit);
109108
// head-ibox, will be closed by print-block after {
110-
111109
ibox(s, 0u);
112110
print_block(s, blk);
113111
eof(s.s);
114-
ret writer.get_str();
112+
io::mem_buffer_str(buffer)
115113
}
116114

117115
fn meta_item_to_str(mi: ast::meta_item) -> str {
@@ -1597,11 +1595,11 @@ fn escape_str(st: str, to_escape: char) -> str {
15971595
}
15981596

15991597
fn to_str<T>(t: T, f: fn@(ps, T)) -> str {
1600-
let writer = io::string_writer();
1601-
let s = rust_printer(writer.get_writer());
1598+
let buffer = io::mk_mem_buffer();
1599+
let s = rust_printer(io::mem_buffer_writer(buffer));
16021600
f(s, t);
16031601
eof(s.s);
1604-
ret writer.get_str();
1602+
io::mem_buffer_str(buffer)
16051603
}
16061604

16071605
fn next_comment(s: ps) -> option::t<lexer::cmnt> {

src/compiletest/errors.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import option;
2-
import str;
31
import std::io;
2+
import io::reader_util;
43
import std::fs;
54

65
import common::config;

src/compiletest/header.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import option;
22
import str;
33
import std::io;
4+
import io::reader_util;
45
import std::fs;
56

67
import common::config;

src/compiletest/procsrv.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,12 @@
44
// child process. Because of that we have to use a complicated scheme with a
55
// dedicated server for spawning processes.
66

7-
import core::comm;
8-
import core::option;
9-
import task;
107
import std::generic_os::setenv;
118
import std::generic_os::getenv;
12-
import vec;
139
import std::os;
1410
import std::run;
1511
import std::io;
16-
import str;
12+
import io::writer_util;
1713
import comm::chan;
1814
import comm::port;
1915
import comm::send;
@@ -75,7 +71,7 @@ fn run(handle: handle, lib_path: str, prog: str, args: [str],
7571

7672
fn writeclose(fd: fd_t, s: option::t<str>) {
7773
if option::is_some(s) {
78-
let writer = io::new_writer(io::fd_buf_writer(fd, option::none));
74+
let writer = io::fd_writer(fd, false);
7975
writer.write_str(option::get(s));
8076
}
8177

@@ -85,7 +81,7 @@ fn writeclose(fd: fd_t, s: option::t<str>) {
8581
fn readclose(fd: fd_t) -> str {
8682
// Copied from run::program_output
8783
let file = os::fd_FILE(fd);
88-
let reader = io::new_reader(io::FILE_buf_reader(file, option::none));
84+
let reader = io::FILE_reader(file, false);
8985
let buf = "";
9086
while !reader.eof() {
9187
let bytes = reader.read_bytes(4096u);

src/compiletest/runtest.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import std::io;
2-
import str;
3-
import option;
2+
import io::writer_util;
43
import std::fs;
54
import std::os;
6-
import vec;
7-
import result;
85

96
import common::mode_run_pass;
107
import common::mode_run_fail;

src/compiletest/util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,5 @@ fn path_div() -> str { ";" }
3636

3737
fn logv(config: config, s: str) {
3838
log(debug, s);
39-
if config.verbose { io::stdout().write_line(s); }
39+
if config.verbose { io::println(s); }
4040
}

src/fuzzer/fuzzer.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import std::{fs, io};
2+
import io::writer_util;
23

34
import rustc::syntax::{ast, ast_util, fold, visit, codemap};
45
import rustc::syntax::parse::parser;
@@ -212,12 +213,12 @@ fn under(n: uint, it: block(uint)) {
212213
while i < n { it(i); i += 1u; }
213214
}
214215

215-
fn devnull() -> io::writer { std::io::string_writer().get_writer() }
216+
fn devnull() -> io::writer { io::mem_buffer_writer(io::mk_mem_buffer()) }
216217

217218
fn as_str(f: fn@(io::writer)) -> str {
218-
let w = std::io::string_writer();
219-
f(w.get_writer());
220-
ret w.get_str();
219+
let buf = io::mk_mem_buffer();
220+
f(io::mem_buffer_writer(buf));
221+
io::mem_buffer_str(buf)
221222
}
222223

223224
fn check_variants_of_ast(crate: ast::crate, codemap: codemap::codemap,

0 commit comments

Comments
 (0)