Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement Writer for Vec<u8> #18885

Merged
merged 1 commit into from
Nov 18, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/libgraphviz/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl<'a> dot::GraphWalk<'a, Nd, Ed> for Edges {
fn target(&self, e: &Ed) -> Nd { let &(_,t) = e; t }
}

# pub fn main() { use std::io::MemWriter; render_to(&mut MemWriter::new()) }
# pub fn main() { render_to(&mut Vec::new()) }
```

```no_run
Expand Down Expand Up @@ -182,7 +182,7 @@ impl<'a> dot::GraphWalk<'a, Nd, Ed<'a>> for Graph {
fn target(&self, e: &Ed) -> Nd { let & &(_,t) = e; t }
}

# pub fn main() { use std::io::MemWriter; render_to(&mut MemWriter::new()) }
# pub fn main() { render_to(&mut Vec::new()) }
```

```no_run
Expand Down Expand Up @@ -246,7 +246,7 @@ impl<'a> dot::GraphWalk<'a, Nd<'a>, Ed<'a>> for Graph {
fn target(&self, e: &Ed<'a>) -> Nd<'a> { let &(_,t) = e; t }
}

# pub fn main() { use std::io::MemWriter; render_to(&mut MemWriter::new()) }
# pub fn main() { render_to(&mut Vec::new()) }
```

```no_run
Expand Down Expand Up @@ -274,7 +274,7 @@ pub fn main() {
#![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
html_favicon_url = "http://www.rust-lang.org/favicon.ico",
html_root_url = "http://doc.rust-lang.org/nightly/")]
#![feature(globs)]
#![feature(globs, slicing_syntax)]

pub use self::LabelText::*;

Expand Down Expand Up @@ -553,7 +553,7 @@ mod tests {
use self::NodeLabels::*;
use super::{Id, LabelText, LabelStr, EscStr, Labeller};
use super::{Nodes, Edges, GraphWalk, render};
use std::io::{MemWriter, BufReader, IoResult};
use std::io::{BufReader, IoResult};
use std::str;

/// each node is an index in a vector in the graph.
Expand Down Expand Up @@ -702,9 +702,9 @@ mod tests {
}

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

Expand Down Expand Up @@ -809,15 +809,15 @@ r#"digraph hasse_diagram {
"branch2",
"afterward"));

let mut writer = MemWriter::new();
let mut writer = Vec::new();

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

render(&g, &mut writer).unwrap();
let mut r = BufReader::new(writer.get_ref());
let mut r = BufReader::new(writer[]);
let r = r.read_to_string();

assert_eq!(r.unwrap().as_slice(),
Expand Down
5 changes: 2 additions & 3 deletions src/librustc/middle/liveness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ use util::nodemap::NodeMap;
use std::fmt;
use std::io;
use std::rc::Rc;
use std::str;
use std::uint;
use syntax::ast;
use syntax::ast::*;
Expand Down Expand Up @@ -742,7 +741,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {

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

fn init_empty(&mut self, ln: LiveNode, succ_ln: LiveNode) {
Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/html/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ pub fn highlight(src: &str, class: Option<&str>, id: Option<&str>) -> String {
src.to_string(),
"<stdin>".to_string());

let mut out = io::MemWriter::new();
let mut out = Vec::new();
doit(&sess,
lexer::StringReader::new(&sess.span_diagnostic, fm),
class,
id,
&mut out).unwrap();
String::from_utf8_lossy(out.unwrap().as_slice()).into_string()
String::from_utf8_lossy(out[]).into_string()
}

/// Exhausts the `lexer` writing the output into `out`.
Expand Down
6 changes: 3 additions & 3 deletions src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use std::collections::{HashMap, HashSet};
use std::collections::hash_map::{Occupied, Vacant};
use std::fmt;
use std::io::fs::PathExtensions;
use std::io::{fs, File, BufferedWriter, MemWriter, BufferedReader};
use std::io::{fs, File, BufferedWriter, BufferedReader};
use std::io;
use std::str;
use std::string::String;
Expand Down Expand Up @@ -420,7 +420,7 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> io::IoResult<String>
}

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

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

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

Ok(String::from_utf8(w.unwrap()).unwrap())
Ok(String::from_utf8(w).unwrap())
}

