Fix serde2 macro generation

This commit is contained in:
Erick Tryzelaar 2014-10-14 18:08:34 -07:00
parent f614fb6667
commit 0b8324ae09
4 changed files with 299 additions and 232 deletions

View File

@ -14,12 +14,11 @@ use test::Bencher;
//use serde2::de;
use serde2::json;
use serde2::Serialize;
use serde2::ser;
#[deriving(Encodable, Decodable)]
#[deriving_serializable]
//#[deriving_deserializable]
#[deriving_serialize]
//#[deriving_deserialize]
struct Http {
protocol: HttpProtocol,
status: u32,
@ -39,15 +38,20 @@ enum HttpProtocol {
HTTP11,
}
impl<S: ser::VisitorState<R>, R> ser::Serialize<S, R> for HttpProtocol {
impl ser::Serialize for HttpProtocol {
#[inline]
fn serialize(&self, s: &mut S) -> R {
s.visit_uint(*self as uint)
fn visit<
S,
R,
E,
V: ser::Visitor<S, R, E>,
>(&self, state: &mut S, visitor: V) -> Result<R, E> {
visitor.visit_uint(state, *self as uint)
}
}
/*
impl<D: de::Deserializer<E>, E> de::Deserializable<D, E> for HttpProtocol {
impl<D: de::Deserializer<E>, E> de::Deserialize<D, E> for HttpProtocol {
#[inline]
fn deserialize_token(d: &mut D, token: de::Token) -> Result<HttpProtocol, E> {
d.expect_from_primitive(token)
@ -70,15 +74,20 @@ enum HttpMethod {
PATCH,
}
impl<S: ser::VisitorState<R>, R> ser::Serialize<S, R> for HttpMethod {
impl ser::Serialize for HttpMethod {
#[inline]
fn serialize(&self, s: &mut S) -> R {
s.visit_uint(*self as uint)
fn visit<
S,
R,
E,
V: ser::Visitor<S, R, E>,
>(&self, state: &mut S, visitor: V) -> Result<R, E> {
visitor.visit_uint(state, *self as uint)
}
}
/*
impl<D: de::Deserializer<E>, E> de::Deserializable<D, E> for HttpMethod {
impl<D: de::Deserializer<E>, E> de::Deserialize<D, E> for HttpMethod {
#[inline]
fn deserialize_token(d: &mut D, token: de::Token) -> Result<HttpMethod, E> {
d.expect_from_primitive(token)
@ -94,15 +103,20 @@ enum CacheStatus {
Hit,
}
impl<S: ser::VisitorState<R>, R> ser::Serialize<S, R> for CacheStatus {
impl ser::Serialize for CacheStatus {
#[inline]
fn serialize(&self, s: &mut S) -> R {
s.visit_uint(*self as uint)
fn visit<
S,
R,
E,
V: ser::Visitor<S, R, E>,
>(&self, state: &mut S, visitor: V) -> Result<R, E> {
visitor.visit_uint(state, *self as uint)
}
}
/*
impl<D: de::Deserializer<E>, E> de::Deserializable<D, E> for CacheStatus {
impl<D: de::Deserializer<E>, E> de::Deserialize<D, E> for CacheStatus {
#[inline]
fn deserialize_token(d: &mut D, token: de::Token) -> Result<CacheStatus, E> {
d.expect_from_primitive(token)
@ -111,8 +125,8 @@ impl<D: de::Deserializer<E>, E> de::Deserializable<D, E> for CacheStatus {
*/
#[deriving(Encodable, Decodable)]
#[deriving_serializable]
//#[deriving_deserializable]
#[deriving_serialize]
//#[deriving_deserialize]
struct Origin {
ip: String,
port: u32,
@ -127,15 +141,20 @@ enum OriginProtocol {
HTTPS,
}
impl<S: ser::VisitorState<R>, R> ser::Serialize<S, R> for OriginProtocol {
impl ser::Serialize for OriginProtocol {
#[inline]
fn serialize(&self, s: &mut S) -> R {
s.visit_uint(*self as uint)
fn visit<
S,
R,
E,
V: ser::Visitor<S, R, E>,
>(&self, state: &mut S, visitor: V) -> Result<R, E> {
visitor.visit_uint(state, *self as uint)
}
}
/*
impl<D: de::Deserializer<E>, E> de::Deserializable<D, E> for OriginProtocol {
impl<D: de::Deserializer<E>, E> de::Deserialize<D, E> for OriginProtocol {
#[inline]
fn deserialize_token(d: &mut D, token: de::Token) -> Result<OriginProtocol, E> {
d.expect_from_primitive(token)
@ -152,15 +171,20 @@ enum ZonePlan {
ENT,
}
impl<S: ser::VisitorState<R>, R> ser::Serialize<S, R> for ZonePlan {
impl ser::Serialize for ZonePlan {
#[inline]
fn serialize(&self, s: &mut S) -> R {
s.visit_uint(*self as uint)
fn visit<
S,
R,
E,
V: ser::Visitor<S, R, E>,
>(&self, state: &mut S, visitor: V) -> Result<R, E> {
visitor.visit_uint(state, *self as uint)
}
}
/*
impl<D: de::Deserializer<E>, E> de::Deserializable<D, E> for ZonePlan {
impl<D: de::Deserializer<E>, E> de::Deserialize<D, E> for ZonePlan {
#[inline]
fn deserialize_token(d: &mut D, token: de::Token) -> Result<ZonePlan, E> {
d.expect_from_primitive(token)
@ -428,15 +452,20 @@ enum Country {
ZW,
}
impl<S: ser::VisitorState<R>, R> ser::Serialize<S, R> for Country {
impl ser::Serialize for Country {
#[inline]
fn serialize(&self, s: &mut S) -> R {
s.visit_uint(*self as uint)
fn visit<
S,
R,
E,
V: ser::Visitor<S, R, E>,
>(&self, state: &mut S, visitor: V) -> Result<R, E> {
visitor.visit_uint(state, *self as uint)
}
}
/*
impl<D: de::Deserializer<E>, E> de::Deserializable<D, E> for Country {
impl<D: de::Deserializer<E>, E> de::Deserialize<D, E> for Country {
#[inline]
fn deserialize_token(d: &mut D, token: de::Token) -> Result<Country, E> {
d.expect_from_primitive(token)
@ -445,8 +474,8 @@ impl<D: de::Deserializer<E>, E> de::Deserializable<D, E> for Country {
*/
#[deriving(Encodable, Decodable)]
#[deriving_serializable]
//#[deriving_deserializable]
#[deriving_serialize]
//#[deriving_deserialize]
struct Log {
timestamp: i64,
zone_id: u32,
@ -632,11 +661,11 @@ fn bench_serializer(b: &mut Bencher) {
fn bench_copy(b: &mut Bencher) {
let s = r#"{"timestamp":2837513946597,"zone_id":123456,"zone_plan":"FREE","http":{"protocol":"HTTP11","status":200,"host_status":503,"up_status":520,"method":"GET","content_type":"text/html","user_agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36","referer":"https://www.cloudflare.com/","request_uri":"/cdn-cgi/trace"},"origin":{"ip":"1.2.3.4","port":8000,"hostname":"www.example.com","protocol":"HTTPS"},"country":"US","cache_status":"Hit","server_ip":"192.168.1.1","server_name":"metal.cloudflare.com","remote_ip":"10.1.2.3","bytes_dlv":123456,"ray_id":"10c73629cce30078-LAX"}"#;
let json = Vec::from_slice(s.as_bytes());
let json = s.as_bytes().to_vec();
b.bytes = json.len() as u64;
b.iter(|| {
let _json = Vec::from_slice(s.as_bytes());
let _json = s.as_bytes().to_vec();
});
}
@ -930,7 +959,7 @@ fn bench_manual_my_mem_writer1_escape(b: &mut Bencher) {
fn direct<W: Writer>(wr: W, log: &Log) {
use serde2::ser::VisitorState;
let mut serializer = json::Serializer::new(wr);
let mut serializer = json::Writer::new(wr);
serializer.serialize_struct_start("Log", 12).unwrap();
serializer.serialize_struct_elt("timestamp", &log.timestamp).unwrap();

View File

@ -12,7 +12,7 @@ use syntax::ast::{
Item,
Expr,
MutMutable,
LitNil,
//LitNil,
};
use syntax::ast;
use syntax::codemap::Span;
@ -22,14 +22,14 @@ use syntax::ext::deriving::generic::{
EnumMatching,
FieldInfo,
MethodDef,
Named,
StaticFields,
StaticStruct,
StaticEnum,
//Named,
//StaticFields,
//StaticStruct,
//StaticEnum,
Struct,
Substructure,
TraitDef,
Unnamed,
//Unnamed,
combine_substructure,
};
use syntax::ext::deriving::generic::ty::{
@ -38,8 +38,8 @@ use syntax::ext::deriving::generic::ty::{
Literal,
Path,
Ptr,
Self,
Tuple,
//Self,
//Tuple,
borrowed_explicit_self,
};
use syntax::parse::token;
@ -51,21 +51,21 @@ use rustc::plugin::Registry;
#[doc(hidden)]
pub fn plugin_registrar(reg: &mut Registry) {
reg.register_syntax_extension(
token::intern("deriving_serializable"),
Decorator(box expand_deriving_serializable));
token::intern("deriving_serialize"),
Decorator(box expand_deriving_serialize));
/*
reg.register_syntax_extension(
token::intern("deriving_deserializable"),
ItemDecorator(box expand_deriving_deserializable));
token::intern("deriving_deserialize"),
ItemDecorator(box expand_deriving_deserialize));
*/
}
fn expand_deriving_serializable(cx: &mut ExtCtxt,
fn expand_deriving_serialize(cx: &mut ExtCtxt,
sp: Span,
mitem: &MetaItem,
item: &Item,
mut push: |P<ast::Item>|) {
push: |P<ast::Item>|) {
let inline = cx.meta_word(sp, token::InternedString::new("inline"));
let attrs = vec!(cx.attribute(sp, inline));
@ -73,54 +73,86 @@ fn expand_deriving_serializable(cx: &mut ExtCtxt,
let trait_def = TraitDef {
span: sp,
attributes: vec!(),
path: Path::new_(vec!("serde2", "ser", "Serialize"), None,
vec!(box Literal(Path::new_local("__S")),
box Literal(Path::new_local("__R"))), true),
path: Path::new(vec!["serde2", "ser", "Serialize"]),
additional_bounds: Vec::new(),
generics: LifetimeBounds {
lifetimes: Vec::new(),
bounds: vec!(("__S", None, vec!(Path::new_(
vec!("serde2", "ser", "VisitorState"), None,
vec!(box Literal(Path::new_local("__R"))), true))),
("__R", None, vec!()))
},
methods: vec!(
generics: LifetimeBounds::empty(),
methods: vec![
MethodDef {
name: "serialize",
generics: LifetimeBounds::empty(),
name: "visit",
generics: LifetimeBounds {
lifetimes: Vec::new(),
bounds: vec![
("__S", None, vec![]),
("__R", None, vec![]),
("__E", None, vec![]),
(
"__V",
None,
vec![
Path::new_(
vec!["serde2", "ser", "Visitor"],
None,
vec![
box Literal(Path::new_local("__S")),
box Literal(Path::new_local("__R")),
box Literal(Path::new_local("__E")),
],
true
),
],
),
]
},
explicit_self: borrowed_explicit_self(),
args: vec!(Ptr(box Literal(Path::new_local("__S")),
Borrowed(None, MutMutable))),
args: vec![
Ptr(
box Literal(Path::new_local("__S")),
Borrowed(None, MutMutable)
),
Literal(
Path::new_local("__V"),
),
],
ret_ty: Literal(
Path::new_local("__R")
Path::new_(
vec!("std", "result", "Result"),
None,
vec![
box Literal(Path::new_local("__R")),
box Literal(Path::new_local("__E")),
],
true
)
),
attributes: attrs,
combine_substructure: combine_substructure(|a, b, c| {
serializable_substructure(a, b, c)
serialize_substructure(a, b, c)
}),
})
}
]
};
trait_def.expand(cx, mitem, item, push)
}
fn serializable_substructure(cx: &ExtCtxt, span: Span, substr: &Substructure) -> P<Expr> {
let serializer = substr.nonself_args[0].clone();
fn serialize_substructure(cx: &ExtCtxt, span: Span, substr: &Substructure) -> P<Expr> {
let state = substr.nonself_args[0].clone();
let visitor = substr.nonself_args[1].clone();
match *substr.fields {
Struct(ref fields) => {
if fields.is_empty() {
serialize_tuple_struct(cx)
} else {
serialize_struct(cx, span, serializer, substr.type_ident, fields)
serialize_struct(cx, span, state, visitor, substr.type_ident, fields)
}
}
EnumMatching(_idx, variant, ref fields) => {
serialize_enum(cx, span, serializer, substr.type_ident, variant, fields)
serialize_enum(cx, span, state, visitor, substr.type_ident, variant, fields)
}
_ => cx.bug("expected Struct or EnumMatching in deriving_serializable")
_ => cx.bug("expected Struct or EnumMatching in deriving_serialize")
}
}
@ -131,7 +163,8 @@ fn serialize_tuple_struct(cx: &ExtCtxt) -> P<Expr> {
fn serialize_struct(cx: &ExtCtxt,
span: Span,
serializer: P<Expr>,
state: P<Expr>,
visitor: P<Expr>,
type_ident: Ident,
fields: &Vec<FieldInfo>) -> P<Expr> {
@ -150,12 +183,13 @@ fn serialize_struct(cx: &ExtCtxt,
};
let name = name.unwrap();
let name_expr = cx.expr_str(span, token::get_ident(name));
let expr = cx.expr_str(span, token::get_ident(name));
quote_arm!(cx,
$i => {
self.state += 1;
Some(s.visit_map_elt($first, $name_expr, &self.value.$name))
let v = try!(visitor.visit_map_elt(state, $first, $expr, &self.value.$name));
Ok(Some(v))
}
)
})
@ -169,14 +203,17 @@ fn serialize_struct(cx: &ExtCtxt,
impl<
'a,
S: ::serde2::VisitorState<R>,
R
> ::serde2::Visitor<S, R> for Visitor<'a> {
S,
R,
E,
> ::serde2::ser::MapVisitor<S, R, E> for Visitor<'a> {
#[inline]
fn visit(&mut self, s: &mut S) -> Option<R> {
fn visit<
V: ::serde2::ser::Visitor<S, R, E>,
>(&mut self, state: &mut S, visitor: V) -> Result<Option<R>, E> {
match self.state {
$arms
_ => None,
_ => Ok(None),
}
}
@ -187,7 +224,7 @@ fn serialize_struct(cx: &ExtCtxt,
}
}
$serializer.visit_named_map($type_name, Visitor {
$visitor.visit_named_map($state, $type_name, Visitor {
value: self,
state: 0,
})
@ -196,7 +233,8 @@ fn serialize_struct(cx: &ExtCtxt,
fn serialize_enum(cx: &ExtCtxt,
span: Span,
serializer: P<Expr>,
state: P<Expr>,
visitor: P<Expr>,
type_ident: Ident,
variant: &ast::Variant,
fields: &Vec<FieldInfo>) -> P<Expr> {
@ -211,23 +249,23 @@ fn serialize_enum(cx: &ExtCtxt,
let len = fields.len();
let stmts: Vec<P<ast::Stmt>> = fields.iter()
.map(|&FieldInfo { ref self_, span, .. }| {
.map(|&FieldInfo { ref self_, .. }| {
quote_stmt!(
cx,
try!($serializer.serialize_enum_elt(&$self_))
try!($visitor.serialize_enum_elt($state, &$self_))
)
})
.collect();
quote_expr!(cx, {
try!($serializer.serialize_enum_start($type_name, $variant_name, $len));
try!($visitor.serialize_enum_start($state, $type_name, $variant_name, $len));
$stmts
$serializer.serialize_enum_end()
$visitor.serialize_enum_end($state)
})
}
/*
pub fn expand_deriving_deserializable(cx: &mut ExtCtxt,
pub fn expand_deriving_deserialize(cx: &mut ExtCtxt,
span: Span,
mitem: &MetaItem,
item: &Item,
@ -235,7 +273,7 @@ pub fn expand_deriving_deserializable(cx: &mut ExtCtxt,
let trait_def = TraitDef {
span: span,
attributes: Vec::new(),
path: Path::new_(vec!("serde2", "de", "Deserializable"), None,
path: Path::new_(vec!("serde2", "de", "Deserialize"), None,
vec!(box Literal(Path::new_local("__D")),
box Literal(Path::new_local("__E"))), true),
additional_bounds: Vec::new(),
@ -271,7 +309,7 @@ pub fn expand_deriving_deserializable(cx: &mut ExtCtxt,
),
attributes: Vec::new(),
combine_substructure: combine_substructure(|a, b, c| {
deserializable_substructure(a, b, c)
deserialize_substructure(a, b, c)
}),
})
};
@ -279,7 +317,7 @@ pub fn expand_deriving_deserializable(cx: &mut ExtCtxt,
trait_def.expand(cx, mitem, item, push)
}
fn deserializable_substructure(cx: &mut ExtCtxt, span: Span,
fn deserialize_substructure(cx: &mut ExtCtxt, span: Span,
substr: &Substructure) -> P<Expr> {
let deserializer = substr.nonself_args[0];
let token = substr.nonself_args[1];
@ -303,7 +341,7 @@ fn deserializable_substructure(cx: &mut ExtCtxt, span: Span,
deserializer,
token)
}
_ => cx.bug("expected StaticEnum or StaticStruct in deriving(Deserializable)")
_ => cx.bug("expected StaticEnum or StaticStruct in deriving(Deserialize)")
}
}
@ -356,7 +394,7 @@ fn deserialize_struct_from_struct(
) -> P<ast::Expr> {
let expect_struct_field = cx.ident_of("expect_struct_field");
let call = deserializable_static_fields(
let call = deserialize_static_fields(
cx,
span,
type_ident,
@ -403,7 +441,7 @@ fn deserialize_struct_from_map(
quote_arm!(cx,
$s => {
$name = Some(
try!(::serde2::de::Deserializable::deserialize($deserializer))
try!(::serde2::de::Deserialize::deserialize($deserializer))
);
continue;
})
@ -489,7 +527,7 @@ fn deserialize_enum(
let arms: Vec<ast::Arm> = fields.iter()
.enumerate()
.map(|(i, &(name, span, ref parts))| {
let call = deserializable_static_fields(
let call = deserialize_static_fields(
cx,
span,
name,
@ -520,7 +558,7 @@ 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 deserializable_static_fields(
fn deserialize_static_fields(
cx: &ExtCtxt,
span: Span,
outer_pat_ident: Ident,

View File

@ -4,8 +4,8 @@ use std::num;
///////////////////////////////////////////////////////////////////////////////
pub trait Deserialize<D, E> {
fn deserialize(d: &mut D) -> Result<Self, E>;
pub trait Deserialize<S, E> {
fn deserialize(state: &mut S) -> Result<Self, E>;
}
pub trait Deserializer<E> {
@ -26,95 +26,95 @@ pub trait Deserializer<E> {
fn end_of_stream_error(&mut self) -> E;
}
pub trait Visitor<D: Deserializer<E>, R, E> {
fn visit_null(&mut self, d: &mut D) -> Result<R, E> {
Err(d.syntax_error())
pub trait Visitor<S: Deserializer<E>, R, E> {
fn visit_null(&mut self, state: &mut S) -> Result<R, E> {
Err(state.syntax_error())
}
fn visit_bool(&mut self, d: &mut D, _v: bool) -> Result<R, E> {
Err(d.syntax_error())
fn visit_bool(&mut self, state: &mut S, _v: bool) -> Result<R, E> {
Err(state.syntax_error())
}
fn visit_int(&mut self, d: &mut D, v: int) -> Result<R, E> {
self.visit_i64(d, v as i64)
fn visit_int(&mut self, state: &mut S, v: int) -> Result<R, E> {
self.visit_i64(state, v as i64)
}
fn visit_i64(&mut self, d: &mut D, _v: i64) -> Result<R, E> {
Err(d.syntax_error())
fn visit_i64(&mut self, state: &mut S, _v: i64) -> Result<R, E> {
Err(state.syntax_error())
}
fn visit_uint(&mut self, d: &mut D, v: uint) -> Result<R, E> {
self.visit_u64(d, v as u64)
fn visit_uint(&mut self, state: &mut S, v: uint) -> Result<R, E> {
self.visit_u64(state, v as u64)
}
fn visit_u64(&mut self, d: &mut D, _v: u64) -> Result<R, E> {
Err(d.syntax_error())
fn visit_u64(&mut self, state: &mut S, _v: u64) -> Result<R, E> {
Err(state.syntax_error())
}
fn visit_f32(&mut self, d: &mut D, v: f32) -> Result<R, E> {
self.visit_f64(d, v as f64)
fn visit_f32(&mut self, state: &mut S, v: f32) -> Result<R, E> {
self.visit_f64(state, v as f64)
}
fn visit_f64(&mut self, d: &mut D, _v: f64) -> Result<R, E> {
Err(d.syntax_error())
fn visit_f64(&mut self, state: &mut S, _v: f64) -> Result<R, E> {
Err(state.syntax_error())
}
fn visit_str(&mut self, d: &mut D, _v: &str) -> Result<R, E> {
Err(d.syntax_error())
fn visit_str(&mut self, state: &mut S, _v: &str) -> Result<R, E> {
Err(state.syntax_error())
}
fn visit_string(&mut self, d: &mut D, v: String) -> Result<R, E> {
self.visit_str(d, v.as_slice())
fn visit_string(&mut self, state: &mut S, v: String) -> Result<R, E> {
self.visit_str(state, v.as_slice())
}
fn visit_option<
V: OptionVisitor<D, E>,
>(&mut self, d: &mut D, _visitor: V) -> Result<R, E> {
Err(d.syntax_error())
V: OptionVisitor<S, E>,
>(&mut self, state: &mut S, _visitor: V) -> Result<R, E> {
Err(state.syntax_error())
}
fn visit_seq<
V: SeqVisitor<D, E>,
>(&mut self, d: &mut D, _visitor: V) -> Result<R, E> {
Err(d.syntax_error())
V: SeqVisitor<S, E>,
>(&mut self, state: &mut S, _visitor: V) -> Result<R, E> {
Err(state.syntax_error())
}
fn visit_map<
V: MapVisitor<D, E>,
>(&mut self, d: &mut D, _visitor: V) -> Result<R, E> {
Err(d.syntax_error())
V: MapVisitor<S, E>,
>(&mut self, state: &mut S, _visitor: V) -> Result<R, E> {
Err(state.syntax_error())
}
}
pub trait OptionVisitor<D, E> {
pub trait OptionVisitor<S, E> {
fn visit<
T: Deserialize<D, E>,
>(&mut self, d: &mut D) -> Result<Option<T>, E>;
T: Deserialize<S, E>,
>(&mut self, state: &mut S) -> Result<Option<T>, E>;
}
pub trait SeqVisitor<D, E> {
pub trait SeqVisitor<S, E> {
fn visit<
T: Deserialize<D, E>,
>(&mut self, d: &mut D) -> Result<Option<T>, E>;
T: Deserialize<S, E>,
>(&mut self, state: &mut S) -> Result<Option<T>, E>;
fn end(&mut self, d: &mut D) -> Result<(), E>;
fn end(&mut self, state: &mut S) -> Result<(), E>;
#[inline]
fn size_hint(&self, _d: &mut D) -> (uint, Option<uint>) {
fn size_hint(&self, _state: &mut S) -> (uint, Option<uint>) {
(0, None)
}
}
pub trait MapVisitor<D, E> {
pub trait MapVisitor<S, E> {
fn visit<
K: Deserialize<D, E>,
V: Deserialize<D, E>,
>(&mut self, d: &mut D) -> Result<Option<(K, V)>, E>;
K: Deserialize<S, E>,
V: Deserialize<S, E>,
>(&mut self, state: &mut S) -> Result<Option<(K, V)>, E>;
fn end(&mut self, d: &mut D) -> Result<(), E>;
fn end(&mut self, state: &mut S) -> Result<(), E>;
#[inline]
fn size_hint(&self, _d: &mut D) -> (uint, Option<uint>) {
fn size_hint(&self, _state: &mut S) -> (uint, Option<uint>) {
(0, None)
}
}
@ -122,45 +122,45 @@ pub trait MapVisitor<D, E> {
///////////////////////////////////////////////////////////////////////////////
impl<
D: Deserializer<E>,
S: Deserializer<E>,
E,
> Deserialize<D, E> for () {
fn deserialize(d: &mut D) -> Result<(), E> {
> Deserialize<S, E> for () {
fn deserialize(state: &mut S) -> Result<(), E> {
struct Visitor;
impl<D: Deserializer<E>, E> self::Visitor<D, (), E> for Visitor {
fn visit_null(&mut self, _d: &mut D) -> Result<(), E> {
impl<S: Deserializer<E>, E> self::Visitor<S, (), E> for Visitor {
fn visit_null(&mut self, _state: &mut S) -> Result<(), E> {
Ok(())
}
fn visit_seq<
V: SeqVisitor<D, E>,
>(&mut self, d: &mut D, mut visitor: V) -> Result<(), E> {
try!(visitor.end(d));
V: SeqVisitor<S, E>,
>(&mut self, state: &mut S, mut visitor: V) -> Result<(), E> {
try!(visitor.end(state));
Ok(())
}
}
d.visit(&mut Visitor)
state.visit(&mut Visitor)
}
}
///////////////////////////////////////////////////////////////////////////////
impl<
D: Deserializer<E>,
S: Deserializer<E>,
E,
> Deserialize<D, E> for bool {
fn deserialize(d: &mut D) -> Result<bool, E> {
> Deserialize<S, E> for bool {
fn deserialize(state: &mut S) -> Result<bool, E> {
struct Visitor;
impl<D: Deserializer<E>, E> self::Visitor<D, bool, E> for Visitor {
fn visit_bool(&mut self, _d: &mut D, v: bool) -> Result<bool, E> {
impl<S: Deserializer<E>, E> self::Visitor<S, bool, E> for Visitor {
fn visit_bool(&mut self, _state: &mut S, v: bool) -> Result<bool, E> {
Ok(v)
}
}
d.visit(&mut Visitor)
state.visit(&mut Visitor)
}
}
@ -168,10 +168,10 @@ impl<
macro_rules! impl_deserialize_num_method {
($dst_ty:ty, $src_ty:ty, $method:ident) => {
fn $method(&mut self, d: &mut D, v: $src_ty) -> Result<$dst_ty, E> {
fn $method(&mut self, state: &mut S, v: $src_ty) -> Result<$dst_ty, E> {
match num::cast(v) {
Some(v) => Ok(v),
None => Err(d.syntax_error()),
None => Err(state.syntax_error()),
}
}
}
@ -179,12 +179,12 @@ macro_rules! impl_deserialize_num_method {
macro_rules! impl_deserialize_num {
($ty:ty) => {
impl<D: Deserializer<E>, E> Deserialize<D, E> for $ty {
impl<S: Deserializer<E>, E> Deserialize<S, E> for $ty {
#[inline]
fn deserialize(d: &mut D) -> Result<$ty, E> {
fn deserialize(state: &mut S) -> Result<$ty, E> {
struct Visitor;
impl<D: Deserializer<E>, E> self::Visitor<D, $ty, E> for Visitor {
impl<S: Deserializer<E>, E> self::Visitor<S, $ty, E> for Visitor {
impl_deserialize_num_method!($ty, int, visit_int)
impl_deserialize_num_method!($ty, i64, visit_i64)
impl_deserialize_num_method!($ty, uint, visit_uint)
@ -193,7 +193,7 @@ macro_rules! impl_deserialize_num {
impl_deserialize_num_method!($ty, f64, visit_f64)
}
d.visit(&mut Visitor)
state.visit(&mut Visitor)
}
}
}
@ -209,75 +209,75 @@ impl_deserialize_num!(f64)
///////////////////////////////////////////////////////////////////////////////
impl<
D: Deserializer<E>,
S: Deserializer<E>,
E,
> Deserialize<D, E> for String {
fn deserialize(d: &mut D) -> Result<String, E> {
> Deserialize<S, E> for String {
fn deserialize(state: &mut S) -> Result<String, E> {
struct Visitor;
impl<D: Deserializer<E>, E> self::Visitor<D, String, E> for Visitor {
fn visit_str(&mut self, _d: &mut D, v: &str) -> Result<String, E> {
impl<S: Deserializer<E>, E> self::Visitor<S, String, E> for Visitor {
fn visit_str(&mut self, _state: &mut S, v: &str) -> Result<String, E> {
Ok(v.to_string())
}
fn visit_string(&mut self, _d: &mut D, v: String) -> Result<String, E> {
fn visit_string(&mut self, _state: &mut S, v: String) -> Result<String, E> {
Ok(v)
}
}
d.visit(&mut Visitor)
state.visit(&mut Visitor)
}
}
///////////////////////////////////////////////////////////////////////////////
impl<
T: Deserialize<D, E>,
D: Deserializer<E>,
T: Deserialize<S, E>,
S: Deserializer<E>,
E,
> Deserialize<D, E> for Option<T> {
fn deserialize(d: &mut D) -> Result<Option<T>, E> {
> Deserialize<S, E> for Option<T> {
fn deserialize(state: &mut S) -> Result<Option<T>, E> {
struct Visitor;
impl<
T: Deserialize<D, E>,
D: Deserializer<E>,
T: Deserialize<S, E>,
S: Deserializer<E>,
E,
> self::Visitor<D, Option<T>, E> for Visitor {
> self::Visitor<S, Option<T>, E> for Visitor {
fn visit_option<
V: OptionVisitor<D, E>,
>(&mut self, d: &mut D, mut visitor: V) -> Result<Option<T>, E> {
visitor.visit(d)
V: OptionVisitor<S, E>,
>(&mut self, state: &mut S, mut visitor: V) -> Result<Option<T>, E> {
visitor.visit(state)
}
}
d.visit_option(&mut Visitor)
state.visit_option(&mut Visitor)
}
}
///////////////////////////////////////////////////////////////////////////////
impl<
T: Deserialize<D, E>,
D: Deserializer<E>,
T: Deserialize<S, E>,
S: Deserializer<E>,
E,
> Deserialize<D, E> for Vec<T> {
fn deserialize(d: &mut D) -> Result<Vec<T>, E> {
> Deserialize<S, E> for Vec<T> {
fn deserialize(state: &mut S) -> Result<Vec<T>, E> {
struct Visitor;
impl<
T: Deserialize<D, E>,
D: Deserializer<E>,
T: Deserialize<S, E>,
S: Deserializer<E>,
E,
> self::Visitor<D, Vec<T>, E> for Visitor {
> self::Visitor<S, Vec<T>, E> for Visitor {
fn visit_seq<
V: SeqVisitor<D, E>,
>(&mut self, d: &mut D, mut visitor: V) -> Result<Vec<T>, E> {
let (len, _) = visitor.size_hint(d);
V: SeqVisitor<S, E>,
>(&mut self, state: &mut S, mut visitor: V) -> Result<Vec<T>, E> {
let (len, _) = visitor.size_hint(state);
let mut values = Vec::with_capacity(len);
loop {
match try!(visitor.visit(d)) {
match try!(visitor.visit(state)) {
Some(value) => {
values.push(value);
}
@ -291,7 +291,7 @@ impl<
}
}
d.visit(&mut Visitor)
state.visit(&mut Visitor)
}
}
@ -309,37 +309,37 @@ macro_rules! impl_deserialize_tuple {
peel!($($name,)*)
impl<
D: Deserializer<E>,
S: Deserializer<E>,
E,
$($name: Deserialize<D, E>),+
> Deserialize<D, E> for ($($name,)+) {
$($name: Deserialize<S, E>),+
> Deserialize<S, E> for ($($name,)+) {
#[inline]
#[allow(non_snake_case)]
fn deserialize(d: &mut D) -> Result<($($name,)+), E> {
fn deserialize(state: &mut S) -> Result<($($name,)+), E> {
struct Visitor;
impl<
D: Deserializer<E>,
S: Deserializer<E>,
E,
$($name: Deserialize<D, E>,)+
> self::Visitor<D, ($($name,)+), E> for Visitor {
$($name: Deserialize<S, E>,)+
> self::Visitor<S, ($($name,)+), E> for Visitor {
fn visit_seq<
V: SeqVisitor<D, E>,
>(&mut self, d: &mut D, mut visitor: V) -> Result<($($name,)+), E> {
V: SeqVisitor<S, E>,
>(&mut self, state: &mut S, mut visitor: V) -> Result<($($name,)+), E> {
$(
let $name = match try!(visitor.visit(d)) {
let $name = match try!(visitor.visit(state)) {
Some(value) => value,
None => { return Err(d.end_of_stream_error()); }
None => { return Err(state.end_of_stream_error()); }
};
)+;
try!(visitor.end(d));
try!(visitor.end(state));
Ok(($($name,)+))
}
}
d.visit(&mut Visitor)
state.visit(&mut Visitor)
}
}
}
@ -350,28 +350,28 @@ impl_deserialize_tuple! { T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, }
///////////////////////////////////////////////////////////////////////////////
impl<
K: Deserialize<D, E> + Eq + Hash,
V: Deserialize<D, E>,
D: Deserializer<E>,
K: Deserialize<S, E> + Eq + Hash,
V: Deserialize<S, E>,
S: Deserializer<E>,
E,
> Deserialize<D, E> for HashMap<K, V> {
fn deserialize(d: &mut D) -> Result<HashMap<K, V>, E> {
> Deserialize<S, E> for HashMap<K, V> {
fn deserialize(state: &mut S) -> Result<HashMap<K, V>, E> {
struct Visitor;
impl<
K: Deserialize<D, E> + Eq + Hash,
V: Deserialize<D, E>,
D: Deserializer<E>,
K: Deserialize<S, E> + Eq + Hash,
V: Deserialize<S, E>,
S: Deserializer<E>,
E,
> self::Visitor<D, HashMap<K, V>, E> for Visitor {
> self::Visitor<S, HashMap<K, V>, E> for Visitor {
fn visit_map<
Visitor: MapVisitor<D, E>,
>(&mut self, d: &mut D, mut visitor: Visitor) -> Result<HashMap<K, V>, E> {
let (len, _) = visitor.size_hint(d);
Visitor: MapVisitor<S, E>,
>(&mut self, state: &mut S, mut visitor: Visitor) -> Result<HashMap<K, V>, E> {
let (len, _) = visitor.size_hint(state);
let mut values = HashMap::with_capacity(len);
loop {
match try!(visitor.visit(d)) {
match try!(visitor.visit(state)) {
Some((key, value)) => {
values.insert(key, value);
}
@ -385,32 +385,32 @@ impl<
}
}
d.visit(&mut Visitor)
state.visit(&mut Visitor)
}
}
impl<
K: Deserialize<D, E> + Eq + Ord,
V: Deserialize<D, E>,
D: Deserializer<E>,
K: Deserialize<S, E> + Eq + Ord,
V: Deserialize<S, E>,
S: Deserializer<E>,
E,
> Deserialize<D, E> for TreeMap<K, V> {
fn deserialize(d: &mut D) -> Result<TreeMap<K, V>, E> {
> Deserialize<S, E> for TreeMap<K, V> {
fn deserialize(state: &mut S) -> Result<TreeMap<K, V>, E> {
struct Visitor;
impl<
K: Deserialize<D, E> + Eq + Ord,
V: Deserialize<D, E>,
D: Deserializer<E>,
K: Deserialize<S, E> + Eq + Ord,
V: Deserialize<S, E>,
S: Deserializer<E>,
E,
> self::Visitor<D, TreeMap<K, V>, E> for Visitor {
> self::Visitor<S, TreeMap<K, V>, E> for Visitor {
fn visit_map<
Visitor: MapVisitor<D, E>,
>(&mut self, d: &mut D, mut visitor: Visitor) -> Result<TreeMap<K, V>, E> {
Visitor: MapVisitor<S, E>,
>(&mut self, state: &mut S, mut visitor: Visitor) -> Result<TreeMap<K, V>, E> {
let mut values = TreeMap::new();
loop {
match try!(visitor.visit(d)) {
match try!(visitor.visit(state)) {
Some((key, value)) => {
values.insert(key, value);
}
@ -424,6 +424,6 @@ impl<
}
}
d.visit(&mut Visitor)
state.visit(&mut Visitor)
}
}

View File

@ -1,6 +1,6 @@
#![feature(macro_rules)]
//pub use ser::{Serialize, Serializer};
pub use ser::{Serialize, Serializer};
//pub use ser::{Visitor, VisitorState};
//pub use ser::GatherTokens;