Skip to content

Cleanup serialize #19980

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

Merged
merged 2 commits into from
Dec 22, 2014
Merged
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
49 changes: 12 additions & 37 deletions src/libserialize/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2016,7 +2016,6 @@ macro_rules! read_primitive {

impl ::Decoder<DecoderError> for Decoder {
fn read_nil(&mut self) -> DecodeResult<()> {
debug!("read_nil");
expect!(self.pop(), Null)
}

Expand All @@ -2034,7 +2033,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),
Expand All @@ -2053,7 +2051,6 @@ impl ::Decoder<DecoderError> for Decoder {
}

fn read_bool(&mut self) -> DecodeResult<bool> {
debug!("read_bool");
expect!(self.pop(), Boolean)
}

Expand All @@ -2071,22 +2068,19 @@ 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)
}

fn read_enum_variant<T, F>(&mut self, names: &[&str],
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) => {
Expand Down Expand Up @@ -2126,49 +2120,44 @@ 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)
}

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()) {
Expand All @@ -2193,7 +2182,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)
Expand All @@ -2206,18 +2194,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)
}

Expand All @@ -2227,14 +2213,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) }
Expand All @@ -2244,7 +2228,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() {
Expand All @@ -2253,17 +2236,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() {
Expand All @@ -2273,17 +2254,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)
}

Expand Down Expand Up @@ -2445,9 +2424,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};
Expand Down Expand Up @@ -2682,8 +2659,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()
Expand Down Expand Up @@ -2760,9 +2735,9 @@ mod tests {
fn test_write_char() {
check_encoder_for_simple!('a', "\"a\"");
check_encoder_for_simple!('\t', "\"\\t\"");
check_encoder_for_simple!('\u00a0', "\"\u00a0\"");
check_encoder_for_simple!('\uabcd', "\"\uabcd\"");
check_encoder_for_simple!('\U0010ffff', "\"\U0010ffff\"");
check_encoder_for_simple!('\u{00a0}', "\"\u{00a0}\"");
check_encoder_for_simple!('\u{abcd}', "\"\u{abcd}\"");
check_encoder_for_simple!('\u{10ffff}', "\"\u{10ffff}\"");
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

already done in #19974


#[test]
Expand Down