Skip to content

Commit fcb1523

Browse files
committed
auto merge of #18885 : thestinger/rust/writer, r=aturon
The trait has an obvious, sensible implementation directly on vectors so the MemWriter wrapper is unnecessary. This will halt the trend towards providing all of the vector methods on MemWriter along with eliminating the noise caused by conversions between the two types. It also provides the useful default Writer methods on Vec<u8>. After the type is removed and code has been migrated, it would make sense to add a new implementation of MemWriter with seeking support. The simple use cases can be covered with vectors alone, and ones with the need for seeks can use a new MemWriter implementation.
2 parents f637f1c + 85c2c2e commit fcb1523

24 files changed

+120
-129
lines changed

src/libgraphviz/lib.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ impl<'a> dot::GraphWalk<'a, Nd, Ed> for Edges {
9090
fn target(&self, e: &Ed) -> Nd { let &(_,t) = e; t }
9191
}
9292
93-
# pub fn main() { use std::io::MemWriter; render_to(&mut MemWriter::new()) }
93+
# pub fn main() { render_to(&mut Vec::new()) }
9494
```
9595
9696
```no_run
@@ -182,7 +182,7 @@ impl<'a> dot::GraphWalk<'a, Nd, Ed<'a>> for Graph {
182182
fn target(&self, e: &Ed) -> Nd { let & &(_,t) = e; t }
183183
}
184184
185-
# pub fn main() { use std::io::MemWriter; render_to(&mut MemWriter::new()) }
185+
# pub fn main() { render_to(&mut Vec::new()) }
186186
```
187187
188188
```no_run
@@ -246,7 +246,7 @@ impl<'a> dot::GraphWalk<'a, Nd<'a>, Ed<'a>> for Graph {
246246
fn target(&self, e: &Ed<'a>) -> Nd<'a> { let &(_,t) = e; t }
247247
}
248248
249-
# pub fn main() { use std::io::MemWriter; render_to(&mut MemWriter::new()) }
249+
# pub fn main() { render_to(&mut Vec::new()) }
250250
```
251251
252252
```no_run
@@ -274,7 +274,7 @@ pub fn main() {
274274
#![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
275275
html_favicon_url = "http://www.rust-lang.org/favicon.ico",
276276
html_root_url = "http://doc.rust-lang.org/nightly/")]
277-
#![feature(globs)]
277+
#![feature(globs, slicing_syntax)]
278278

279279
pub use self::LabelText::*;
280280

@@ -553,7 +553,7 @@ mod tests {
553553
use self::NodeLabels::*;
554554
use super::{Id, LabelText, LabelStr, EscStr, Labeller};
555555
use super::{Nodes, Edges, GraphWalk, render};
556-
use std::io::{MemWriter, BufReader, IoResult};
556+
use std::io::{BufReader, IoResult};
557557
use std::str;
558558

559559
/// each node is an index in a vector in the graph.
@@ -702,9 +702,9 @@ mod tests {
702702
}
703703

704704
fn test_input(g: LabelledGraph) -> IoResult<String> {
705-
let mut writer = MemWriter::new();
705+
let mut writer = Vec::new();
706706
render(&g, &mut writer).unwrap();
707-
let mut r = BufReader::new(writer.get_ref());
707+
let mut r = BufReader::new(writer[]);
708708
r.read_to_string()
709709
}
710710

@@ -809,15 +809,15 @@ r#"digraph hasse_diagram {
809809
"branch2",
810810
"afterward"));
811811

812-
let mut writer = MemWriter::new();
812+
let mut writer = Vec::new();
813813

814814
let g = LabelledGraphWithEscStrs::new(
815815
"syntax_tree", labels,
816816
vec!(edge(0, 1, "then"), edge(0, 2, "else"),
817817
edge(1, 3, ";"), edge(2, 3, ";" )));
818818

819819
render(&g, &mut writer).unwrap();
820-
let mut r = BufReader::new(writer.get_ref());
820+
let mut r = BufReader::new(writer[]);
821821
let r = r.read_to_string();
822822

