update to rust HEAD, switch to rustc_serialize

This commit is contained in:
Erick Tryzelaar
2015-01-04 17:18:50 -08:00
parent f1929ca86d
commit b98719a4a0
16 changed files with 219 additions and 182 deletions
+8 -6
View File
@@ -7,7 +7,7 @@ use de;
use super::error::{Error, ErrorCode};
#[deriving(PartialEq, Show)]
#[derive(PartialEq, Show)]
enum State {
// Parse a value.
Value,
@@ -37,7 +37,9 @@ pub struct Parser<Iter> {
buf: Vec<u8>,
}
impl<Iter: Iterator<u8>> Iterator<Result<de::Token, Error>> for Parser<Iter> {
impl<Iter: Iterator<Item=u8>> Iterator for Parser<Iter> {
type Item = Result<de::Token, Error>;
#[inline]
fn next(&mut self) -> Option<Result<de::Token, Error>> {
let state = match self.state_stack.pop() {
@@ -80,7 +82,7 @@ impl<Iter: Iterator<u8>> Iterator<Result<de::Token, Error>> for Parser<Iter> {
}
}
impl<Iter: Iterator<u8>> Parser<Iter> {
impl<Iter: Iterator<Item=u8>> Parser<Iter> {
/// Creates the JSON parser.
#[inline]
pub fn new(rdr: Iter) -> Parser<Iter> {
@@ -395,7 +397,7 @@ impl<Iter: Iterator<u8>> Parser<Iter> {
}
};
let buf = &mut [0u8, .. 4];
let buf = &mut [0u8; 4];
let len = c.encode_utf8(buf).unwrap_or(0);
self.buf.extend(buf.slice_to(len).iter().map(|b| *b));
}
@@ -515,7 +517,7 @@ impl<Iter: Iterator<u8>> Parser<Iter> {
}
}
impl<Iter: Iterator<u8>> de::Deserializer<Error> for Parser<Iter> {
impl<Iter: Iterator<Item=u8>> de::Deserializer<Error> for Parser<Iter> {
fn end_of_stream_error(&mut self) -> Error {
Error::SyntaxError(ErrorCode::EOFWhileParsingValue, self.line, self.col)
}
@@ -635,7 +637,7 @@ impl<Iter: Iterator<u8>> de::Deserializer<Error> for Parser<Iter> {
/// Decodes a json value from an `Iterator<u8>`.
pub fn from_iter<
Iter: Iterator<u8>,
Iter: Iterator<Item=u8>,
T: de::Deserialize<Parser<Iter>, Error>
>(iter: Iter) -> Result<T, Error> {
let mut parser = Parser::new(iter);
+2 -2
View File
@@ -5,7 +5,7 @@ use std::io;
use de::{Token, TokenKind};
/// The errors that can arise while parsing a JSON stream.
#[deriving(Clone, PartialEq)]
#[derive(Clone, PartialEq)]
pub enum ErrorCode {
ConversionError(Token),
EOFWhileParsingList,
@@ -78,7 +78,7 @@ impl fmt::Show for ErrorCode {
}
}
#[deriving(Clone, PartialEq, Show)]
#[derive(Clone, PartialEq, Show)]
pub enum Error {
/// msg, line, col
SyntaxError(ErrorCode, uint, uint),
+31 -31
View File
@@ -55,12 +55,12 @@ of values to and from JSON via the serialization API.
To be able to serialize a piece of data, it must implement the `serde::Serialize` trait.
To be able to deserialize a piece of data, it must implement the `serde::Deserialize` trait.
The Rust compiler provides an annotation to automatically generate
the code for these traits: `#[deriving_serialize]` and `#[deriving_deserialize]`.
the code for these traits: `#[derive_serialize]` and `#[derive_deserialize]`.
To serialize using `Serialize`:
```rust
#![feature(phase)]
#![feature(phase, old_orphan_check)]
#[phase(plugin)]
extern crate serde_macros;
extern crate serde;
@@ -69,7 +69,7 @@ use std::io::ByRefWriter;
use serde::json;
use serde::Serialize;
#[deriving_serialize]
#[derive_serialize]
pub struct TestStruct {
data_str: String,
}
@@ -110,7 +110,7 @@ A basic `ToJson` example using a BTreeMap of attribute name / attribute value:
```rust
#![feature(phase)]
#![feature(phase, old_orphan_check)]
#[phase(plugin)]
extern crate serde_macros;
extern crate serde;
@@ -142,7 +142,7 @@ fn main() {
Or you can use the helper type `ObjectBuilder`:
```rust
#![feature(phase)]
#![feature(phase, old_orphan_check)]
#[phase(plugin)]
extern crate serde_macros;
extern crate serde;
@@ -173,7 +173,7 @@ fn main() {
To deserialize a JSON string using `Deserialize` trait:
```rust
#![feature(phase)]
#![feature(phase, old_orphan_check)]
#[phase(plugin)]
extern crate serde_macros;
extern crate serde;
@@ -181,7 +181,7 @@ extern crate serde;
use serde::json;
use serde::Deserialize;
#[deriving_deserialize]
#[derive_deserialize]
pub struct MyStruct {
attr1: u8,
attr2: String,
@@ -205,15 +205,15 @@ Create a struct called `TestStruct1` and serialize and deserialize it to and fro
using the serialization API, using the derived serialization code.
```rust
#![feature(phase)]
#![feature(phase, old_orphan_check)]
#[phase(plugin)]
extern crate serde_macros;
extern crate serde;
use serde::json;
#[deriving_serialize]
#[deriving_deserialize]
#[derive_serialize]
#[derive_deserialize]
pub struct TestStruct1 {
data_int: u8,
data_str: String,
@@ -245,7 +245,7 @@ This example use the ToJson impl to deserialize the JSON string.
Example of `ToJson` trait implementation for TestStruct1.
```rust
#![feature(phase)]
#![feature(phase, old_orphan_check)]
#[phase(plugin)]
extern crate serde_macros;
extern crate serde;
@@ -254,8 +254,8 @@ use serde::json::ToJson;
use serde::json;
use serde::Deserialize;
#[deriving_serialize] // generate Serialize impl
#[deriving_deserialize] // generate Deserialize impl
#[derive_serialize] // generate Serialize impl
#[derive_deserialize] // generate Deserialize impl
pub struct TestStruct1 {
data_int: u8,
data_str: String,
@@ -357,9 +357,9 @@ mod tests {
})
}
#[deriving(PartialEq, Show)]
#[deriving_serialize]
#[deriving_deserialize]
#[derive(PartialEq, Show)]
#[derive_serialize]
#[derive_deserialize]
enum Animal {
Dog,
Frog(String, Vec<int>)
@@ -386,9 +386,9 @@ mod tests {
}
}
#[deriving(PartialEq, Show)]
#[deriving_serialize]
#[deriving_deserialize]
#[derive(PartialEq, Show)]
#[derive_serialize]
#[derive_deserialize]
struct Inner {
a: (),
b: uint,
@@ -407,9 +407,9 @@ mod tests {
}
}
#[deriving(PartialEq, Show)]
#[deriving_serialize]
#[deriving_deserialize]
#[derive(PartialEq, Show)]
#[derive_serialize]
#[derive_deserialize]
struct Outer {
inner: Vec<Inner>,
}
@@ -1103,9 +1103,9 @@ mod tests {
("\"jodhpurs\"", Some("jodhpurs".to_string())),
]);
#[deriving(PartialEq, Show)]
#[deriving_serialize]
#[deriving_deserialize]
#[derive(PartialEq, Show)]
#[derive_serialize]
#[derive_deserialize]
struct Foo {
x: Option<int>,
}
@@ -1177,23 +1177,23 @@ mod tests {
}
/*
#[deriving(Decodable)]
#[derive(Decodable)]
struct DecodeStruct {
x: f64,
y: bool,
z: String,
w: Vec<DecodeStruct>
}
#[deriving(Decodable)]
#[derive(Decodable)]
enum DecodeEnum {
A(f64),
B(String)
}
fn check_err<T: Decodable<Decoder, DecoderError>>(to_parse: &'static str,
fn check_err<T: RustcDecodable<Decoder, DecoderError>>(to_parse: &'static str,
expected: DecoderError) {
let res: DecodeResult<T> = match from_str(to_parse) {
Err(e) => Err(ParseError(e)),
Ok(json) => Decodable::decode(&mut Decoder::new(json))
Ok(json) => RustcDecodable::decode(&mut Decoder::new(json))
};
match res {
Ok(_) => panic!("`{}` parsed & decoded ok, expecting error `{}`",
@@ -1792,7 +1792,7 @@ mod bench {
let json = encoder_json(count);
b.iter(|| {
assert_eq!(json.to_string(), src);
assert_eq!(json.pretty().to_string(), src);
});
}
@@ -1801,7 +1801,7 @@ mod bench {
let json = encoder_json(count);
b.iter(|| {
assert_eq!(json.to_pretty_str(), src);
assert_eq!(json.pretty().to_string(), src);
});
}
+7 -7
View File
@@ -45,7 +45,7 @@ pub fn escape_str<W: Writer>(wr: &mut W, v: &str) -> IoResult<()> {
}
fn escape_char<W: Writer>(wr: &mut W, v: char) -> IoResult<()> {
let buf = &mut [0, .. 4];
let buf = &mut [0; 4];
v.encode_utf8(buf);
escape_bytes(wr, buf)
}
@@ -66,7 +66,7 @@ fn fmt_f64_or_null<W: Writer>(wr: &mut W, v: f64) -> IoResult<()> {
fn spaces<W: Writer>(wr: &mut W, mut n: uint) -> IoResult<()> {
const LEN: uint = 16;
const BUF: &'static [u8, ..LEN] = &[b' ', ..LEN];
const BUF: &'static [u8; LEN] = &[b' '; LEN];
while n >= LEN {
try!(wr.write(BUF));
@@ -81,7 +81,7 @@ fn spaces<W: Writer>(wr: &mut W, mut n: uint) -> IoResult<()> {
}
/*
#[deriving(Show)]
#[derive(Show)]
enum SerializerState {
ValueState,
TupleState,
@@ -287,7 +287,7 @@ impl<W: Writer> ser::Serializer<IoError> for Serializer<W> {
#[inline]
fn serialize_seq<
T: Serialize<Serializer<W>, IoError>,
Iter: Iterator<T>
Iter: Iterator<Item=T>
>(&mut self, mut iter: Iter) -> IoResult<()> {
try!(self.wr.write_str("["));
let mut first = true;
@@ -307,7 +307,7 @@ impl<W: Writer> ser::Serializer<IoError> for Serializer<W> {
fn serialize_map<
K: Serialize<Serializer<W>, IoError>,
V: Serialize<Serializer<W>, IoError>,
Iter: Iterator<(K, V)>
Iter: Iterator<Item=(K, V)>
>(&mut self, mut iter: Iter) -> IoResult<()> {
try!(self.wr.write_str("{"));
let mut first = true;
@@ -541,7 +541,7 @@ impl<W: Writer> ser::Serializer<IoError> for PrettySerializer<W> {
#[inline]
fn serialize_seq<
T: Serialize<PrettySerializer<W>, IoError>,
Iter: Iterator<T>
Iter: Iterator<Item=T>
>(&mut self, mut iter: Iter) -> IoResult<()> {
try!(self.wr.write_str("["));
@@ -558,7 +558,7 @@ impl<W: Writer> ser::Serializer<IoError> for PrettySerializer<W> {
fn serialize_map<
K: Serialize<PrettySerializer<W>, IoError>,
V: Serialize<PrettySerializer<W>, IoError>,
Iter: Iterator<(K, V)>
Iter: Iterator<Item=(K, V)>
>(&mut self, mut iter: Iter) -> IoResult<()> {
try!(self.wr.write_str("{"));
+11 -7
View File
@@ -5,7 +5,7 @@ use std::io;
use std::str;
use std::vec;
use de::{mod, Token, TokenKind};
use de::{self, Token, TokenKind};
use ser::Serialize;
use ser;
@@ -13,7 +13,7 @@ use super::ser::{Serializer, PrettySerializer};
use super::error::{Error, ErrorCode};
/// Represents a JSON value
#[deriving(Clone, PartialEq, PartialOrd)]
#[derive(Clone, PartialEq, PartialOrd)]
pub enum Value {
Null,
Boolean(bool),
@@ -207,19 +207,21 @@ impl Value {
}
}
struct WriterFormatter<'a, 'b: 'a>(&'a mut fmt::Formatter<'b>);
struct WriterFormatter<'a, 'b: 'a> {
inner: &'a mut fmt::Formatter<'b>,
}
impl<'a, 'b> Writer for WriterFormatter<'a, 'b> {
fn write(&mut self, buf: &[u8]) -> IoResult<()> {
let WriterFormatter(ref mut f) = *self;
f.write(buf).map_err(|_| io::IoError::last_error())
self.inner.write_str(str::from_utf8(buf).unwrap()).map_err(|_| io::IoError::last_error())
}
}
impl fmt::Show for Value {
/// Serializes a json value into a string
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.to_writer(WriterFormatter(f)).map_err(|_| fmt::Error)
let wr = WriterFormatter { inner: f };
self.to_writer(wr).map_err(|_| fmt::Error)
}
}
@@ -320,7 +322,9 @@ impl Deserializer {
}
}
impl Iterator<Result<Token, Error>> for Deserializer {
impl Iterator for Deserializer {
type Item = Result<Token, Error>;
#[inline]
fn next(&mut self) -> Option<Result<Token, Error>> {
loop {