fn write_shared(cx: &Context,
Expand Down
6 changes: 3 additions & 3 deletions src/librustdoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ extern crate "test" as testing;
#[phase(plugin, link)] extern crate log;

use std::io;
use std::io::{File, MemWriter};
use std::io::File;
use std::collections::HashMap;
use std::collections::hash_map::{Occupied, Vacant};
use serialize::{json, Decodable, Encodable};
Expand Down Expand Up @@ -467,12 +467,12 @@ fn json_output(krate: clean::Crate, res: Vec<plugins::PluginJson> ,
// FIXME #8335: yuck, Rust -> str -> JSON round trip! No way to .encode
// straight to the Rust JSON representation.
let crate_json_str = {
let mut w = MemWriter::new();
let mut w = Vec::new();
{
let mut encoder = json::Encoder::new(&mut w as &mut io::Writer);
krate.encode(&mut encoder).unwrap();
}
String::from_utf8(w.unwrap()).unwrap()
String::from_utf8(w).unwrap()
};
let crate_json = match json::from_str(crate_json_str.as_slice()) {
Ok(j) => j,
Expand Down
42 changes: 17 additions & 25 deletions src/libserialize/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ use self::InternalStackElement::*;
use std;
use std::collections::{HashMap, TreeMap};
use std::{char, f64, fmt, io, num, str};
use std::io::MemWriter;
use std::mem::{swap, transmute};
use std::num::{Float, FPNaN, FPInfinite, Int};
use std::str::{FromStr, ScalarValue};
Expand Down Expand Up @@ -412,14 +411,14 @@ impl<'a> Encoder<'a> {
/// Encode the specified struct into a json [u8]
pub fn buffer_encode<T:Encodable<Encoder<'a>, io::IoError>>(object: &T) -> Vec<u8> {
//Serialize the object in a string using a writer
let mut m = MemWriter::new();
let mut m = Vec::new();
// FIXME(14302) remove the transmute and unsafe block.
unsafe {
let mut encoder = Encoder::new(&mut m as &mut io::Writer);
// MemWriter never Errs
// Vec<u8> never Errs
let _ = object.encode(transmute(&mut encoder));
}
m.unwrap()
m
}
}

Expand Down Expand Up @@ -578,13 +577,13 @@ impl<'a> ::Encoder<io::IoError> for Encoder<'a> {
if idx != 0 { try!(write!(self.writer, ",")) }
// ref #12967, make sure to wrap a key in double quotes,
// in the event that its of a type that omits them (eg numbers)
let mut buf = MemWriter::new();
let mut buf = Vec::new();
// FIXME(14302) remove the transmute and unsafe block.
unsafe {
let mut check_encoder = Encoder::new(&mut buf);
try!(f(transmute(&mut check_encoder)));
}
let out = str::from_utf8(buf.get_ref()).unwrap();
let out = str::from_utf8(buf[]).unwrap();
let needs_wrapping = out.char_at(0) != '"' && out.char_at_reverse(out.len()) != '"';
if needs_wrapping { try!(write!(self.writer, "\"")); }
try!(f(self));
Expand Down Expand Up @@ -839,13 +838,13 @@ impl<'a> ::Encoder<io::IoError> for PrettyEncoder<'a> {
try!(spaces(self.writer, self.curr_indent));
// ref #12967, make sure to wrap a key in double quotes,
// in the event that its of a type that omits them (eg numbers)
let mut buf = MemWriter::new();
let mut buf = Vec::new();
// FIXME(14302) remove the transmute and unsafe block.
unsafe {
let mut check_encoder = PrettyEncoder::new(&mut buf);
try!(f(transmute(&mut check_encoder)));
}
let out = str::from_utf8(buf.get_ref()).unwrap();
let out = str::from_utf8(buf[]).unwrap();
let needs_wrapping = out.char_at(0) != '"' && out.char_at_reverse(out.len()) != '"';
if needs_wrapping { try!(write!(self.writer, "\"")); }
try!(f(self));
Expand Down Expand Up @@ -892,9 +891,9 @@ impl Json {

/// Encodes a json value into a string
pub fn to_pretty_str(&self) -> string::String {
let mut s = MemWriter::new();
let mut s = Vec::new();
self.to_pretty_writer(&mut s as &mut io::Writer).unwrap();
string::String::from_utf8(s.unwrap()).unwrap()
string::String::from_utf8(s).unwrap()
}

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

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

let mut m = MemWriter::new();
let mut m = Vec::new();
f(&mut m as &mut io::Writer);
str::from_utf8(m.unwrap().as_slice()).unwrap().to_string()
string::String::from_utf8(m).unwrap()
}

#[test]
Expand Down Expand Up @@ -3286,17 +3284,15 @@ mod tests {
fn test_encode_hashmap_with_numeric_key() {
use std::str::from_utf8;
use std::io::Writer;
use std::io::MemWriter;
use std::collections::HashMap;
let mut hm: HashMap<uint, bool> = HashMap::new();
hm.insert(1, true);
let mut mem_buf = MemWriter::new();
let mut mem_buf = Vec::new();
{
let mut encoder = Encoder::new(&mut mem_buf as &mut io::Writer);
hm.encode(&mut encoder).unwrap();
}
let bytes = mem_buf.unwrap();
let json_str = from_utf8(bytes.as_slice()).unwrap();
let json_str = from_utf8(mem_buf[]).unwrap();
match from_str(json_str) {
Err(_) => panic!("Unable to parse json_str: {}", json_str),
_ => {} // it parsed and we are good to go
Expand All @@ -3307,17 +3303,15 @@ mod tests {
fn test_prettyencode_hashmap_with_numeric_key() {
use std::str::from_utf8;
use std::io::Writer;
use std::io::MemWriter;
use std::collections::HashMap;
let mut hm: HashMap<uint, bool> = HashMap::new();
hm.insert(1, true);
let mut mem_buf = MemWriter::new();
let mut mem_buf = Vec::new();
{
let mut encoder = PrettyEncoder::new(&mut mem_buf as &mut io::Writer);
hm.encode(&mut encoder).unwrap()
}
let bytes = mem_buf.unwrap();
let json_str = from_utf8(bytes.as_slice()).unwrap();
let json_str = from_utf8(mem_buf[]).unwrap();
match from_str(json_str) {
Err(_) => panic!("Unable to parse json_str: {}", json_str),
_ => {} // it parsed and we are good to go
Expand All @@ -3327,7 +3321,6 @@ mod tests {
#[test]
fn test_prettyencoder_indent_level_param() {
use std::str::from_utf8;
use std::io::MemWriter;
use std::collections::TreeMap;

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

// Test up to 4 spaces of indents (more?)
for i in range(0, 4u) {
let mut writer = MemWriter::new();
let mut writer = Vec::new();
{
let ref mut encoder = PrettyEncoder::new(&mut writer);
encoder.set_indent(i);
json.encode(encoder).unwrap();
}

let bytes = writer.unwrap();
let printed = from_utf8(bytes.as_slice()).unwrap();
let printed = from_utf8(writer[]).unwrap();

// Check for indents at each line
let lines: Vec<&str> = printed.lines().collect();
Expand Down
11 changes: 6 additions & 5 deletions src/libstd/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ actually invoking the `write` function defined in this module. Example usage is:
# #![allow(unused_must_use)]
use std::io;

let mut w = io::MemWriter::new();
let mut w = Vec::new();
write!(&mut w as &mut io::Writer, "Hello {}!", "world");
```

Expand Down Expand Up @@ -415,6 +415,7 @@ use io::Writer;
use io;
use result::{Ok, Err};
use string;
use vec::Vec;

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

impl<'a> Writer for Formatter<'a> {
Expand Down
Loading