823823
assert_eq!(r.unwrap().as_slice(),

src/librustc/middle/liveness.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ use util::nodemap::NodeMap;
122122
use std::fmt;
123123
use std::io;
124124
use std::rc::Rc;
125-
use std::str;
126125
use std::uint;
127126
use syntax::ast;
128127
use syntax::ast::*;
@@ -742,7 +741,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
742741

743742
#[allow(unused_must_use)]
744743
fn ln_str(&self, ln: LiveNode) -> String {
745-
let mut wr = io::MemWriter::new();
744+
let mut wr = Vec::new();
746745
{
747746
let wr = &mut wr as &mut io::Writer;
748747
write!(wr, "[ln({}) of kind {} reads", ln.get(), self.ir.lnk(ln));
@@ -751,7 +750,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
751750
self.write_vars(wr, ln, |idx| self.users[idx].writer);
752751
write!(wr, " precedes {}]", self.successors[ln.get()].to_string());
753752
}
754-
str::from_utf8(wr.unwrap().as_slice()).unwrap().to_string()
753+
String::from_utf8(wr).unwrap()
755754
}
756755

757756
fn init_empty(&mut self, ln: LiveNode, succ_ln: LiveNode) {

src/librustdoc/html/highlight.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ pub fn highlight(src: &str, class: Option<&str>, id: Option<&str>) -> String {
2828
src.to_string(),
2929
"<stdin>".to_string());
3030

31-
let mut out = io::MemWriter::new();
31+
let mut out = Vec::new();
3232
doit(&sess,
3333
lexer::StringReader::new(&sess.span_diagnostic, fm),
3434
class,
3535
id,
3636
&mut out).unwrap();
37-
String::from_utf8_lossy(out.unwrap().as_slice()).into_string()
37+
String::from_utf8_lossy(out[]).into_string()
3838
}
3939

4040
/// Exhausts the `lexer` writing the output into `out`.

src/librustdoc/html/render.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use std::collections::{HashMap, HashSet};
3838
use std::collections::hash_map::{Occupied, Vacant};
3939
use std::fmt;
4040
use std::io::fs::PathExtensions;
41-
use std::io::{fs, File, BufferedWriter, MemWriter, BufferedReader};
41+
use std::io::{fs, File, BufferedWriter, BufferedReader};
4242
use std::io;
4343
use std::str;
4444
use std::string::String;
@@ -420,7 +420,7 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> io::IoResult<String>
420420
}
421421

