more simplification

This commit is contained in:
Erick Tryzelaar
2014-07-27 21:07:01 -07:00
parent 14d97f9c26
commit 7200be09e1
3 changed files with 85 additions and 110 deletions
+60 -97
View File
@@ -124,7 +124,7 @@ fn expand_deriving_serializable(cx: &mut ExtCtxt,
trait_def.expand(cx, mitem, item, push) trait_def.expand(cx, mitem, item, push)
} }
fn serializable_substructure(cx: &mut ExtCtxt, trait_span: Span, fn serializable_substructure(cx: &ExtCtxt, span: Span,
substr: &Substructure) -> Gc<Expr> { substr: &Substructure) -> Gc<Expr> {
let serializer = substr.nonself_args[0]; let serializer = substr.nonself_args[0];
@@ -132,101 +132,64 @@ fn serializable_substructure(cx: &mut ExtCtxt, trait_span: Span,
Struct(ref fields) => { Struct(ref fields) => {
if fields.is_empty() { if fields.is_empty() {
// unit structs have no fields and need to return `Ok()` // unit structs have no fields and need to return `Ok()`
cx.expr_ok(trait_span, cx.expr_lit(trait_span, LitNil)) quote_expr!(cx, Ok(()))
} else { } else {
let mut stmts = vec!(); let type_name = cx.expr_str(
span,
let call = cx.expr_method_call( token::get_ident(substr.type_ident)
trait_span,
serializer,
cx.ident_of("serialize_struct_start"),
vec!(
cx.expr_str(trait_span, token::get_ident(substr.type_ident)),
cx.expr_uint(trait_span, fields.len()),
)
); );
let call = cx.expr_try(trait_span, call); let len = fields.len();
stmts.push(cx.stmt_expr(call));
let emit_struct_sep = cx.ident_of("serialize_struct_sep"); let mut stmts: Vec<Gc<ast::Stmt>> = fields.iter()
.enumerate()
.map(|(i, &FieldInfo { name, self_, span, .. })| {
let name = match name {
Some(id) => token::get_ident(id),
None => token::intern_and_get_ident(format!("_field{}", i).as_slice()),
};
for (i, &FieldInfo { let name = cx.expr_str(span, name);
name,
self_, quote_stmt!(
span, cx,
.. try!($serializer.serialize_struct_elt($name, &$self_))
}) in fields.iter().enumerate() {
let name = match name {
Some(id) => token::get_ident(id),
None => token::intern_and_get_ident(format!("_field{}", i).as_slice()),
};
let call = cx.expr_method_call(
span,
serializer,
emit_struct_sep,
vec!(
cx.expr_str(span, name),
cx.expr_addr_of(span, self_),
) )
); })
.collect();
let call = cx.expr_try(span, call); quote_expr!(cx, {
stmts.push(cx.stmt_expr(call)); try!($serializer.serialize_struct_start($type_name, $len));
} $stmts
$serializer.serialize_struct_end()
let call = cx.expr_method_call( })
trait_span,
serializer,
cx.ident_of("serialize_struct_end"),
vec!()
);
cx.expr_block(cx.block(trait_span, stmts, Some(call)))
} }
} }
EnumMatching(_idx, variant, ref fields) => { EnumMatching(_idx, variant, ref fields) => {
let mut stmts = vec!(); let type_name = cx.expr_str(
span,
let call = cx.expr_method_call( token::get_ident(substr.type_ident)
trait_span,
serializer,
cx.ident_of("serialize_enum_start"),
vec!(
cx.expr_str(trait_span, token::get_ident(substr.type_ident)),
cx.expr_str(trait_span, token::get_ident(variant.node.name)),
cx.expr_uint(trait_span, fields.len()),
)
); );
let variant_name = cx.expr_str(
span,
token::get_ident(variant.node.name)
);
let len = fields.len();
let call = cx.expr_try(trait_span, call); let stmts: Vec<Gc<ast::Stmt>> = fields.iter()
stmts.push(cx.stmt_expr(call)); .map(|&FieldInfo { self_, span, .. }| {
quote_stmt!(
let serialize_struct_sep = cx.ident_of("serialize_enum_sep"); cx,
try!($serializer.serialize_enum_elt(&$self_))
for &FieldInfo { self_, span, .. } in fields.iter() {
let call = cx.expr_method_call(
span,
serializer,
serialize_struct_sep,
vec!(
cx.expr_addr_of(span, self_),
) )
); })
.collect();
let call = cx.expr_try(span, call); quote_expr!(cx, {
try!($serializer.serialize_enum_start($type_name, $variant_name, $len));
stmts.push(cx.stmt_expr(call)); $stmts
} $serializer.serialize_enum_end()
})
let call = cx.expr_method_call(
trait_span,
serializer,
cx.ident_of("serialize_enum_end"),
vec!()
);
cx.expr_block(cx.block(trait_span, stmts, Some(call)))
} }
_ => cx.bug("expected Struct or EnumMatching in deriving_serializable") _ => cx.bug("expected Struct or EnumMatching in deriving_serializable")
@@ -300,7 +263,7 @@ pub fn expand_deriving_deserializable(cx: &mut ExtCtxt,
trait_def.expand(cx, mitem, item, push) trait_def.expand(cx, mitem, item, push)
} }
fn deserializable_substructure(cx: &mut ExtCtxt, trait_span: Span, fn deserializable_substructure(cx: &mut ExtCtxt, span: Span,
substr: &Substructure) -> Gc<Expr> { substr: &Substructure) -> Gc<Expr> {
let deserializer = substr.nonself_args[0]; let deserializer = substr.nonself_args[0];
let token = substr.nonself_args[1]; let token = substr.nonself_args[1];
@@ -309,7 +272,7 @@ fn deserializable_substructure(cx: &mut ExtCtxt, trait_span: Span,
StaticStruct(_, ref fields) => { StaticStruct(_, ref fields) => {
deserialize_struct( deserialize_struct(
cx, cx,
trait_span, span,
substr.type_ident, substr.type_ident,
fields, fields,
deserializer, deserializer,
@@ -318,7 +281,7 @@ fn deserializable_substructure(cx: &mut ExtCtxt, trait_span: Span,
StaticEnum(_, ref fields) => { StaticEnum(_, ref fields) => {
deserialize_enum( deserialize_enum(
cx, cx,
trait_span, span,
substr.type_ident, substr.type_ident,
fields.as_slice(), fields.as_slice(),
deserializer, deserializer,
@@ -328,8 +291,8 @@ fn deserializable_substructure(cx: &mut ExtCtxt, trait_span: Span,
} }
} }
fn deserialize_struct<'a>( fn deserialize_struct(
cx: &ExtCtxt<'a>, cx: &ExtCtxt,
span: Span, span: Span,
type_ident: Ident, type_ident: Ident,
fields: &StaticFields, fields: &StaticFields,
@@ -362,8 +325,8 @@ fn deserialize_struct<'a>(
) )
} }
fn deserialize_struct_from_struct<'a>( fn deserialize_struct_from_struct(
cx: &ExtCtxt<'a>, cx: &ExtCtxt,
span: Span, span: Span,
type_ident: Ident, type_ident: Ident,
fields: &StaticFields, fields: &StaticFields,
@@ -418,8 +381,8 @@ fn deserialize_struct_from_struct<'a>(
) )
} }
fn deserialize_struct_from_map<'a>( fn deserialize_struct_from_map(
cx: &ExtCtxt<'a>, cx: &ExtCtxt,
span: Span, span: Span,
type_ident: Ident, type_ident: Ident,
fields: &StaticFields, fields: &StaticFields,
@@ -509,8 +472,8 @@ fn deserialize_struct_from_map<'a>(
}) })
} }
fn deserialize_enum<'a>( fn deserialize_enum(
cx: &ExtCtxt<'a>, cx: &ExtCtxt,
span: Span, span: Span,
type_ident: Ident, type_ident: Ident,
fields: &[(Ident, Span, StaticFields)], fields: &[(Ident, Span, StaticFields)],
@@ -563,7 +526,7 @@ fn deserialize_enum<'a>(
/// - `getarg` should retrieve the `uint`-th field with name `&str`. /// - `getarg` should retrieve the `uint`-th field with name `&str`.
fn deserializable_static_fields( fn deserializable_static_fields(
cx: &ExtCtxt, cx: &ExtCtxt,
trait_span: Span, span: Span,
outer_pat_ident: Ident, outer_pat_ident: Ident,
fields: &StaticFields, fields: &StaticFields,
getarg: |&ExtCtxt, Span, token::InternedString| -> Gc<Expr> getarg: |&ExtCtxt, Span, token::InternedString| -> Gc<Expr>
@@ -571,7 +534,7 @@ fn deserializable_static_fields(
match *fields { match *fields {
Unnamed(ref fields) => { Unnamed(ref fields) => {
if fields.is_empty() { if fields.is_empty() {
cx.expr_ident(trait_span, outer_pat_ident) cx.expr_ident(span, outer_pat_ident)
} else { } else {
let fields = fields.iter().enumerate().map(|(i, &span)| { let fields = fields.iter().enumerate().map(|(i, &span)| {
getarg( getarg(
@@ -581,7 +544,7 @@ fn deserializable_static_fields(
) )
}).collect(); }).collect();
cx.expr_call_ident(trait_span, outer_pat_ident, fields) cx.expr_call_ident(span, outer_pat_ident, fields)
} }
} }
Named(ref fields) => { Named(ref fields) => {
@@ -595,7 +558,7 @@ fn deserializable_static_fields(
cx.field_imm(span, name, arg) cx.field_imm(span, name, arg)
}).collect(); }).collect();
cx.expr_struct_ident(trait_span, outer_pat_ident, fields) cx.expr_struct_ident(span, outer_pat_ident, fields)
} }
} }
} }
+18 -6
View File
@@ -925,7 +925,7 @@ impl<W: Writer> ser::Serializer<io::IoError> for Serializer<W> {
} }
#[inline] #[inline]
fn serialize_tuple_sep< fn serialize_tuple_elt<
T: Serializable T: Serializable
>(&mut self, value: &T) -> IoResult<()> { >(&mut self, value: &T) -> IoResult<()> {
if self.first { if self.first {
@@ -948,7 +948,7 @@ impl<W: Writer> ser::Serializer<io::IoError> for Serializer<W> {
} }
#[inline] #[inline]
fn serialize_struct_sep< fn serialize_struct_elt<
T: Serializable T: Serializable
>(&mut self, name: &str, value: &T) -> IoResult<()> { >(&mut self, name: &str, value: &T) -> IoResult<()> {
if self.first { if self.first {
@@ -975,7 +975,7 @@ impl<W: Writer> ser::Serializer<io::IoError> for Serializer<W> {
} }
#[inline] #[inline]
fn serialize_enum_sep< fn serialize_enum_elt<
T: Serializable T: Serializable
>(&mut self, value: &T) -> IoResult<()> { >(&mut self, value: &T) -> IoResult<()> {
if self.first { if self.first {
@@ -1184,7 +1184,7 @@ impl<W: Writer> ser::Serializer<io::IoError> for PrettySerializer<W> {
} }
#[inline] #[inline]
fn serialize_tuple_sep< fn serialize_tuple_elt<
T: Serializable T: Serializable
>(&mut self, value: &T) -> IoResult<()> { >(&mut self, value: &T) -> IoResult<()> {
try!(self.serialize_sep()); try!(self.serialize_sep());
@@ -1203,7 +1203,7 @@ impl<W: Writer> ser::Serializer<io::IoError> for PrettySerializer<W> {
} }
#[inline] #[inline]
fn serialize_struct_sep< fn serialize_struct_elt<
T: Serializable T: Serializable
>(&mut self, name: &str, value: &T) -> IoResult<()> { >(&mut self, name: &str, value: &T) -> IoResult<()> {
try!(self.serialize_sep()); try!(self.serialize_sep());
@@ -1228,7 +1228,7 @@ impl<W: Writer> ser::Serializer<io::IoError> for PrettySerializer<W> {
} }
#[inline] #[inline]
fn serialize_enum_sep< fn serialize_enum_elt<
T: Serializable T: Serializable
>(&mut self, value: &T) -> IoResult<()> { >(&mut self, value: &T) -> IoResult<()> {
try!(self.serialize_sep()); try!(self.serialize_sep());
@@ -2234,11 +2234,14 @@ mod tests {
} }
#[deriving(PartialEq, Show)] #[deriving(PartialEq, Show)]
#[deriving_serializable]
#[deriving_deserializable]
enum Animal { enum Animal {
Dog, Dog,
Frog(String, Vec<int>) Frog(String, Vec<int>)
} }
/*
impl ser::Serializable for Animal { impl ser::Serializable for Animal {
#[inline] #[inline]
fn serialize< fn serialize<
@@ -2285,6 +2288,7 @@ mod tests {
} }
} }
} }
*/
impl ToJson for Animal { impl ToJson for Animal {
fn to_json(&self) -> Json { fn to_json(&self) -> Json {
@@ -2308,12 +2312,15 @@ mod tests {
} }
#[deriving(PartialEq, Show)] #[deriving(PartialEq, Show)]
#[deriving_serializable]
#[deriving_deserializable]
struct Inner { struct Inner {
a: (), a: (),
b: uint, b: uint,
c: Vec<String>, c: Vec<String>,
} }
/*
impl ser::Serializable for Inner { impl ser::Serializable for Inner {
#[inline] #[inline]
fn serialize< fn serialize<
@@ -2388,6 +2395,7 @@ mod tests {
} }
} }
} }
*/
impl ToJson for Inner { impl ToJson for Inner {
fn to_json(&self) -> Json { fn to_json(&self) -> Json {
@@ -2402,10 +2410,13 @@ mod tests {
} }
#[deriving(PartialEq, Show)] #[deriving(PartialEq, Show)]
#[deriving_serializable]
#[deriving_deserializable]
struct Outer { struct Outer {
inner: Vec<Inner>, inner: Vec<Inner>,
} }
/*
impl ser::Serializable for Outer { impl ser::Serializable for Outer {
#[inline] #[inline]
fn serialize< fn serialize<
@@ -2465,6 +2476,7 @@ mod tests {
} }
} }
} }
*/
impl ToJson for Outer { impl ToJson for Outer {
fn to_json(&self) -> Json { fn to_json(&self) -> Json {
+7 -7
View File
@@ -76,15 +76,15 @@ pub trait Serializer<E> {
fn serialize_str(&mut self, v: &str) -> Result<(), E>; fn serialize_str(&mut self, v: &str) -> Result<(), E>;
fn serialize_tuple_start(&mut self, len: uint) -> Result<(), E>; fn serialize_tuple_start(&mut self, len: uint) -> Result<(), E>;
fn serialize_tuple_sep<T: Serializable>(&mut self, v: &T) -> Result<(), E>; fn serialize_tuple_elt<T: Serializable>(&mut self, v: &T) -> Result<(), E>;
fn serialize_tuple_end(&mut self) -> Result<(), E>; fn serialize_tuple_end(&mut self) -> Result<(), E>;
fn serialize_struct_start(&mut self, name: &str, len: uint) -> Result<(), E>; fn serialize_struct_start(&mut self, name: &str, len: uint) -> Result<(), E>;
fn serialize_struct_sep<T: Serializable>(&mut self, name: &str, v: &T) -> Result<(), E>; fn serialize_struct_elt<T: Serializable>(&mut self, name: &str, v: &T) -> Result<(), E>;
fn serialize_struct_end(&mut self) -> Result<(), E>; fn serialize_struct_end(&mut self) -> Result<(), E>;
fn serialize_enum_start(&mut self, name: &str, variant: &str, len: uint) -> Result<(), E>; fn serialize_enum_start(&mut self, name: &str, variant: &str, len: uint) -> Result<(), E>;
fn serialize_enum_sep<T: Serializable>(&mut self, v: &T) -> Result<(), E>; fn serialize_enum_elt<T: Serializable>(&mut self, v: &T) -> Result<(), E>;
fn serialize_enum_end(&mut self) -> Result<(), E>; fn serialize_enum_end(&mut self) -> Result<(), E>;
fn serialize_option<T: Serializable>(&mut self, v: &Option<T>) -> Result<(), E>; fn serialize_option<T: Serializable>(&mut self, v: &Option<T>) -> Result<(), E>;
@@ -267,7 +267,7 @@ macro_rules! impl_serialize_tuple {
try!(s.serialize_tuple_start(len)); try!(s.serialize_tuple_start(len));
$( $(
try!(s.serialize_tuple_sep($name)); try!(s.serialize_tuple_elt($name));
)* )*
s.serialize_tuple_end() s.serialize_tuple_end()
} }
@@ -455,7 +455,7 @@ mod tests {
self.serialize(TupleStart(len)) self.serialize(TupleStart(len))
} }
fn serialize_tuple_sep< fn serialize_tuple_elt<
T: Serializable T: Serializable
>(&mut self, value: &T) -> Result<(), Error> { >(&mut self, value: &T) -> Result<(), Error> {
try!(self.serialize(TupleSep)); try!(self.serialize(TupleSep));
@@ -470,7 +470,7 @@ mod tests {
self.serialize(StructStart(name, len)) self.serialize(StructStart(name, len))
} }
fn serialize_struct_sep< fn serialize_struct_elt<
T: Serializable T: Serializable
>(&mut self, name: &str, value: &T) -> Result<(), Error> { >(&mut self, name: &str, value: &T) -> Result<(), Error> {
try!(self.serialize(StructSep(name))); try!(self.serialize(StructSep(name)));
@@ -485,7 +485,7 @@ mod tests {
self.serialize(EnumStart(name, variant, len)) self.serialize(EnumStart(name, variant, len))
} }
fn serialize_enum_sep< fn serialize_enum_elt<
T: Serializable T: Serializable
>(&mut self, value: &T) -> Result<(), Error> { >(&mut self, value: &T) -> Result<(), Error> {
try!(self.serialize(EnumSep)); try!(self.serialize(EnumSep));