From b98719a4a063a848d3fe8554f5dfdb67ba043704 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Sun, 4 Jan 2015 17:18:50 -0800 Subject: [PATCH] update to rust HEAD, switch to rustc_serialize --- Cargo.toml | 3 ++ benches/bench_enum.rs | 18 +++---- benches/bench_log.rs | 103 +++++++++++++++++++++------------------- benches/bench_map.rs | 10 ++-- benches/bench_struct.rs | 26 +++++----- benches/bench_vec.rs | 24 ++++++---- serde_macros/src/lib.rs | 21 ++++---- src/de.rs | 45 +++++++++++------- src/json/de.rs | 14 +++--- src/json/error.rs | 4 +- src/json/mod.rs | 62 ++++++++++++------------ src/json/ser.rs | 14 +++--- src/json/value.rs | 18 ++++--- src/lib.rs | 3 ++ src/ser.rs | 28 +++++------ tests/json_struct.rs | 8 ++-- 16 files changed, 219 insertions(+), 182 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d9d4742c..22a05777 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,5 +8,8 @@ license = "MIT/Apache-2.0" name = "serde" path = "src/lib.rs" +[dependencies] +rustc-serialize = "*" + [dependencies.serde_macros] path = "serde_macros/" diff --git a/benches/bench_enum.rs b/benches/bench_enum.rs index 9ded96c5..4170d299 100644 --- a/benches/bench_enum.rs +++ b/benches/bench_enum.rs @@ -1,15 +1,15 @@ -#![feature(phase)] +#![feature(associated_types, phase, old_orphan_check)] #[phase(plugin)] extern crate serde_macros; extern crate serde; -extern crate serialize; +extern crate "rustc-serialize" as rustc_serialize; extern crate test; use test::Bencher; -use serialize::{Decoder, Decodable}; +use rustc_serialize::{Decoder, Decodable}; use serde::de::{Deserializer, Deserialize}; @@ -17,8 +17,8 @@ use Animal::{Dog, Frog}; ////////////////////////////////////////////////////////////////////////////// -#[deriving(Clone, PartialEq, Show, Decodable)] -#[deriving_deserialize] +#[derive(Clone, PartialEq, Show, RustcDecodable)] +#[derive_deserialize] enum Animal { Dog, Frog(String, int) @@ -26,7 +26,7 @@ enum Animal { ////////////////////////////////////////////////////////////////////////////// -#[deriving(Show)] +#[derive(Show)] pub enum Error { EndOfStream, SyntaxError, @@ -36,7 +36,7 @@ pub enum Error { ////////////////////////////////////////////////////////////////////////////// mod decoder { - use serialize::Decoder; + use rustc_serialize::Decoder; use super::{Animal, Error}; use super::Animal::{Dog, Frog}; @@ -265,7 +265,9 @@ mod deserializer { } } - impl Iterator> for AnimalDeserializer { + impl Iterator for AnimalDeserializer { + type Item = Result; + #[inline] fn next(&mut self) -> Option> { match self.stack.pop() { diff --git a/benches/bench_log.rs b/benches/bench_log.rs index 7fd3aadc..3bb1ac2a 100644 --- a/benches/bench_log.rs +++ b/benches/bench_log.rs @@ -1,16 +1,17 @@ -#![feature(phase, macro_rules)] +#![feature(phase, macro_rules, old_orphan_check)] #![allow(non_camel_case_types)] #[phase(plugin)] extern crate serde_macros; extern crate serde; -extern crate serialize; +extern crate "rustc-serialize" as rustc_serialize; extern crate test; -use std::io; use std::io::ByRefWriter; use std::io::extensions::Bytes; +use std::io; +use std::num::FromPrimitive; use test::Bencher; use serde::de; @@ -19,11 +20,11 @@ use serde::json; use serde::ser::Serialize; use serde::ser; -use serialize::Encodable; +use rustc_serialize::Encodable; -#[deriving(Show, PartialEq, Encodable, Decodable)] -#[deriving_serialize] -#[deriving_deserialize] +#[derive(Show, PartialEq, RustcEncodable, RustcDecodable)] +#[derive_serialize] +#[derive_deserialize] struct Http { protocol: HttpProtocol, status: u32, @@ -36,20 +37,20 @@ struct Http { request_uri: String, } -#[deriving(Copy, Show, PartialEq, FromPrimitive)] +#[derive(Copy, Show, PartialEq, FromPrimitive)] enum HttpProtocol { HTTP_PROTOCOL_UNKNOWN, HTTP10, HTTP11, } -impl, E> serialize::Encodable for HttpProtocol { +impl, E> rustc_serialize::Encodable for HttpProtocol { fn encode(&self, s: &mut S) -> Result<(), E> { (*self as uint).encode(s) } } -impl, E> serialize::Decodable for HttpProtocol { +impl, E> rustc_serialize::Decodable for HttpProtocol { fn decode(d: &mut D) -> Result { match FromPrimitive::from_uint(try!(d.read_uint())) { Some(value) => Ok(value), @@ -72,7 +73,7 @@ impl, E> de::Deserialize for HttpProtocol { } } -#[deriving(Copy, Show, PartialEq, FromPrimitive)] +#[derive(Copy, Show, PartialEq, FromPrimitive)] enum HttpMethod { METHOD_UNKNOWN, GET, @@ -87,13 +88,13 @@ enum HttpMethod { PATCH, } -impl, E> serialize::Encodable for HttpMethod { +impl, E> rustc_serialize::Encodable for HttpMethod { fn encode(&self, s: &mut S) -> Result<(), E> { (*self as uint).encode(s) } } -impl, E> serialize::Decodable for HttpMethod { +impl, E> rustc_serialize::Decodable for HttpMethod { fn decode(d: &mut D) -> Result { match FromPrimitive::from_uint(try!(d.read_uint())) { Some(value) => Ok(value), @@ -116,7 +117,7 @@ impl, E> de::Deserialize for HttpMethod { } } -#[deriving(Copy, Show, PartialEq, FromPrimitive)] +#[derive(Copy, Show, PartialEq, FromPrimitive)] enum CacheStatus { CACHESTATUS_UNKNOWN, Miss, @@ -124,13 +125,13 @@ enum CacheStatus { Hit, } -impl, E> serialize::Encodable for CacheStatus { +impl, E> rustc_serialize::Encodable for CacheStatus { fn encode(&self, s: &mut S) -> Result<(), E> { (*self as uint).encode(s) } } -impl, E> serialize::Decodable for CacheStatus { +impl, E> rustc_serialize::Decodable for CacheStatus { fn decode(d: &mut D) -> Result { match FromPrimitive::from_uint(try!(d.read_uint())) { Some(value) => Ok(value), @@ -153,9 +154,9 @@ impl, E> de::Deserialize for CacheStatus { } } -#[deriving(Show, PartialEq, Encodable, Decodable)] -#[deriving_serialize] -#[deriving_deserialize] +#[derive(Show, PartialEq, RustcEncodable, RustcDecodable)] +#[derive_serialize] +#[derive_deserialize] struct Origin { ip: String, port: u32, @@ -163,20 +164,20 @@ struct Origin { protocol: OriginProtocol, } -#[deriving(Copy, Show, PartialEq, FromPrimitive)] +#[derive(Copy, Show, PartialEq, FromPrimitive)] enum OriginProtocol { ORIGIN_PROTOCOL_UNKNOWN, HTTP, HTTPS, } -impl, E> serialize::Encodable for OriginProtocol { +impl, E> rustc_serialize::Encodable for OriginProtocol { fn encode(&self, s: &mut S) -> Result<(), E> { (*self as uint).encode(s) } } -impl, E> serialize::Decodable for OriginProtocol { +impl, E> rustc_serialize::Decodable for OriginProtocol { fn decode(d: &mut D) -> Result { match FromPrimitive::from_uint(try!(d.read_uint())) { Some(value) => Ok(value), @@ -199,7 +200,7 @@ impl, E> de::Deserialize for OriginProtocol { } } -#[deriving(Copy, Show, PartialEq, FromPrimitive)] +#[derive(Copy, Show, PartialEq, FromPrimitive)] enum ZonePlan { ZONEPLAN_UNKNOWN, FREE, @@ -208,13 +209,13 @@ enum ZonePlan { ENT, } -impl, E> serialize::Encodable for ZonePlan { +impl, E> rustc_serialize::Encodable for ZonePlan { fn encode(&self, s: &mut S) -> Result<(), E> { (*self as uint).encode(s) } } -impl, E> serialize::Decodable for ZonePlan { +impl, E> rustc_serialize::Decodable for ZonePlan { fn decode(d: &mut D) -> Result { match FromPrimitive::from_uint(try!(d.read_uint())) { Some(value) => Ok(value), @@ -237,7 +238,7 @@ impl, E> de::Deserialize for ZonePlan { } } -#[deriving(Copy, Show, PartialEq, FromPrimitive)] +#[derive(Copy, Show, PartialEq, FromPrimitive)] enum Country { UNKNOWN, A1, @@ -497,13 +498,13 @@ enum Country { ZW, } -impl, E> serialize::Encodable for Country { +impl, E> rustc_serialize::Encodable for Country { fn encode(&self, s: &mut S) -> Result<(), E> { (*self as uint).encode(s) } } -impl, E> serialize::Decodable for Country { +impl, E> rustc_serialize::Decodable for Country { fn decode(d: &mut D) -> Result { match FromPrimitive::from_uint(try!(d.read_uint())) { Some(value) => Ok(value), @@ -526,9 +527,9 @@ impl, E> de::Deserialize for Country { } } -#[deriving(Show, PartialEq, Encodable, Decodable)] -#[deriving_serialize] -#[deriving_deserialize] +#[derive(Show, PartialEq, RustcEncodable, RustcDecodable)] +#[derive_serialize] +#[derive_deserialize] struct Log { timestamp: i64, zone_id: u32, @@ -669,14 +670,14 @@ const JSON_STR: &'static str = r#"{"timestamp":2837513946597,"zone_id":123456,"z #[test] fn test_encoder() { - use serialize::Encodable; + use rustc_serialize::Encodable; let log = Log::new(); let mut wr = Vec::with_capacity(1024); { - let mut encoder = serialize::json::Encoder::new(&mut wr as &mut Writer); + let mut encoder = rustc_serialize::json::Encoder::new(&mut wr); log.encode(&mut encoder).unwrap(); } @@ -690,7 +691,7 @@ fn bench_encoder(b: &mut Bencher) { let mut wr = Vec::with_capacity(1024); { - let mut encoder = serialize::json::Encoder::new(&mut wr as &mut Writer); + let mut encoder = rustc_serialize::json::Encoder::new(&mut wr); log.encode(&mut encoder).unwrap(); } @@ -699,7 +700,7 @@ fn bench_encoder(b: &mut Bencher) { b.iter(|| { wr.clear(); - let mut encoder = serialize::json::Encoder::new(&mut wr as &mut Writer); + let mut encoder = rustc_serialize::json::Encoder::new(&mut wr); log.encode(&mut encoder).unwrap(); }); } @@ -756,7 +757,7 @@ fn bench_serializer_slice(b: &mut Bencher) { let json = json::to_vec(&log); b.bytes = json.len() as u64; - let mut buf = [0, .. 1024]; + let mut buf = [0; 1024]; b.iter(|| { for item in buf.iter_mut(){ *item = 0; } @@ -1259,20 +1260,24 @@ fn bench_direct_my_mem_writer1(b: &mut Bencher) { #[test] fn test_decoder() { - let json = serialize::json::from_str(JSON_STR).unwrap(); - let mut decoder = serialize::json::Decoder::new(json); - let log: Log = serialize::Decodable::decode(&mut decoder).unwrap(); + use rustc_serialize::json::Json; + + let json = Json::from_str(JSON_STR).unwrap(); + let mut decoder = rustc_serialize::json::Decoder::new(json); + let log: Log = rustc_serialize::Decodable::decode(&mut decoder).unwrap(); assert_eq!(log, Log::new()); } #[bench] fn bench_decoder(b: &mut Bencher) { + use rustc_serialize::json::Json; + b.bytes = JSON_STR.len() as u64; b.iter(|| { - let json = serialize::json::from_str(JSON_STR).unwrap(); - let mut decoder = serialize::json::Decoder::new(json); - let _log: Log = serialize::Decodable::decode(&mut decoder).unwrap(); + let json = Json::from_str(JSON_STR).unwrap(); + let mut decoder = rustc_serialize::json::Decoder::new(json); + let _log: Log = rustc_serialize::Decodable::decode(&mut decoder).unwrap(); }); } @@ -1350,7 +1355,7 @@ fn manual_reader_string(rdr: &mut R, buf: &mut [u8], key: &[u8]) -> S #[inline] fn manual_reader_deserialize(rdr: &mut R) -> Log { - let mut buf = [0, .. 128]; + let mut buf = [0; 128]; manual_reader_ignore(rdr, &mut buf, b"{"); let timestamp = manual_reader_int(rdr, &mut buf, b"timestamp"); @@ -1426,7 +1431,7 @@ fn manual_reader_deserialize(rdr: &mut R) -> Log { ////////////////////////////////////////////////////////////////////////////// #[inline] -fn manual_iter_ignore>(mut rdr: R, buf: &mut [u8], key: &[u8]) { +fn manual_iter_ignore>(mut rdr: R, buf: &mut [u8], key: &[u8]) { let buf = buf.slice_mut(0, key.len()); for idx in range(0, key.len()) { @@ -1436,7 +1441,7 @@ fn manual_iter_ignore>(mut rdr: R, buf: &mut [u8], key: &[u8]) { } #[inline] -fn manual_iter_field>(mut rdr: R, buf: &mut [u8], key: &[u8]) { +fn manual_iter_field>(mut rdr: R, buf: &mut [u8], key: &[u8]) { let b = rdr.next().unwrap(); assert_eq!(b, b'"'); @@ -1450,7 +1455,7 @@ fn manual_iter_field>(mut rdr: R, buf: &mut [u8], key: &[u8]) { } #[inline] -fn manual_iter_int>(mut rdr: R, buf: &mut [u8], key: &[u8]) -> i64 { +fn manual_iter_int>(mut rdr: R, buf: &mut [u8], key: &[u8]) -> i64 { manual_iter_field(rdr.by_ref(), buf, key); let mut res = 0; @@ -1470,7 +1475,7 @@ fn manual_iter_int>(mut rdr: R, buf: &mut [u8], key: &[u8]) -> i } #[inline] -fn manual_iter_string>(mut rdr: R, buf: &mut [u8], key: &[u8]) -> String { +fn manual_iter_string>(mut rdr: R, buf: &mut [u8], key: &[u8]) -> String { manual_iter_field(rdr.by_ref(), buf, key); manual_iter_ignore(rdr.by_ref(), buf, b"\""); @@ -1493,8 +1498,8 @@ fn manual_iter_string>(mut rdr: R, buf: &mut [u8], key: &[u8]) - } #[inline] -fn manual_iter_deserialize>(mut rdr: R) -> Log { - let mut buf = [0u8, .. 128]; +fn manual_iter_deserialize>(mut rdr: R) -> Log { + let mut buf = [0u8; 128]; manual_iter_ignore(rdr.by_ref(), &mut buf, b"{"); let timestamp = manual_iter_int(rdr.by_ref(), &mut buf, b"timestamp"); diff --git a/benches/bench_map.rs b/benches/bench_map.rs index ad6ddd09..90516206 100644 --- a/benches/bench_map.rs +++ b/benches/bench_map.rs @@ -1,4 +1,4 @@ -#![feature(phase)] +#![feature(associated_types, phase)] #[phase(plugin)] extern crate serde_macros; @@ -17,7 +17,7 @@ use serde::de::{Deserializer, Deserialize}; ////////////////////////////////////////////////////////////////////////////// -#[deriving(Show)] +#[derive(Show)] pub enum Error { EndOfStream, SyntaxError, @@ -224,7 +224,7 @@ mod deserializer { use serde::de; - #[deriving(PartialEq, Show)] + #[derive(PartialEq, Show)] enum State { StartState, KeyOrEndState, @@ -249,7 +249,9 @@ mod deserializer { } } - impl Iterator> for IntDeserializer { + impl Iterator for IntDeserializer { + type Item = Result; + #[inline] fn next(&mut self) -> Option> { match self.stack.pop() { diff --git a/benches/bench_struct.rs b/benches/bench_struct.rs index 650d11d5..8d5f3688 100644 --- a/benches/bench_struct.rs +++ b/benches/bench_struct.rs @@ -1,23 +1,23 @@ -#![feature(phase)] +#![feature(associated_types, phase, old_orphan_check)] #[phase(plugin)] extern crate serde_macros; extern crate serde; -extern crate serialize; +extern crate "rustc-serialize" as rustc_serialize; extern crate test; use std::collections::HashMap; use test::Bencher; -use serialize::{Decoder, Decodable}; +use rustc_serialize::{Decoder, Decodable}; use serde::de::{Deserializer, Deserialize}; ////////////////////////////////////////////////////////////////////////////// -#[deriving(Clone, PartialEq, Show, Decodable)] -#[deriving_deserialize] +#[derive(Clone, PartialEq, Show, RustcDecodable)] +#[derive_deserialize] struct Inner { a: (), b: uint, @@ -26,15 +26,15 @@ struct Inner { ////////////////////////////////////////////////////////////////////////////// -#[deriving(Clone, PartialEq, Show, Decodable)] -#[deriving_deserialize] +#[derive(Clone, PartialEq, Show, RustcDecodable)] +#[derive_deserialize] struct Outer { inner: Vec, } ////////////////////////////////////////////////////////////////////////////// -#[deriving(Show)] +#[derive(Show)] pub enum Error { EndOfStream, SyntaxError(String), @@ -46,7 +46,7 @@ pub enum Error { mod decoder { use std::collections::HashMap; - use serialize::Decoder; + use rustc_serialize::Decoder; use super::{Outer, Inner, Error}; @@ -63,7 +63,7 @@ mod decoder { OptionState, }; - #[deriving(Show)] + #[derive(Show)] enum State { OuterState(Outer), InnerState(Inner), @@ -337,7 +337,7 @@ mod deserializer { EndState, }; - #[deriving(Show)] + #[derive(Show)] enum State { OuterState(Outer), InnerState(Inner), @@ -366,7 +366,9 @@ mod deserializer { } } - impl Iterator> for OuterDeserializer { + impl Iterator for OuterDeserializer { + type Item = Result; + #[inline] fn next(&mut self) -> Option> { match self.stack.pop() { diff --git a/benches/bench_vec.rs b/benches/bench_vec.rs index a06f651c..48406bc5 100644 --- a/benches/bench_vec.rs +++ b/benches/bench_vec.rs @@ -1,22 +1,22 @@ -#![feature(phase)] +#![feature(associated_types, phase)] #[phase(plugin)] extern crate serde_macros; extern crate serde; -extern crate serialize; +extern crate "rustc-serialize" as rustc_serialize; extern crate test; use std::fmt::Show; use test::Bencher; -use serialize::{Decoder, Decodable}; +use rustc_serialize::{Decoder, Decodable}; use serde::de::{Deserializer, Deserialize}; ////////////////////////////////////////////////////////////////////////////// -#[deriving(Show)] +#[derive(Show)] pub enum Error { EndOfStream, SyntaxError, @@ -27,7 +27,7 @@ pub enum Error { mod decoder { use std::vec; - use serialize; + use rustc_serialize; use super::Error; use super::Error::{EndOfStream, SyntaxError, OtherError}; @@ -47,7 +47,7 @@ mod decoder { } } - impl serialize::Decoder for IntDecoder { + impl rustc_serialize::Decoder for IntDecoder { fn error(&mut self, msg: &str) -> Error { OtherError(msg.to_string()) } @@ -199,7 +199,7 @@ mod decoder { } } - impl serialize::Decoder for U8Decoder { + impl rustc_serialize::Decoder for U8Decoder { fn error(&mut self, msg: &str) -> Error { OtherError(msg.to_string()) } @@ -349,7 +349,7 @@ mod deserializer { use serde::de; - #[deriving(PartialEq, Show)] + #[derive(PartialEq, Show)] enum State { StartState, SepOrEndState, @@ -373,7 +373,9 @@ mod deserializer { } } - impl Iterator> for IntDeserializer { + impl Iterator for IntDeserializer { + type Item = Result; + #[inline] fn next(&mut self) -> Option> { match self.state { @@ -445,7 +447,9 @@ mod deserializer { } } - impl Iterator> for U8Deserializer { + impl Iterator for U8Deserializer { + type Item = Result; + #[inline] fn next(&mut self) -> Option> { match self.state { diff --git a/serde_macros/src/lib.rs b/serde_macros/src/lib.rs index 4aef1ab5..0e751c3e 100644 --- a/serde_macros/src/lib.rs +++ b/serde_macros/src/lib.rs @@ -58,15 +58,15 @@ use rustc::plugin::Registry; #[doc(hidden)] pub fn plugin_registrar(reg: &mut Registry) { reg.register_syntax_extension( - token::intern("deriving_serialize"), - Decorator(box expand_deriving_serialize)); + token::intern("derive_serialize"), + Decorator(box expand_derive_serialize)); reg.register_syntax_extension( - token::intern("deriving_deserialize"), - Decorator(box expand_deriving_deserialize)); + token::intern("derive_deserialize"), + Decorator(box expand_derive_deserialize)); } -fn expand_deriving_serialize(cx: &mut ExtCtxt, +fn expand_derive_serialize(cx: &mut ExtCtxt, sp: Span, mitem: &MetaItem, item: &Item, @@ -191,11 +191,11 @@ fn serialize_substructure(cx: &ExtCtxt, }) } - _ => cx.bug("expected Struct or EnumMatching in deriving_serialize") + _ => cx.bug("expected Struct or EnumMatching in derive_serialize") } } -pub fn expand_deriving_deserialize(cx: &mut ExtCtxt, +pub fn expand_derive_deserialize(cx: &mut ExtCtxt, span: Span, mitem: &MetaItem, item: &Item, @@ -274,7 +274,7 @@ fn deserialize_substructure(cx: &mut ExtCtxt, deserializer, token) } - _ => cx.bug("expected StaticEnum or StaticStruct in deriving(Deserialize)") + _ => cx.bug("expected StaticEnum or StaticStruct in derive(Deserialize)") } } @@ -486,8 +486,9 @@ fn deserialize_static_fields( } } -fn find_serial_name<'a, I: Iterator<&'a Attribute>>(mut iterator: I) - -> Option { +fn find_serial_name<'a, I>(mut iterator: I) -> Option where + I: Iterator +{ for at in iterator { match at.node.value.node { MetaNameValue(ref at_name, ref value) => { diff --git a/src/de.rs b/src/de.rs index d436b82d..5b5a0889 100644 --- a/src/de.rs +++ b/src/de.rs @@ -10,13 +10,14 @@ use std::collections::{HashMap, HashSet, BTreeMap, BTreeSet}; use std::hash::Hash; -use std::num; -use std::rc::Rc; +use std::iter::FromIterator; +use std::num::{self, FromPrimitive}; use std::option; +use std::rc::Rc; use std::string; use std::sync::Arc; -#[deriving(Clone, PartialEq, Show)] +#[derive(Clone, PartialEq, Show)] pub enum Token { Null, Bool(bool), @@ -77,7 +78,7 @@ impl Token { } } -#[deriving(Copy, Clone, PartialEq, Eq)] +#[derive(Copy, Clone, PartialEq, Eq)] pub enum TokenKind { NullKind, BoolKind, @@ -176,7 +177,7 @@ macro_rules! to_result { } } -pub trait Deserializer: Iterator> { +pub trait Deserializer: Iterator> + Sized { /// Called when a `Deserialize` expected more tokens, but the /// `Deserializer` was empty. fn end_of_stream_error(&mut self) -> E; @@ -196,11 +197,13 @@ pub trait Deserializer: Iterator> { T: Deserialize >(&mut self, field: &'static str) -> Result; + /* /// Called when a `Deserialize` has decided to not consume this token. fn ignore_field(&mut self, _token: Token) -> Result<(), E> { let _: IgnoreTokens = try!(Deserialize::deserialize(self)); Ok(()) } + */ #[inline] fn expect_token(&mut self) -> Result { @@ -584,7 +587,7 @@ pub trait Deserializer: Iterator> { ////////////////////////////////////////////////////////////////////////////// -struct SeqDeserializer<'a, D: 'a, E: 'a> { +struct SeqDeserializer<'a, D: 'a, E: 'a, T> { d: &'a mut D, len: uint, err: &'a mut Option, @@ -595,7 +598,9 @@ impl< D: Deserializer, E, T: Deserialize -> Iterator for SeqDeserializer<'a, D, E> { +> Iterator for SeqDeserializer<'a, D, E, T> { + type Item = T; + #[inline] fn next(&mut self) -> option::Option { match self.d.expect_seq_elt_or_end() { @@ -618,7 +623,7 @@ impl< ////////////////////////////////////////////////////////////////////////////// -struct MapDeserializer<'a, D:'a, E: 'a> { +struct MapDeserializer<'a, D:'a, E: 'a, K, V> { d: &'a mut D, len: uint, err: &'a mut option::Option, @@ -630,7 +635,9 @@ impl< E, K: Deserialize, V: Deserialize -> Iterator<(K, V)> for MapDeserializer<'a, D, E> { +> Iterator for MapDeserializer<'a, D, E, K, V> { + type Item = (K, V); + #[inline] fn next(&mut self) -> option::Option<(K, V)> { match self.d.expect_map_elt_or_end() { @@ -650,7 +657,7 @@ impl< ////////////////////////////////////////////////////////////////////////////// -pub trait Deserialize, E> { +pub trait Deserialize, E>: Sized { #[inline] fn deserialize(d: &mut D) -> Result { let token = try!(d.expect_token()); @@ -850,7 +857,7 @@ impl_deserialize_tuple! { T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, } /// Helper struct that will ignore tokens while taking in consideration /// recursive structures. -#[deriving(Copy)] +#[derive(Copy)] pub struct IgnoreTokens; impl, E> Deserialize for IgnoreTokens { @@ -1083,7 +1090,7 @@ mod tests { ////////////////////////////////////////////////////////////////////////////// - #[deriving(Clone, PartialEq, Show, Decodable)] + #[derive(Clone, PartialEq, Show, RustcDecodable)] struct Inner { a: (), b: uint, @@ -1125,7 +1132,7 @@ mod tests { ////////////////////////////////////////////////////////////////////////////// - #[deriving(Clone, PartialEq, Show, Decodable)] + #[derive(Clone, PartialEq, Show, RustcDecodable)] struct Outer { inner: Vec, } @@ -1158,7 +1165,7 @@ mod tests { ////////////////////////////////////////////////////////////////////////////// - #[deriving(Clone, PartialEq, Show, Decodable)] + #[derive(Clone, PartialEq, Show, RustcDecodable)] enum Animal { Dog, Frog(string::String, int) @@ -1185,7 +1192,7 @@ mod tests { ////////////////////////////////////////////////////////////////////////////// - #[deriving(Show)] + #[derive(Show)] enum Error { EndOfStream, SyntaxError(Vec), @@ -1200,7 +1207,7 @@ mod tests { tokens: Iter, } - impl> TokenDeserializer { + impl> TokenDeserializer { #[inline] fn new(tokens: Iter) -> TokenDeserializer { TokenDeserializer { @@ -1209,14 +1216,16 @@ mod tests { } } - impl> Iterator> for TokenDeserializer { + impl> Iterator for TokenDeserializer { + type Item = Result; + #[inline] fn next(&mut self) -> option::Option> { self.tokens.next().map(|token| Ok(token)) } } - impl> Deserializer for TokenDeserializer { + impl> Deserializer for TokenDeserializer { fn end_of_stream_error(&mut self) -> Error { Error::EndOfStream } diff --git a/src/json/de.rs b/src/json/de.rs index 33eac20a..b6487dfc 100644 --- a/src/json/de.rs +++ b/src/json/de.rs @@ -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 { buf: Vec, } -impl> Iterator> for Parser { +impl> Iterator for Parser { + type Item = Result; + #[inline] fn next(&mut self) -> Option> { let state = match self.state_stack.pop() { @@ -80,7 +82,7 @@ impl> Iterator> for Parser { } } -impl> Parser { +impl> Parser { /// Creates the JSON parser. #[inline] pub fn new(rdr: Iter) -> Parser { @@ -395,7 +397,7 @@ impl> Parser { } }; - 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> Parser { } } -impl> de::Deserializer for Parser { +impl> de::Deserializer for Parser { fn end_of_stream_error(&mut self) -> Error { Error::SyntaxError(ErrorCode::EOFWhileParsingValue, self.line, self.col) } @@ -635,7 +637,7 @@ impl> de::Deserializer for Parser { /// Decodes a json value from an `Iterator`. pub fn from_iter< - Iter: Iterator, + Iter: Iterator, T: de::Deserialize, Error> >(iter: Iter) -> Result { let mut parser = Parser::new(iter); diff --git a/src/json/error.rs b/src/json/error.rs index 5e0202f6..795c429c 100644 --- a/src/json/error.rs +++ b/src/json/error.rs @@ -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), diff --git a/src/json/mod.rs b/src/json/mod.rs index feca55f6..a282f3f2 100644 --- a/src/json/mod.rs +++ b/src/json/mod.rs @@ -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) @@ -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, } @@ -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, } @@ -1177,23 +1177,23 @@ mod tests { } /* - #[deriving(Decodable)] + #[derive(Decodable)] struct DecodeStruct { x: f64, y: bool, z: String, w: Vec } - #[deriving(Decodable)] + #[derive(Decodable)] enum DecodeEnum { A(f64), B(String) } - fn check_err>(to_parse: &'static str, + fn check_err>(to_parse: &'static str, expected: DecoderError) { let res: DecodeResult = 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); }); } diff --git a/src/json/ser.rs b/src/json/ser.rs index 2478c813..8eb59452 100644 --- a/src/json/ser.rs +++ b/src/json/ser.rs @@ -45,7 +45,7 @@ pub fn escape_str(wr: &mut W, v: &str) -> IoResult<()> { } fn escape_char(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(wr: &mut W, v: f64) -> IoResult<()> { fn spaces(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(wr: &mut W, mut n: uint) -> IoResult<()> { } /* -#[deriving(Show)] +#[derive(Show)] enum SerializerState { ValueState, TupleState, @@ -287,7 +287,7 @@ impl ser::Serializer for Serializer { #[inline] fn serialize_seq< T: Serialize, IoError>, - Iter: Iterator + Iter: Iterator >(&mut self, mut iter: Iter) -> IoResult<()> { try!(self.wr.write_str("[")); let mut first = true; @@ -307,7 +307,7 @@ impl ser::Serializer for Serializer { fn serialize_map< K: Serialize, IoError>, V: Serialize, IoError>, - Iter: Iterator<(K, V)> + Iter: Iterator >(&mut self, mut iter: Iter) -> IoResult<()> { try!(self.wr.write_str("{")); let mut first = true; @@ -541,7 +541,7 @@ impl ser::Serializer for PrettySerializer { #[inline] fn serialize_seq< T: Serialize, IoError>, - Iter: Iterator + Iter: Iterator >(&mut self, mut iter: Iter) -> IoResult<()> { try!(self.wr.write_str("[")); @@ -558,7 +558,7 @@ impl ser::Serializer for PrettySerializer { fn serialize_map< K: Serialize, IoError>, V: Serialize, IoError>, - Iter: Iterator<(K, V)> + Iter: Iterator >(&mut self, mut iter: Iter) -> IoResult<()> { try!(self.wr.write_str("{")); diff --git a/src/json/value.rs b/src/json/value.rs index 70ac4733..87187e99 100644 --- a/src/json/value.rs +++ b/src/json/value.rs @@ -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> for Deserializer { +impl Iterator for Deserializer { + type Item = Result; + #[inline] fn next(&mut self) -> Option> { loop { diff --git a/src/lib.rs b/src/lib.rs index c4df6b91..08a5bb13 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,8 @@ #![crate_type = "dylib"] #![crate_type = "rlib"] +#![feature(associated_types, old_orphan_check)] + // test harness access #[cfg(test)] extern crate test; @@ -12,6 +14,7 @@ extern crate serde_macros; #[cfg(test)] extern crate serialize; +extern crate "rustc-serialize" as rustc_serialize; extern crate unicode; pub use de::{Deserializer, Deserialize}; diff --git a/src/ser.rs b/src/ser.rs index 61fd7395..19b84f66 100644 --- a/src/ser.rs +++ b/src/ser.rs @@ -101,13 +101,13 @@ pub trait Serializer { fn serialize_seq< T: Serialize, - Iter: Iterator + Iter: Iterator >(&mut self, iter: Iter) -> Result<(), E>; fn serialize_map< K: Serialize, V: Serialize, - Iter: Iterator<(K, V)> + Iter: Iterator >(&mut self, iter: Iter) -> Result<(), E>; } @@ -327,8 +327,8 @@ mod tests { ////////////////////////////////////////////////////////////////////////////// - #[deriving(Clone, PartialEq, Show, Decodable)] - #[deriving_serialize] + #[derive(Clone, PartialEq, Show, RustcDecodable)] + #[derive_serialize] struct Inner { a: (), b: uint, @@ -337,16 +337,16 @@ mod tests { ////////////////////////////////////////////////////////////////////////////// - #[deriving(Clone, PartialEq, Show, Decodable)] - #[deriving_serialize] + #[derive(Clone, PartialEq, Show, RustcDecodable)] + #[derive_serialize] struct Outer { inner: Vec, } ////////////////////////////////////////////////////////////////////////////// - #[deriving(Clone, PartialEq, Show, Decodable)] - #[deriving_serialize] + #[derive(Clone, PartialEq, Show, RustcDecodable)] + #[derive_serialize] enum Animal { Dog, Frog(String, int) @@ -354,7 +354,7 @@ mod tests { ////////////////////////////////////////////////////////////////////////////// - #[deriving(Clone, PartialEq, Show)] + #[derive(Clone, PartialEq, Show)] pub enum Token<'a> { Null, Bool(bool), @@ -394,7 +394,7 @@ mod tests { MapEnd, } - #[deriving(Show)] + #[derive(Show)] #[allow(dead_code)] enum Error { EndOfStream, @@ -407,7 +407,7 @@ mod tests { iter: Iter, } - impl<'a, Iter: Iterator>> AssertSerializer { + impl<'a, Iter: Iterator>> AssertSerializer { fn new(iter: Iter) -> AssertSerializer { AssertSerializer { iter: iter, @@ -426,7 +426,7 @@ mod tests { } } - impl<'a, Iter: Iterator>> Serializer for AssertSerializer { + impl<'a, Iter: Iterator>> Serializer for AssertSerializer { fn serialize_null(&mut self) -> Result<(), Error> { self.serialize(Token::Null) } @@ -550,7 +550,7 @@ mod tests { fn serialize_seq< T: Serialize, Error>, - SeqIter: Iterator + SeqIter: Iterator >(&mut self, mut iter: SeqIter) -> Result<(), Error> { let (len, _) = iter.size_hint(); try!(self.serialize(Token::SeqStart(len))); @@ -563,7 +563,7 @@ mod tests { fn serialize_map< K: Serialize, Error>, V: Serialize, Error>, - MapIter: Iterator<(K, V)> + MapIter: Iterator >(&mut self, mut iter: MapIter) -> Result<(), Error> { let (len, _) = iter.size_hint(); try!(self.serialize(Token::MapStart(len))); diff --git a/tests/json_struct.rs b/tests/json_struct.rs index 90469953..d0ed6207 100644 --- a/tests/json_struct.rs +++ b/tests/json_struct.rs @@ -1,12 +1,12 @@ -#![feature(phase)] +#![feature(phase, old_orphan_check)] extern crate serde; #[phase(plugin)] extern crate serde_macros; -#[deriving(PartialEq, Show)] -#[deriving_serialize] -#[deriving_deserialize] +#[derive(PartialEq, Show)] +#[derive_serialize] +#[derive_deserialize] struct Test { #[serial_name = "$schema"] schema: String,