422422
// Collect the index into a string
423-
let mut w = MemWriter::new();
423+
let mut w = Vec::new();
424424
try!(write!(&mut w, r#"searchIndex['{}'] = {{"items":["#, krate.name));
425425

426426
let mut lastpath = "".to_string();
@@ -463,7 +463,7 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> io::IoResult<String>
463463

464464
try!(write!(&mut w, "]}};"));
465465

466-
Ok(String::from_utf8(w.unwrap()).unwrap())
466+
Ok(String::from_utf8(w).unwrap())
467467
}
468468

469469
fn write_shared(cx: &Context,

src/librustdoc/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ extern crate "test" as testing;
2828
#[phase(plugin, link)] extern crate log;
2929

3030
use std::io;
31-
use std::io::{File, MemWriter};
31+
use std::io::File;
3232
use std::collections::HashMap;
3333
use std::collections::hash_map::{Occupied, Vacant};
3434
use serialize::{json, Decodable, Encodable};
@@ -467,12 +467,12 @@ fn json_output(krate: clean::Crate, res: Vec<plugins::PluginJson> ,
467467
// FIXME #8335: yuck, Rust -> str -> JSON round trip! No way to .encode
468468
// straight to the Rust JSON representation.
469469
let crate_json_str = {
470-
let mut w = MemWriter::new();
470+
let mut w = Vec::new();
471471
{
472472
let mut encoder = json::Encoder::new(&mut w as &mut io::Writer);
473473
krate.encode(&mut encoder).unwrap();
474474
}
475-
String::from_utf8(w.unwrap()).unwrap()
475+
String::from_utf8(w).unwrap()
476476
};
477477
let crate_json = match json::from_str(crate_json_str.as_slice()) {
478478
Ok(j) => j,

src/libserialize/json.rs

+17-25
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,6 @@ use self::InternalStackElement::*;
206206
use std;
207207
use std::collections::{HashMap, TreeMap};
208208
use std::{char, f64, fmt, io, num, str};
209-
use std::io::MemWriter;
210209
use std::mem::{swap, transmute};
211210
use std::num::{Float, FPNaN, FPInfinite, Int};
212211
use std::str::{FromStr, ScalarValue};
@@ -412,14 +411,14 @@ impl<'a> Encoder<'a> {
412411
/// Encode the specified struct into a json [u8]
413412
pub fn buffer_encode<T:Encodable<Encoder<'a>, io::IoError>>(object: &T) -> Vec<u8> {
414413
//Serialize the object in a string using a writer
415-
let mut m = MemWriter::new();
414+
let mut m = Vec::new();
416415
// FIXME(14302) remove the transmute and unsafe block.
417416
unsafe {
418417
let mut encoder = Encoder::new(&mut m as &mut io::Writer);
419-
// MemWriter never Errs
418+
// Vec<u8> never Errs
420419
let _ = object.encode(transmute(&mut encoder));
421420
}
422-
m.unwrap()
421+
m
423422
}
424423
}
425424

@@ -578,13 +577,13 @@ impl<'a> ::Encoder<io::IoError> for Encoder<'a> {
578577
if idx != 0 { try!(write!(self.writer, ",")) }
579578
// ref #12967, make sure to wrap a key in double quotes,
580579
// in the event that its of a type that omits them (eg numbers)
581-
let mut buf = MemWriter::new();
580+
let mut buf = Vec::new();
582581
// FIXME(14302) remove the transmute and unsafe block.
583582
unsafe {
584583
let mut check_encoder = Encoder::new(&mut buf);
585584
try!(f(transmute(&mut check_encoder)));
586585
}
587-
let out = str::from_utf8(buf.get_ref()).unwrap();
586+
let out = str::from_utf8(buf[]).unwrap();
588587
let needs_wrapping = out.char_at(0) != '"' && out.char_at_reverse(out.len()) != '"';
589588
if needs_wrapping { try!(write!(self.writer, "\"")); }
590589
try!(f(self));
@@ -839,13 +838,13 @@ impl<'a> ::Encoder<io::IoError> for PrettyEncoder<'a> {
839838
try!(spaces(self.writer, self.curr_indent));
840839
// ref #12967, make sure to wrap a key in double quotes,
841840
// in the event that its of a type that omits them (eg numbers)
842-
let mut buf = MemWriter::new();
841+
let mut buf = Vec::new();
843842
// FIXME(14302) remove the transmute and unsafe block.
844843
unsafe {
845844
let mut check_encoder = PrettyEncoder::new(&mut buf);
846845
try!(f(transmute(&mut check_encoder)));
847846
}
848-
let out = str::from_utf8(buf.get_ref()).unwrap();
847+
let out = str::from_utf8(buf[]).unwrap();
849848
let needs_wrapping = out.char_at(0) != '"' && out.char_at_reverse(out.len()) != '"';
850849
if needs_wrapping { try!(write!(self.writer, "\"")); }
851850
try!(f(self));
@@ -892,9 +891,9 @@ impl Json {
892891

893892
/// Encodes a json value into a string
894893
pub fn to_pretty_str(&self) -> string::String {
895-
let mut s = MemWriter::new();
894+
let mut s = Vec::new();
896895
self.to_pretty_writer(&mut s as &mut io::Writer).unwrap();
897-
string::String::from_utf8(s.unwrap()).unwrap()
896+
string::String::from_utf8(s).unwrap()
898897
}
899898

900899
/// If the Json value is an Object, returns the value associated with the provided key.
@@ -2659,12 +2658,11 @@ mod tests {
26592658
}
26602659

26612660
fn with_str_writer(f: |&mut io::Writer|) -> string::String {
2662-
use std::io::MemWriter;
26632661
use std::str;
26642662

2665-
let mut m = MemWriter::new();
2663+
let mut m = Vec::new();
26662664
f(&mut m as &mut io::Writer);
2667-
str::from_utf8(m.unwrap().as_slice()).unwrap().to_string()
2665+
string::String::from_utf8(m).unwrap()
26682666
}
26692667

26702668
#[test]
@@ -3286,17 +3284,15 @@ mod tests {
32863284
fn test_encode_hashmap_with_numeric_key() {
32873285
use std::str::from_utf8;
32883286
use std::io::Writer;
3289-
use std::io::MemWriter;
32903287
use std::collections::HashMap;
32913288
let mut hm: HashMap<uint, bool> = HashMap::new();
32923289
hm.insert(1, true);
3293-
let mut mem_buf = MemWriter::new();
3290+
let mut mem_buf = Vec::new();
32943291
{
32953292
let mut encoder = Encoder::new(&mut mem_buf as &mut io::Writer);
32963293
hm.encode(&mut encoder).unwrap();
32973294
}
3298-
let bytes = mem_buf.unwrap();
3299-
let json_str = from_utf8(bytes.as_slice()).unwrap();
3295+
let json_str = from_utf8(mem_buf[]).unwrap();
33003296
match from_str(json_str) {
33013297
Err(_) => panic!("Unable to parse json_str: {}", json_str),
33023298
_ => {} // it parsed and we are good to go
@@ -3307,17 +3303,15 @@ mod tests {
33073303
fn test_prettyencode_hashmap_with_numeric_key() {
33083304
use std::str::from_utf8;
33093305
use std::io::Writer;
3310-
use std::io::MemWriter;
33113306
use std::collections::HashMap;
33123307
let mut hm: HashMap<uint, bool> = HashMap::new();
33133308
hm.insert(1, true);
3314-
let mut mem_buf = MemWriter::new();
3309+
let mut mem_buf = Vec::new();
33153310
{
33163311
let mut encoder = PrettyEncoder::new(&mut mem_buf as &mut io::Writer);
33173312
hm.encode(&mut encoder).unwrap()
33183313
}
3319-
let bytes = mem_buf.unwrap();
3320-
let json_str = from_utf8(bytes.as_slice()).unwrap();
3314+
let json_str = from_utf8(mem_buf[]).unwrap();
33213315
match from_str(json_str) {
33223316
Err(_) => panic!("Unable to parse json_str: {}", json_str),
33233317
_ => {} // it parsed and we are good to go
@@ -3327,7 +3321,6 @@ mod tests {
33273321
#[test]
33283322
fn test_prettyencoder_indent_level_param() {
33293323
use std::str::from_utf8;
3330-
use std::io::MemWriter;
33313324
use std::collections::TreeMap;
33323325

33333326
let mut tree = TreeMap::new();
@@ -3354,15 +3347,14 @@ mod tests {
33543347

33553348
// Test up to 4 spaces of indents (more?)
33563349
for i in range(0, 4u) {
3357-
let mut writer = MemWriter::new();
3350+
let mut writer = Vec::new();
33583351
{
33593352
let ref mut encoder = PrettyEncoder::new(&mut writer);
33603353
encoder.set_indent(i);
33613354
json.encode(encoder).unwrap();
33623355
}
33633356

3364-
let bytes = writer.unwrap();
3365-
let printed = from_utf8(bytes.as_slice()).unwrap();
3357+
let printed = from_utf8(writer[]).unwrap();
33663358

33673359
// Check for indents at each line
33683360
let lines: Vec<&str> = printed.lines().collect();

src/libstd/fmt.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ actually invoking the `write` function defined in this module. Example usage is:
256256
# #![allow(unused_must_use)]
257257
use std::io;
258258
259-
let mut w = io::MemWriter::new();
259+
let mut w = Vec::new();
260260
write!(&mut w as &mut io::Writer, "Hello {}!", "world");
261261
```
262262
@@ -415,6 +415,7 @@ use io::Writer;
415415
use io;
416416
use result::{Ok, Err};
417417
use string;
418+
use vec::Vec;
418419

419420
pub use core::fmt::{Formatter, Result, FormatWriter, rt};
420421
pub use core::fmt::{Show, Bool, Char, Signed, Unsigned, Octal, Binary};
@@ -443,10 +444,10 @@ pub use core::fmt::{argument, argumentstr, argumentuint};
443444
/// let s = format_args!(fmt::format, "Hello, {}!", "world");
444445
/// assert_eq!(s, "Hello, world!".to_string());
445446
/// ```
446-
pub fn format(args: &Arguments) -> string::String{
447-
let mut output = io::MemWriter::new();
448-
let _ = write!(&mut output, "{}", args);
449-
string::String::from_utf8(output.unwrap()).unwrap()
447+
pub fn format(args: &Arguments) -> string::String {
448+
let mut output = Vec::new();
449+
let _ = write!(&mut output as &mut Writer, "{}", args);
450+
string::String::from_utf8(output).unwrap()
450451
}
451452

452453
impl<'a> Writer for Formatter<'a> {

0 commit comments

Comments
 (0)