implement Writer for Vec<u8>
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.
This commit is contained in:
parent
9c96a79a74
commit
85c2c2e38c
24 changed files with 120 additions and 129 deletions
|
@ -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};
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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));
|
||||
|
@ -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));
|
||||
|
@ -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.
|
||||
|
@ -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]
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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();
|
||||
|
@ -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();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue