rollup merge of #19980: erickt/cleanup-serialize
This brings over some changes from [rustc-serialize](https://github.com/rust-lang/rustc-serialize). It makes sense to keep the two in sync until we finally remove libserialize, just to make sure they don't diverge from each other.
This commit is contained in:
commit
583112269a
|
@ -2012,7 +2012,6 @@ macro_rules! read_primitive {
|
|||
|
||||
impl ::Decoder<DecoderError> for Decoder {
|
||||
fn read_nil(&mut self) -> DecodeResult<()> {
|
||||
debug!("read_nil");
|
||||
expect!(self.pop(), Null)
|
||||
}
|
||||
|
||||
|
@ -2030,7 +2029,6 @@ impl ::Decoder<DecoderError> for Decoder {
|
|||
fn read_f32(&mut self) -> DecodeResult<f32> { self.read_f64().map(|x| x as f32) }
|
||||
|
||||
fn read_f64(&mut self) -> DecodeResult<f64> {
|
||||
debug!("read_f64");
|
||||
match self.pop() {
|
||||
Json::I64(f) => Ok(f as f64),
|
||||
Json::U64(f) => Ok(f as f64),
|
||||
|
@ -2049,7 +2047,6 @@ impl ::Decoder<DecoderError> for Decoder {
|
|||
}
|
||||
|
||||
fn read_bool(&mut self) -> DecodeResult<bool> {
|
||||
debug!("read_bool");
|
||||
expect!(self.pop(), Boolean)
|
||||
}
|
||||
|
||||
|
@ -2067,14 +2064,12 @@ impl ::Decoder<DecoderError> for Decoder {
|
|||
}
|
||||
|
||||
fn read_str(&mut self) -> DecodeResult<string::String> {
|
||||
debug!("read_str");
|
||||
expect!(self.pop(), String)
|
||||
}
|
||||
|
||||
fn read_enum<T, F>(&mut self, name: &str, f: F) -> DecodeResult<T> where
|
||||
fn read_enum<T, F>(&mut self, _name: &str, f: F) -> DecodeResult<T> where
|
||||
F: FnOnce(&mut Decoder) -> DecodeResult<T>,
|
||||
{
|
||||
debug!("read_enum({})", name);
|
||||
f(self)
|
||||
}
|
||||
|
||||
|
@ -2082,7 +2077,6 @@ impl ::Decoder<DecoderError> for Decoder {
|
|||
mut f: F) -> DecodeResult<T>
|
||||
where F: FnMut(&mut Decoder, uint) -> DecodeResult<T>,
|
||||
{
|
||||
debug!("read_enum_variant(names={})", names);
|
||||
let name = match self.pop() {
|
||||
Json::String(s) => s,
|
||||
Json::Object(mut o) => {
|
||||
|
@ -2122,36 +2116,32 @@ impl ::Decoder<DecoderError> for Decoder {
|
|||
f(self, idx)
|
||||
}
|
||||
|
||||
fn read_enum_variant_arg<T, F>(&mut self, idx: uint, f: F) -> DecodeResult<T> where
|
||||
fn read_enum_variant_arg<T, F>(&mut self, _idx: uint, f: F) -> DecodeResult<T> where
|
||||
F: FnOnce(&mut Decoder) -> DecodeResult<T>,
|
||||
{
|
||||
debug!("read_enum_variant_arg(idx={})", idx);
|
||||
f(self)
|
||||
}
|
||||
|
||||
fn read_enum_struct_variant<T, F>(&mut self, names: &[&str], f: F) -> DecodeResult<T> where
|
||||
F: FnMut(&mut Decoder, uint) -> DecodeResult<T>,
|
||||
{
|
||||
debug!("read_enum_struct_variant(names={})", names);
|
||||
self.read_enum_variant(names, f)
|
||||
}
|
||||
|
||||
|
||||
fn read_enum_struct_variant_field<T, F>(&mut self,
|
||||
name: &str,
|
||||
_name: &str,
|
||||
idx: uint,
|
||||
f: F)
|
||||
-> DecodeResult<T> where
|
||||
F: FnOnce(&mut Decoder) -> DecodeResult<T>,
|
||||
{
|
||||
debug!("read_enum_struct_variant_field(name={}, idx={})", name, idx);
|
||||
self.read_enum_variant_arg(idx, f)
|
||||
}
|
||||
|
||||
fn read_struct<T, F>(&mut self, name: &str, len: uint, f: F) -> DecodeResult<T> where
|
||||
fn read_struct<T, F>(&mut self, _name: &str, _len: uint, f: F) -> DecodeResult<T> where
|
||||
F: FnOnce(&mut Decoder) -> DecodeResult<T>,
|
||||
{
|
||||
debug!("read_struct(name={}, len={})", name, len);
|
||||
let value = try!(f(self));
|
||||
self.pop();
|
||||
Ok(value)
|
||||
|
@ -2159,12 +2149,11 @@ impl ::Decoder<DecoderError> for Decoder {
|
|||
|
||||
fn read_struct_field<T, F>(&mut self,
|
||||
name: &str,
|
||||
idx: uint,
|
||||
_idx: uint,
|
||||
f: F)
|
||||
-> DecodeResult<T> where
|
||||
F: FnOnce(&mut Decoder) -> DecodeResult<T>,
|
||||
{
|
||||
debug!("read_struct_field(name={}, idx={})", name, idx);
|
||||
let mut obj = try!(expect!(self.pop(), Object));
|
||||
|
||||
let value = match obj.remove(&name.to_string()) {
|
||||
|
@ -2189,7 +2178,6 @@ impl ::Decoder<DecoderError> for Decoder {
|
|||
fn read_tuple<T, F>(&mut self, tuple_len: uint, f: F) -> DecodeResult<T> where
|
||||
F: FnOnce(&mut Decoder) -> DecodeResult<T>,
|
||||
{
|
||||
debug!("read_tuple()");
|
||||
self.read_seq(move |d, len| {
|
||||
if len == tuple_len {
|
||||
f(d)
|
||||
|
@ -2202,18 +2190,16 @@ impl ::Decoder<DecoderError> for Decoder {
|
|||
fn read_tuple_arg<T, F>(&mut self, idx: uint, f: F) -> DecodeResult<T> where
|
||||
F: FnOnce(&mut Decoder) -> DecodeResult<T>,
|
||||
{
|
||||
debug!("read_tuple_arg(idx={})", idx);
|
||||
self.read_seq_elt(idx, f)
|
||||
}
|
||||
|
||||
fn read_tuple_struct<T, F>(&mut self,
|
||||
name: &str,
|
||||
_name: &str,
|
||||
len: uint,
|
||||
f: F)
|
||||
-> DecodeResult<T> where
|
||||
F: FnOnce(&mut Decoder) -> DecodeResult<T>,
|
||||
{
|
||||
debug!("read_tuple_struct(name={})", name);
|
||||
self.read_tuple(len, f)
|
||||
}
|
||||
|
||||
|
@ -2223,14 +2209,12 @@ impl ::Decoder<DecoderError> for Decoder {
|
|||
-> DecodeResult<T> where
|
||||
F: FnOnce(&mut Decoder) -> DecodeResult<T>,
|
||||
{
|
||||
debug!("read_tuple_struct_arg(idx={})", idx);
|
||||
self.read_tuple_arg(idx, f)
|
||||
}
|
||||
|
||||
fn read_option<T, F>(&mut self, mut f: F) -> DecodeResult<T> where
|
||||
F: FnMut(&mut Decoder, bool) -> DecodeResult<T>,
|
||||
{
|
||||
debug!("read_option()");
|
||||
match self.pop() {
|
||||
Json::Null => f(self, false),
|
||||
value => { self.stack.push(value); f(self, true) }
|
||||
|
@ -2240,7 +2224,6 @@ impl ::Decoder<DecoderError> for Decoder {
|
|||
fn read_seq<T, F>(&mut self, f: F) -> DecodeResult<T> where
|
||||
F: FnOnce(&mut Decoder, uint) -> DecodeResult<T>,
|
||||
{
|
||||
debug!("read_seq()");
|
||||
let array = try!(expect!(self.pop(), Array));
|
||||
let len = array.len();
|
||||
for v in array.into_iter().rev() {
|
||||
|
@ -2249,17 +2232,15 @@ impl ::Decoder<DecoderError> for Decoder {
|
|||
f(self, len)
|
||||
}
|
||||
|
||||
fn read_seq_elt<T, F>(&mut self, idx: uint, f: F) -> DecodeResult<T> where
|
||||
fn read_seq_elt<T, F>(&mut self, _idx: uint, f: F) -> DecodeResult<T> where
|
||||
F: FnOnce(&mut Decoder) -> DecodeResult<T>,
|
||||
{
|
||||
debug!("read_seq_elt(idx={})", idx);
|
||||
f(self)
|
||||
}
|
||||
|
||||
fn read_map<T, F>(&mut self, f: F) -> DecodeResult<T> where
|
||||
F: FnOnce(&mut Decoder, uint) -> DecodeResult<T>,
|
||||
{
|
||||
debug!("read_map()");
|
||||
let obj = try!(expect!(self.pop(), Object));
|
||||
let len = obj.len();
|
||||
for (key, value) in obj.into_iter() {
|
||||
|
@ -2269,17 +2250,15 @@ impl ::Decoder<DecoderError> for Decoder {
|
|||
f(self, len)
|
||||
}
|
||||
|
||||
fn read_map_elt_key<T, F>(&mut self, idx: uint, f: F) -> DecodeResult<T> where
|
||||
fn read_map_elt_key<T, F>(&mut self, _idx: uint, f: F) -> DecodeResult<T> where
|
||||
F: FnOnce(&mut Decoder) -> DecodeResult<T>,
|
||||
{
|
||||
debug!("read_map_elt_key(idx={})", idx);
|
||||
f(self)
|
||||
}
|
||||
|
||||
fn read_map_elt_val<T, F>(&mut self, idx: uint, f: F) -> DecodeResult<T> where
|
||||
fn read_map_elt_val<T, F>(&mut self, _idx: uint, f: F) -> DecodeResult<T> where
|
||||
F: FnOnce(&mut Decoder) -> DecodeResult<T>,
|
||||
{
|
||||
debug!("read_map_elt_val(idx={})", idx);
|
||||
f(self)
|
||||
}
|
||||
|
||||
|
@ -2441,9 +2420,7 @@ mod tests {
|
|||
use super::ParserError::*;
|
||||
use super::DecoderError::*;
|
||||
use super::JsonEvent::*;
|
||||
use super::ParserState::*;
|
||||
use super::StackElement::*;
|
||||
use super::InternalStackElement::*;
|
||||
use super::{PrettyEncoder, Json, from_str, DecodeResult, DecoderError, JsonEvent, Parser,
|
||||
StackElement, Stack, Encoder, Decoder};
|
||||
use std::{i64, u64, f32, f64, io};
|
||||
|
@ -2678,8 +2655,6 @@ mod tests {
|
|||
}
|
||||
|
||||
fn with_str_writer<F>(f: F) -> string::String where F: FnOnce(&mut io::Writer){
|
||||
use std::str;
|
||||
|
||||
let mut m = Vec::new();
|
||||
f(&mut m as &mut io::Writer);
|
||||
string::String::from_utf8(m).unwrap()
|
||||
|
|
Loading…
Reference in New Issue