diff --git a/benches/bench_enum.rs b/benches/bench_enum.rs index 4170d299..855deceb 100644 --- a/benches/bench_enum.rs +++ b/benches/bench_enum.rs @@ -1,6 +1,6 @@ -#![feature(associated_types, phase, old_orphan_check)] +#![feature(plugin)] -#[phase(plugin)] +#[plugin] extern crate serde_macros; extern crate serde; @@ -63,7 +63,9 @@ mod decoder { } } - impl Decoder for AnimalDecoder { + impl Decoder for AnimalDecoder { + type Error = Error; + fn error(&mut self, msg: &str) -> Error { OtherError(msg.to_string()) } diff --git a/benches/bench_log.rs b/benches/bench_log.rs index 3bb1ac2a..65f49a3b 100644 --- a/benches/bench_log.rs +++ b/benches/bench_log.rs @@ -1,7 +1,7 @@ -#![feature(phase, macro_rules, old_orphan_check)] +#![feature(plugin)] #![allow(non_camel_case_types)] -#[phase(plugin)] +#[plugin] extern crate serde_macros; extern crate serde; @@ -44,14 +44,14 @@ enum HttpProtocol { HTTP11, } -impl, E> rustc_serialize::Encodable for HttpProtocol { - fn encode(&self, s: &mut S) -> Result<(), E> { +impl rustc_serialize::Encodable for HttpProtocol { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { (*self as uint).encode(s) } } -impl, E> rustc_serialize::Decodable for HttpProtocol { - fn decode(d: &mut D) -> Result { +impl rustc_serialize::Decodable for HttpProtocol { + fn decode(d: &mut D) -> Result { match FromPrimitive::from_uint(try!(d.read_uint())) { Some(value) => Ok(value), None => Err(d.error("cannot convert from uint")), @@ -88,14 +88,14 @@ enum HttpMethod { PATCH, } -impl, E> rustc_serialize::Encodable for HttpMethod { - fn encode(&self, s: &mut S) -> Result<(), E> { +impl rustc_serialize::Encodable for HttpMethod { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { (*self as uint).encode(s) } } -impl, E> rustc_serialize::Decodable for HttpMethod { - fn decode(d: &mut D) -> Result { +impl rustc_serialize::Decodable for HttpMethod { + fn decode(d: &mut D) -> Result { match FromPrimitive::from_uint(try!(d.read_uint())) { Some(value) => Ok(value), None => Err(d.error("cannot convert from uint")), @@ -125,14 +125,14 @@ enum CacheStatus { Hit, } -impl, E> rustc_serialize::Encodable for CacheStatus { - fn encode(&self, s: &mut S) -> Result<(), E> { +impl rustc_serialize::Encodable for CacheStatus { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { (*self as uint).encode(s) } } -impl, E> rustc_serialize::Decodable for CacheStatus { - fn decode(d: &mut D) -> Result { +impl rustc_serialize::Decodable for CacheStatus { + fn decode(d: &mut D) -> Result { match FromPrimitive::from_uint(try!(d.read_uint())) { Some(value) => Ok(value), None => Err(d.error("cannot convert from uint")), @@ -171,14 +171,14 @@ enum OriginProtocol { HTTPS, } -impl, E> rustc_serialize::Encodable for OriginProtocol { - fn encode(&self, s: &mut S) -> Result<(), E> { +impl rustc_serialize::Encodable for OriginProtocol { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { (*self as uint).encode(s) } } -impl, E> rustc_serialize::Decodable for OriginProtocol { - fn decode(d: &mut D) -> Result { +impl rustc_serialize::Decodable for OriginProtocol { + fn decode(d: &mut D) -> Result { match FromPrimitive::from_uint(try!(d.read_uint())) { Some(value) => Ok(value), None => Err(d.error("cannot convert from uint")), @@ -209,14 +209,14 @@ enum ZonePlan { ENT, } -impl, E> rustc_serialize::Encodable for ZonePlan { - fn encode(&self, s: &mut S) -> Result<(), E> { +impl rustc_serialize::Encodable for ZonePlan { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { (*self as uint).encode(s) } } -impl, E> rustc_serialize::Decodable for ZonePlan { - fn decode(d: &mut D) -> Result { +impl rustc_serialize::Decodable for ZonePlan { + fn decode(d: &mut D) -> Result { match FromPrimitive::from_uint(try!(d.read_uint())) { Some(value) => Ok(value), None => Err(d.error("cannot convert from uint")), @@ -498,14 +498,14 @@ enum Country { ZW, } -impl, E> rustc_serialize::Encodable for Country { - fn encode(&self, s: &mut S) -> Result<(), E> { +impl rustc_serialize::Encodable for Country { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { (*self as uint).encode(s) } } -impl, E> rustc_serialize::Decodable for Country { - fn decode(d: &mut D) -> Result { +impl rustc_serialize::Decodable for Country { + fn decode(d: &mut D) -> Result { match FromPrimitive::from_uint(try!(d.read_uint())) { Some(value) => Ok(value), None => Err(d.error("cannot convert from uint")), diff --git a/benches/bench_map.rs b/benches/bench_map.rs index 90516206..e44caeed 100644 --- a/benches/bench_map.rs +++ b/benches/bench_map.rs @@ -1,23 +1,23 @@ -#![feature(associated_types, phase)] +#![feature(plugin)] -#[phase(plugin)] +#[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 std::collections::HashMap; use test::Bencher; -use serialize::{Decoder, Decodable}; +use rustc_serialize::{Decoder, Decodable}; use serde::de::{Deserializer, Deserialize}; ////////////////////////////////////////////////////////////////////////////// -#[derive(Show)] +#[derive(PartialEq, Show)] pub enum Error { EndOfStream, SyntaxError, @@ -29,7 +29,7 @@ pub enum Error { mod decoder { use std::collections::HashMap; use std::collections::hash_map::IntoIter; - use serialize; + use rustc_serialize; use super::Error; use super::Error::{EndOfStream, SyntaxError, OtherError}; @@ -57,7 +57,9 @@ mod decoder { } } - impl serialize::Decoder for IntDecoder { + impl rustc_serialize::Decoder for IntDecoder { + type Error = Error; + fn error(&mut self, msg: &str) -> Error { OtherError(msg.to_string()) } @@ -318,13 +320,12 @@ mod deserializer { ////////////////////////////////////////////////////////////////////////////// fn run_decoder< - E: Show, - D: Decoder, - T: Clone + PartialEq + Show + Decodable + D: Decoder, + T: Clone + PartialEq + Show + Decodable >(mut d: D, value: T) { - let v: T = Decodable::decode(&mut d).unwrap(); + let v = Decodable::decode(&mut d); - assert_eq!(value, v); + assert_eq!(Ok(value), v); } #[bench] diff --git a/benches/bench_struct.rs b/benches/bench_struct.rs index 8d5f3688..44a73733 100644 --- a/benches/bench_struct.rs +++ b/benches/bench_struct.rs @@ -1,6 +1,6 @@ -#![feature(associated_types, phase, old_orphan_check)] +#![feature(plugin)] -#[phase(plugin)] +#[plugin] extern crate serde_macros; extern crate serde; @@ -91,7 +91,9 @@ mod decoder { } } - impl Decoder for OuterDecoder { + impl Decoder for OuterDecoder { + type Error = Error; + fn error(&mut self, msg: &str) -> Error { Error::OtherError(msg.to_string()) } diff --git a/benches/bench_vec.rs b/benches/bench_vec.rs index 48406bc5..37c55243 100644 --- a/benches/bench_vec.rs +++ b/benches/bench_vec.rs @@ -1,6 +1,6 @@ -#![feature(associated_types, phase)] +#![feature(plugin)] -#[phase(plugin)] +#[plugin] extern crate serde_macros; extern crate serde; @@ -16,7 +16,7 @@ use serde::de::{Deserializer, Deserialize}; ////////////////////////////////////////////////////////////////////////////// -#[derive(Show)] +#[derive(PartialEq, Show)] pub enum Error { EndOfStream, SyntaxError, @@ -47,7 +47,9 @@ mod decoder { } } - impl rustc_serialize::Decoder for IntDecoder { + impl rustc_serialize::Decoder for IntDecoder { + type Error = Error; + fn error(&mut self, msg: &str) -> Error { OtherError(msg.to_string()) } @@ -199,7 +201,9 @@ mod decoder { } } - impl rustc_serialize::Decoder for U8Decoder { + impl rustc_serialize::Decoder for U8Decoder { + type Error = Error; + fn error(&mut self, msg: &str) -> Error { OtherError(msg.to_string()) } @@ -508,13 +512,12 @@ mod deserializer { ////////////////////////////////////////////////////////////////////////////// fn run_decoder< - D: Decoder, - E: Show, - T: Clone + PartialEq + Show + Decodable + D: Decoder, + T: Clone + PartialEq + Show + Decodable >(mut d: D, value: T) { - let v: T = Decodable::decode(&mut d).unwrap(); + let v = Decodable::decode(&mut d); - assert_eq!(value, v); + assert_eq!(Ok(value), v); } fn run_deserializer< diff --git a/serde2/Cargo.lock b/serde2/Cargo.lock index edccb429..1037cd6d 100644 --- a/serde2/Cargo.lock +++ b/serde2/Cargo.lock @@ -2,9 +2,15 @@ name = "serde2" version = "0.1.0" dependencies = [ + "rustc-serialize 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", "serde2_macros 0.1.0", ] +[[package]] +name = "rustc-serialize" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "serde2_macros" version = "0.1.0" diff --git a/serde2/Cargo.toml b/serde2/Cargo.toml index 9001c6bd..4f75e485 100644 --- a/serde2/Cargo.toml +++ b/serde2/Cargo.toml @@ -10,5 +10,8 @@ name = "serde2" name = "serde2" path = "src/bin.rs" +[dependencies] +rustc-serialize = "*" + [dependencies.serde2_macros] path = "serde2_macros/" diff --git a/serde2/benches/bench_log.rs b/serde2/benches/bench_log.rs index c55f89c7..bbf55cc5 100644 --- a/serde2/benches/bench_log.rs +++ b/serde2/benches/bench_log.rs @@ -1,15 +1,16 @@ -#![feature(phase, macro_rules)] +#![feature(plugin)] #![allow(non_camel_case_types)] -#[phase(plugin)] +#[plugin] extern crate serde2_macros; extern crate serde2; -extern crate serialize; +extern crate "rustc-serialize" as rustc_serialize; extern crate test; use std::io; use std::io::ByRefWriter; +use std::num::FromPrimitive; use test::Bencher; use serde2::json::ser::escape_str; @@ -19,7 +20,7 @@ use serde2::ser; use serde2::de::{Deserialize, Deserializer}; use serde2::de; -use serialize::Encodable; +use rustc_serialize::Encodable; enum HttpField { Protocol, @@ -66,9 +67,9 @@ impl< } } -#[deriving(Show, PartialEq, Encodable, Decodable)] -#[deriving_serialize] -//#[deriving_deserialize] +#[derive(Show, PartialEq, RustcEncodable, RustcDecodable)] +#[derive_serialize] +//#[derive_deserialize] struct Http { protocol: HttpProtocol, status: u32, @@ -141,21 +142,21 @@ impl< } } -#[deriving(Copy, Show, PartialEq, FromPrimitive)] +#[derive(Copy, Show, PartialEq, FromPrimitive)] enum HttpProtocol { HTTP_PROTOCOL_UNKNOWN, HTTP10, HTTP11, } -impl, E> serialize::Encodable for HttpProtocol { - fn encode(&self, s: &mut S) -> Result<(), E> { +impl rustc_serialize::Encodable for HttpProtocol { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { (*self as uint).encode(s) } } -impl, E> serialize::Decodable for HttpProtocol { - fn decode(d: &mut D) -> Result { +impl rustc_serialize::Decodable for HttpProtocol { + fn decode(d: &mut D) -> Result { match FromPrimitive::from_uint(try!(d.read_uint())) { Some(value) => Ok(value), None => Err(d.error("cannot convert from uint")), @@ -185,7 +186,7 @@ impl< } } -#[deriving(Copy, Show, PartialEq, FromPrimitive)] +#[derive(Copy, Show, PartialEq, FromPrimitive)] enum HttpMethod { METHOD_UNKNOWN, GET, @@ -200,14 +201,14 @@ enum HttpMethod { PATCH, } -impl, E> serialize::Encodable for HttpMethod { - fn encode(&self, s: &mut S) -> Result<(), E> { +impl rustc_serialize::Encodable for HttpMethod { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { (*self as uint).encode(s) } } -impl, E> serialize::Decodable for HttpMethod { - fn decode(d: &mut D) -> Result { +impl rustc_serialize::Decodable for HttpMethod { + fn decode(d: &mut D) -> Result { match FromPrimitive::from_uint(try!(d.read_uint())) { Some(value) => Ok(value), None => Err(d.error("cannot convert from uint")), @@ -237,7 +238,7 @@ impl< } } -#[deriving(Copy, Show, PartialEq, FromPrimitive)] +#[derive(Copy, Show, PartialEq, FromPrimitive)] enum CacheStatus { CACHESTATUS_UNKNOWN, Miss, @@ -245,14 +246,14 @@ enum CacheStatus { Hit, } -impl, E> serialize::Encodable for CacheStatus { - fn encode(&self, s: &mut S) -> Result<(), E> { +impl rustc_serialize::Encodable for CacheStatus { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { (*self as uint).encode(s) } } -impl, E> serialize::Decodable for CacheStatus { - fn decode(d: &mut D) -> Result { +impl rustc_serialize::Decodable for CacheStatus { + fn decode(d: &mut D) -> Result { match FromPrimitive::from_uint(try!(d.read_uint())) { Some(value) => Ok(value), None => Err(d.error("cannot convert from uint")), @@ -314,9 +315,9 @@ impl< } } -#[deriving(Show, PartialEq, Encodable, Decodable)] -#[deriving_serialize] -//#[deriving_deserialize] +#[derive(Show, PartialEq, RustcEncodable, RustcDecodable)] +#[derive_serialize] +//#[derive_deserialize] struct Origin { ip: String, port: u32, @@ -370,21 +371,21 @@ impl< } } -#[deriving(Copy, Show, PartialEq, FromPrimitive)] +#[derive(Copy, Show, PartialEq, FromPrimitive)] enum OriginProtocol { ORIGIN_PROTOCOL_UNKNOWN, HTTP, HTTPS, } -impl, E> serialize::Encodable for OriginProtocol { - fn encode(&self, s: &mut S) -> Result<(), E> { +impl rustc_serialize::Encodable for OriginProtocol { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { (*self as uint).encode(s) } } -impl, E> serialize::Decodable for OriginProtocol { - fn decode(d: &mut D) -> Result { +impl rustc_serialize::Decodable for OriginProtocol { + fn decode(d: &mut D) -> Result { match FromPrimitive::from_uint(try!(d.read_uint())) { Some(value) => Ok(value), None => Err(d.error("cannot convert from uint")), @@ -411,7 +412,7 @@ impl, E: de::Error> de::Deserialize for OriginProto } } -#[deriving(Copy, Show, PartialEq, FromPrimitive)] +#[derive(Copy, Show, PartialEq, FromPrimitive)] enum ZonePlan { ZONEPLAN_UNKNOWN, FREE, @@ -420,14 +421,14 @@ enum ZonePlan { ENT, } -impl, E> serialize::Encodable for ZonePlan { - fn encode(&self, s: &mut S) -> Result<(), E> { +impl rustc_serialize::Encodable for ZonePlan { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { (*self as uint).encode(s) } } -impl, E> serialize::Decodable for ZonePlan { - fn decode(d: &mut D) -> Result { +impl rustc_serialize::Decodable for ZonePlan { + fn decode(d: &mut D) -> Result { match FromPrimitive::from_uint(try!(d.read_uint())) { Some(value) => Ok(value), None => Err(d.error("cannot convert from uint")), @@ -454,7 +455,7 @@ impl, E: de::Error> de::Deserialize for ZonePlan { } } -#[deriving(Copy, Show, PartialEq, FromPrimitive)] +#[derive(Copy, Show, PartialEq, FromPrimitive)] enum Country { UNKNOWN, A1, @@ -714,14 +715,14 @@ enum Country { ZW, } -impl, E> serialize::Encodable for Country { - fn encode(&self, s: &mut S) -> Result<(), E> { +impl rustc_serialize::Encodable for Country { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { (*self as uint).encode(s) } } -impl, E> serialize::Decodable for Country { - fn decode(d: &mut D) -> Result { +impl rustc_serialize::Decodable for Country { + fn decode(d: &mut D) -> Result { match FromPrimitive::from_uint(try!(d.read_uint())) { Some(value) => Ok(value), None => Err(d.error("cannot convert from uint")), @@ -799,9 +800,9 @@ impl< } } -#[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, @@ -1012,14 +1013,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(); } @@ -1033,7 +1034,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(); } @@ -1042,7 +1043,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(); }); } @@ -1099,7 +1100,7 @@ fn bench_serializer_slice(b: &mut Bencher) { let json = json::to_vec(&log).unwrap(); 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; } diff --git a/serde2/serde2_macros/src/lib.rs b/serde2/serde2_macros/src/lib.rs index 12f00078..b6710e58 100644 --- a/serde2/serde2_macros/src/lib.rs +++ b/serde2/serde2_macros/src/lib.rs @@ -48,17 +48,17 @@ 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"), - ItemDecorator(box expand_deriving_deserialize)); + token::intern("derive_deserialize"), + ItemDecorator(box expand_derive_deserialize)); */ } -fn expand_deriving_serialize<>(cx: &mut ExtCtxt, +fn expand_derive_serialize<>(cx: &mut ExtCtxt, sp: Span, mitem: &MetaItem, item: &Item, @@ -121,7 +121,7 @@ fn expand_deriving_serialize<>(cx: &mut ExtCtxt, ) ), attributes: attrs, - combine_substructure: combine_substructure(|a, b, c| { + combine_substructure: combine_substructure(box |a, b, c| { serialize_substructure(a, b, c) }), } @@ -148,7 +148,7 @@ fn serialize_substructure(cx: &ExtCtxt, span: Span, substr: &Substructure) -> P< serialize_enum(cx, span, state, visitor, substr.type_ident, variant, fields) } - _ => cx.bug("expected Struct or EnumMatching in deriving_serialize") + _ => cx.bug("expected Struct or EnumMatching in derive_serialize") } } @@ -261,7 +261,7 @@ fn serialize_enum(cx: &ExtCtxt, } /* -pub fn expand_deriving_deserialize(cx: &mut ExtCtxt, +pub fn expand_derive_deserialize(cx: &mut ExtCtxt, span: Span, mitem: &MetaItem, item: &Item, @@ -304,7 +304,7 @@ pub fn expand_deriving_deserialize(cx: &mut ExtCtxt, ) ), attributes: Vec::new(), - combine_substructure: combine_substructure(|a, b, c| { + combine_substructure: combine_substructure(box |a, b, c| { deserialize_substructure(a, b, c) }), }) @@ -337,7 +337,7 @@ fn deserialize_substructure(cx: &mut ExtCtxt, span: Span, deserializer, token) } - _ => cx.bug("expected StaticEnum or StaticStruct in deriving(Deserialize)") + _ => cx.bug("expected StaticEnum or StaticStruct in derive(Deserialize)") } } diff --git a/serde2/src/bin.rs b/serde2/src/bin.rs index c844bcb6..dbdd3143 100644 --- a/serde2/src/bin.rs +++ b/serde2/src/bin.rs @@ -7,7 +7,7 @@ use std::string; use serde2::de; use serde2::de::{Deserialize, Deserializer}; -#[deriving(Show)] +#[derive(Show)] pub enum Token { Null, Int(int), @@ -18,7 +18,7 @@ pub enum Token { End, } -#[deriving(Show)] +#[derive(Show)] enum Error { SyntaxError, EndOfStreamError, @@ -41,7 +41,7 @@ struct MyDeserializer { peeked: option::Option, } -impl> MyDeserializer { +impl> MyDeserializer { pub fn new(tokens: Iter) -> MyDeserializer { MyDeserializer { tokens: tokens, @@ -68,7 +68,7 @@ impl> MyDeserializer { } } -impl> Deserializer for MyDeserializer { +impl> Deserializer for MyDeserializer { fn visit< R, V: de::Visitor, R, Error>, @@ -139,7 +139,7 @@ struct MyOptionVisitor<'a, Iter: 'a> { impl< 'a, - Iter: Iterator, + Iter: Iterator, > de::OptionVisitor, Error> for MyOptionVisitor<'a, Iter> { fn visit< T: Deserialize, Error>, @@ -161,7 +161,7 @@ struct MySeqVisitor<'a, Iter: 'a> { impl< 'a, - Iter: Iterator, + Iter: Iterator, > de::SeqVisitor, Error> for MySeqVisitor<'a, Iter> { fn visit< T: Deserialize, Error> @@ -206,7 +206,7 @@ struct MyMapVisitor<'a, Iter: 'a> { impl< 'a, - Iter: Iterator, + Iter: Iterator, > de::MapVisitor, Error> for MyMapVisitor<'a, Iter> { fn visit_key< K: Deserialize, Error>, @@ -256,7 +256,7 @@ mod json { use std::collections::BTreeMap; use serde2::de; - #[deriving(Show)] + #[derive(Show)] pub enum Value { Null, //Bool(bool), diff --git a/serde2/src/de.rs b/serde2/src/de.rs index 17ce0904..4a8d0933 100644 --- a/serde2/src/de.rs +++ b/serde2/src/de.rs @@ -1,5 +1,6 @@ use std::collections::{HashMap, BTreeMap}; use std::hash::Hash; +use std::num::FromPrimitive; /////////////////////////////////////////////////////////////////////////////// diff --git a/serde2/src/json/builder.rs b/serde2/src/json/builder.rs index a495eeaa..f307607d 100644 --- a/serde2/src/json/builder.rs +++ b/serde2/src/json/builder.rs @@ -10,8 +10,8 @@ use std::collections::BTreeMap; -use ser::{mod, Serialize}; -use json::value::{mod, Value}; +use ser::{self, Serialize}; +use json::value::{self, Value}; pub struct ArrayBuilder { array: Vec, @@ -31,13 +31,17 @@ impl ArrayBuilder { self } - pub fn push_array(mut self, f: |ArrayBuilder| -> ArrayBuilder) -> ArrayBuilder { + pub fn push_array(mut self, f: F) -> ArrayBuilder where + F: FnOnce(ArrayBuilder) -> ArrayBuilder + { let builder = ArrayBuilder::new(); self.array.push(f(builder).unwrap()); self } - pub fn push_object(mut self, f: |ObjectBuilder| -> ObjectBuilder) -> ArrayBuilder { + pub fn push_object(mut self, f: F) -> ArrayBuilder where + F: FnOnce(ObjectBuilder) -> ObjectBuilder + { let builder = ObjectBuilder::new(); self.array.push(f(builder).unwrap()); self @@ -62,13 +66,17 @@ impl ObjectBuilder { self } - pub fn insert_array(mut self, key: String, f: |ArrayBuilder| -> ArrayBuilder) -> ObjectBuilder { + pub fn insert_array(mut self, key: String, f: F) -> ObjectBuilder where + F: FnOnce(ArrayBuilder) -> ArrayBuilder + { let builder = ArrayBuilder::new(); self.object.insert(key, f(builder).unwrap()); self } - pub fn insert_object(mut self, key: String, f: |ObjectBuilder| -> ObjectBuilder) -> ObjectBuilder { + pub fn insert_object(mut self, key: String, f: F) -> ObjectBuilder where + F: FnOnce(ObjectBuilder) -> ObjectBuilder + { let builder = ObjectBuilder::new(); self.object.insert(key, f(builder).unwrap()); self diff --git a/serde2/src/json/de.rs b/serde2/src/json/de.rs index 8e38eeb9..b4abb3e5 100644 --- a/serde2/src/json/de.rs +++ b/serde2/src/json/de.rs @@ -15,7 +15,7 @@ pub struct Parser { buf: Vec, } -impl> Parser { +impl> Parser { /// Creates the JSON parser. #[inline] pub fn new(rdr: Iter) -> Parser { @@ -353,7 +353,7 @@ impl> Parser { } }; - let buf = &mut [0u8, .. 4]; + let buf = &mut [0; 4]; let len = c.encode_utf8(buf).unwrap_or(0); self.buf.extend(buf.slice_to(len).iter().map(|b| *b)); } @@ -380,7 +380,7 @@ impl> Parser { } } -impl> Deserializer for Parser { +impl> Deserializer for Parser { #[inline] fn visit< R, @@ -395,7 +395,7 @@ struct SeqVisitor<'a, Iter: 'a> { first: bool, } -impl<'a, Iter: Iterator> de::SeqVisitor, Error> for SeqVisitor<'a, Iter> { +impl<'a, Iter: Iterator> de::SeqVisitor, Error> for SeqVisitor<'a, Iter> { fn visit< T: de::Deserialize, Error>, >(&mut self) -> Result, Error> { @@ -439,7 +439,7 @@ struct MapVisitor<'a, Iter: 'a> { first: bool, } -impl<'a, Iter: Iterator> de::MapVisitor, Error> for MapVisitor<'a, Iter> { +impl<'a, Iter: Iterator> de::MapVisitor, Error> for MapVisitor<'a, Iter> { fn visit_key< K: de::Deserialize, Error>, >(&mut self) -> Result, Error> { @@ -506,7 +506,7 @@ impl<'a, Iter: Iterator> de::MapVisitor, Error> for MapVisitor< /// 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/serde2/src/json/error.rs b/serde2/src/json/error.rs index d73ea566..ebe82f1b 100644 --- a/serde2/src/json/error.rs +++ b/serde2/src/json/error.rs @@ -5,7 +5,7 @@ use std::io; use de; /// The errors that can arise while parsing a JSON stream. -#[deriving(Copy, Clone, PartialEq)] +#[derive(Copy, Clone, PartialEq)] pub enum ErrorCode { EOFWhileParsingList, EOFWhileParsingObject, @@ -75,7 +75,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/serde2/src/json/ser.rs b/serde2/src/json/ser.rs index 52895fe3..d3373c16 100644 --- a/serde2/src/json/ser.rs +++ b/serde2/src/json/ser.rs @@ -1,5 +1,5 @@ use std::f64; -use std::io::{mod, ByRefWriter, IoError}; +use std::io::{self, ByRefWriter, IoError}; use std::num::{Float, FpCategory}; use std::string::FromUtf8Error; @@ -218,7 +218,7 @@ pub fn escape_str(wr: &mut W, value: &str) -> Result<(), IoError> #[inline] pub fn escape_char(wr: &mut W, value: char) -> Result<(), IoError> { - let mut buf = &mut [0, .. 4]; + let mut buf = &mut [0; 4]; value.encode_utf8(buf); escape_bytes(wr, buf) } diff --git a/serde2/src/json/value.rs b/serde2/src/json/value.rs index ea700dff..57574016 100644 --- a/serde2/src/json/value.rs +++ b/serde2/src/json/value.rs @@ -1,10 +1,11 @@ use std::collections::BTreeMap; use std::fmt; use std::io; +use std::str; -use ser::{mod, Serializer}; +use ser::{self, Serializer}; -#[deriving(PartialEq)] +#[derive(PartialEq)] pub enum Value { Null, Bool(bool), @@ -49,19 +50,20 @@ impl ser::Serialize for 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> io::Writer for WriterFormatter<'a, 'b> { fn write(&mut self, buf: &[u8]) -> io::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 { - let mut wr = WriterFormatter(f); + let mut wr = WriterFormatter { inner: f }; super::ser::to_writer(&mut wr, self).map_err(|_| fmt::Error) } } diff --git a/serde2/src/lib.rs b/serde2/src/lib.rs index b8a5da00..2d5354b9 100644 --- a/serde2/src/lib.rs +++ b/serde2/src/lib.rs @@ -1,5 +1,3 @@ -#![feature(macro_rules)] - extern crate unicode; pub use ser::{Serialize, Serializer}; diff --git a/serde2/src/ser.rs b/serde2/src/ser.rs index 6a97a1f9..c322b30e 100644 --- a/serde2/src/ser.rs +++ b/serde2/src/ser.rs @@ -224,7 +224,7 @@ pub struct SeqIteratorVisitor { first: bool, } -impl> SeqIteratorVisitor { +impl> SeqIteratorVisitor { #[inline] pub fn new(iter: Iter) -> SeqIteratorVisitor { SeqIteratorVisitor { @@ -236,7 +236,7 @@ impl> SeqIteratorVisitor { impl< T: Serialize, - Iter: Iterator, + Iter: Iterator, S, R, E, @@ -281,10 +281,15 @@ impl< /////////////////////////////////////////////////////////////////////////////// +// FIXME(rust #19630) Remove this work-around +macro_rules! e { + ($e:expr) => { $e } +} + macro_rules! tuple_impls { ($( ($($T:ident),+) { - $($state:pat => $method:ident,)+ + $($state:pat => $idx:tt,)+ } )+) => { $( @@ -317,8 +322,10 @@ macro_rules! tuple_impls { $( $state => { self.state += 1; - let value = self.tuple.$method(); - let value = try!(visitor.visit_seq_elt(state, true, value)); + let value = try!(visitor.visit_seq_elt( + state, + true, + &e!(self.tuple.$idx))); Ok(Some(value)) } )+ @@ -341,101 +348,101 @@ macro_rules! tuple_impls { tuple_impls! { (T0) { - 0 => ref0, + 0 => 0, } (T0, T1) { - 0 => ref0, - 1 => ref1, + 0 => 0, + 1 => 1, } (T0, T1, T2, T3) { - 0 => ref0, - 1 => ref1, - 2 => ref2, - 3 => ref3, + 0 => 0, + 1 => 1, + 2 => 2, + 3 => 3, } (T0, T1, T2, T3, T4) { - 0 => ref0, - 1 => ref1, - 2 => ref2, - 3 => ref3, - 4 => ref4, + 0 => 0, + 1 => 1, + 2 => 2, + 3 => 3, + 4 => 4, } (T0, T1, T2, T3, T4, T5) { - 0 => ref0, - 1 => ref1, - 2 => ref2, - 3 => ref3, - 4 => ref4, - 5 => ref5, + 0 => 0, + 1 => 1, + 2 => 2, + 3 => 3, + 4 => 4, + 5 => 5, } (T0, T1, T2, T3, T4, T5, T6) { - 0 => ref0, - 1 => ref1, - 2 => ref2, - 3 => ref3, - 4 => ref4, - 5 => ref5, - 6 => ref6, + 0 => 0, + 1 => 1, + 2 => 2, + 3 => 3, + 4 => 4, + 5 => 5, + 6 => 6, } (T0, T1, T2, T3, T4, T5, T6, T7) { - 0 => ref0, - 1 => ref1, - 2 => ref2, - 3 => ref3, - 4 => ref4, - 5 => ref5, - 6 => ref6, - 7 => ref7, + 0 => 0, + 1 => 1, + 2 => 2, + 3 => 3, + 4 => 4, + 5 => 5, + 6 => 6, + 7 => 7, } (T0, T1, T2, T3, T4, T5, T6, T7, T8) { - 0 => ref0, - 1 => ref1, - 2 => ref2, - 3 => ref3, - 4 => ref4, - 5 => ref5, - 6 => ref6, - 7 => ref7, - 8 => ref8, + 0 => 0, + 1 => 1, + 2 => 2, + 3 => 3, + 4 => 4, + 5 => 5, + 6 => 6, + 7 => 7, + 8 => 8, } (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9) { - 0 => ref0, - 1 => ref1, - 2 => ref2, - 3 => ref3, - 4 => ref4, - 5 => ref5, - 6 => ref6, - 7 => ref7, - 8 => ref8, - 9 => ref9, + 0 => 0, + 1 => 1, + 2 => 2, + 3 => 3, + 4 => 4, + 5 => 5, + 6 => 6, + 7 => 7, + 8 => 8, + 9 => 9, } (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10) { - 0 => ref0, - 1 => ref1, - 2 => ref2, - 3 => ref3, - 4 => ref4, - 5 => ref5, - 6 => ref6, - 7 => ref7, - 8 => ref8, - 9 => ref9, - 10 => ref10, + 0 => 0, + 1 => 1, + 2 => 2, + 3 => 3, + 4 => 4, + 5 => 5, + 6 => 6, + 7 => 7, + 8 => 8, + 9 => 9, + 10 => 10, } (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11) { - 0 => ref0, - 1 => ref1, - 2 => ref2, - 3 => ref3, - 4 => ref4, - 5 => ref5, - 6 => ref6, - 7 => ref7, - 8 => ref8, - 9 => ref9, - 10 => ref10, - 11 => ref11, + 0 => 0, + 1 => 1, + 2 => 2, + 3 => 3, + 4 => 4, + 5 => 5, + 6 => 6, + 7 => 7, + 8 => 8, + 9 => 9, + 10 => 10, + 11 => 11, } } @@ -446,7 +453,7 @@ pub struct MapIteratorVisitor { first: bool, } -impl> MapIteratorVisitor { +impl> MapIteratorVisitor { #[inline] pub fn new(iter: Iter) -> MapIteratorVisitor { MapIteratorVisitor { @@ -459,7 +466,7 @@ impl> MapIteratorVisitor { impl< K: Serialize, V: Serialize, - Iter: Iterator<(K, V)>, + Iter: Iterator, S, R, E, diff --git a/serde_macros/src/lib.rs b/serde_macros/src/lib.rs index 0e751c3e..09ce36fd 100644 --- a/serde_macros/src/lib.rs +++ b/serde_macros/src/lib.rs @@ -107,7 +107,7 @@ fn expand_derive_serialize(cx: &mut ExtCtxt, ) ), attributes: attrs, - combine_substructure: combine_substructure(|a, b, c| { + combine_substructure: combine_substructure(box |a, b, c| { serialize_substructure(a, b, c, item) }), }) @@ -238,7 +238,7 @@ pub fn expand_derive_deserialize(cx: &mut ExtCtxt, ) ), attributes: Vec::new(), - combine_substructure: combine_substructure(|a, b, c| { + combine_substructure: combine_substructure(box |a, b, c| { deserialize_substructure(a, b, c) }), }) @@ -415,7 +415,7 @@ fn deserialize_enum( path, serial_names.as_slice(), parts, - |cx, _, _| { + |&: cx, _, _| { quote_expr!(cx, try!($deserializer.expect_enum_elt())) } ); @@ -441,14 +441,14 @@ fn deserialize_enum( /// Create a deserializer for a single enum variant/struct: /// - `outer_pat_ident` is the name of this enum variant/struct /// - `getarg` should retrieve the `uint`-th field with name `&str`. -fn deserialize_static_fields( +fn deserialize_static_fields( cx: &ExtCtxt, span: Span, outer_pat_path: ast::Path, serial_names: &[Option], fields: &StaticFields, - getarg: |&ExtCtxt, Span, token::InternedString| -> P -) -> P { + getarg: F +) -> P where F: Fn(&ExtCtxt, Span, token::InternedString) -> P { match *fields { Unnamed(ref fields) => { let path_expr = cx.expr_path(outer_pat_path); diff --git a/src/json/builder.rs b/src/json/builder.rs index 3bba0ab8..6986fd3d 100644 --- a/src/json/builder.rs +++ b/src/json/builder.rs @@ -31,12 +31,16 @@ impl ArrayBuilder { builder } - pub fn push_array(self, f: |ArrayBuilder| -> ArrayBuilder) -> ArrayBuilder { + pub fn push_array(self, f: F) -> ArrayBuilder where + F: FnOnce(ArrayBuilder) -> ArrayBuilder + { let builder = ArrayBuilder::new(); self.push(f(builder).unwrap()) } - pub fn push_object(self, f: |ObjectBuilder| -> ObjectBuilder) -> ArrayBuilder { + pub fn push_object(self, f: F) -> ArrayBuilder where + F: FnOnce(ObjectBuilder) -> ObjectBuilder + { let builder = ObjectBuilder::new(); self.push(f(builder).unwrap()) } @@ -61,12 +65,16 @@ impl ObjectBuilder { builder } - pub fn insert_array(self, key: String, f: |ArrayBuilder| -> ArrayBuilder) -> ObjectBuilder { + pub fn insert_array(self, key: String, f: F) -> ObjectBuilder where + F: FnOnce(ArrayBuilder) -> ArrayBuilder + { let builder = ArrayBuilder::new(); self.insert(key, f(builder).unwrap()) } - pub fn insert_object(self, key: String, f: |ObjectBuilder| -> ObjectBuilder) -> ObjectBuilder { + pub fn insert_object(self, key: String, f: F) -> ObjectBuilder where + F: FnOnce(ObjectBuilder) -> ObjectBuilder + { let builder = ObjectBuilder::new(); self.insert(key, f(builder).unwrap()) } diff --git a/src/json/mod.rs b/src/json/mod.rs index a282f3f2..9c0471e2 100644 --- a/src/json/mod.rs +++ b/src/json/mod.rs @@ -60,8 +60,8 @@ the code for these traits: `#[derive_serialize]` and `#[derive_deserialize]`. To serialize using `Serialize`: ```rust -#![feature(phase, old_orphan_check)] -#[phase(plugin)] +#![feature(plugin)] +#[plugin] extern crate serde_macros; extern crate serde; @@ -110,8 +110,8 @@ A basic `ToJson` example using a BTreeMap of attribute name / attribute value: ```rust -#![feature(phase, old_orphan_check)] -#[phase(plugin)] +#![feature(plugin)] +#[plugin] extern crate serde_macros; extern crate serde; @@ -142,8 +142,8 @@ fn main() { Or you can use the helper type `ObjectBuilder`: ```rust -#![feature(phase, old_orphan_check)] -#[phase(plugin)] +#![feature(plugin)] +#[plugin] extern crate serde_macros; extern crate serde; @@ -173,8 +173,8 @@ fn main() { To deserialize a JSON string using `Deserialize` trait: ```rust -#![feature(phase, old_orphan_check)] -#[phase(plugin)] +#![feature(plugin)] +#[plugin] extern crate serde_macros; extern crate serde; @@ -205,8 +205,8 @@ 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, old_orphan_check)] -#[phase(plugin)] +#![feature(plugin)] +#[plugin] extern crate serde_macros; extern crate serde; @@ -245,8 +245,8 @@ This example use the ToJson impl to deserialize the JSON string. Example of `ToJson` trait implementation for TestStruct1. ```rust -#![feature(phase, old_orphan_check)] -#[phase(plugin)] +#![feature(plugin)] +#[plugin] extern crate serde_macros; extern crate serde; @@ -1792,7 +1792,7 @@ mod bench { let json = encoder_json(count); b.iter(|| { - assert_eq!(json.pretty().to_string(), src); + assert_eq!(json.to_string(), src); }); } diff --git a/src/lib.rs b/src/lib.rs index 08a5bb13..d58a8ac1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,14 +1,12 @@ -#![feature(macro_rules, phase)] +#![feature(plugin)] #![crate_type = "dylib"] #![crate_type = "rlib"] -#![feature(associated_types, old_orphan_check)] - // test harness access #[cfg(test)] extern crate test; -#[phase(plugin)] +#[plugin] extern crate serde_macros; #[cfg(test)] diff --git a/tests/json_struct.rs b/tests/json_struct.rs index d0ed6207..793afd06 100644 --- a/tests/json_struct.rs +++ b/tests/json_struct.rs @@ -1,7 +1,7 @@ -#![feature(phase, old_orphan_check)] +#![feature(plugin)] extern crate serde; -#[phase(plugin)] +#[plugin] extern crate serde_macros; #[derive(PartialEq, Show)]