Support (de)serialize_with in tuples
This commit is contained in:
parent
cc115ca43a
commit
938f42faf6
@ -177,31 +177,29 @@ pub struct FieldAttrs {
|
|||||||
name: Name,
|
name: Name,
|
||||||
skip_serializing_field: bool,
|
skip_serializing_field: bool,
|
||||||
skip_deserializing_field: bool,
|
skip_deserializing_field: bool,
|
||||||
skip_serializing_field_if: Option<P<ast::Expr>>,
|
skip_serializing_if: Option<ast::Path>,
|
||||||
default_expr_if_missing: Option<P<ast::Expr>>,
|
default_expr_if_missing: Option<P<ast::Expr>>,
|
||||||
serialize_with: Option<P<ast::Expr>>,
|
serialize_with: Option<ast::Path>,
|
||||||
deserialize_with: Option<ast::Path>,
|
deserialize_with: Option<ast::Path>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FieldAttrs {
|
impl FieldAttrs {
|
||||||
/// Extract out the `#[serde(...)]` attributes from a struct field.
|
/// Extract out the `#[serde(...)]` attributes from a struct field.
|
||||||
pub fn from_field(cx: &ExtCtxt,
|
pub fn from_field(cx: &ExtCtxt,
|
||||||
container_ty: &P<ast::Ty>,
|
index: usize,
|
||||||
generics: &ast::Generics,
|
field: &ast::StructField) -> Result<Self, Error> {
|
||||||
field: &ast::StructField,
|
|
||||||
is_enum: bool) -> Result<Self, Error> {
|
|
||||||
let builder = AstBuilder::new();
|
let builder = AstBuilder::new();
|
||||||
|
|
||||||
let field_ident = match field.ident {
|
let field_ident = match field.ident {
|
||||||
Some(ident) => ident,
|
Some(ident) => ident,
|
||||||
None => { cx.span_bug(field.span, "struct field has no name?") }
|
None => builder.id(index.to_string()),
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut field_attrs = FieldAttrs {
|
let mut field_attrs = FieldAttrs {
|
||||||
name: Name::new(field_ident),
|
name: Name::new(field_ident),
|
||||||
skip_serializing_field: false,
|
skip_serializing_field: false,
|
||||||
skip_deserializing_field: false,
|
skip_deserializing_field: false,
|
||||||
skip_serializing_field_if: None,
|
skip_serializing_if: None,
|
||||||
default_expr_if_missing: None,
|
default_expr_if_missing: None,
|
||||||
serialize_with: None,
|
serialize_with: None,
|
||||||
deserialize_with: None,
|
deserialize_with: None,
|
||||||
@ -260,27 +258,14 @@ impl FieldAttrs {
|
|||||||
|
|
||||||
// Parse `#[serde(skip_serializing_if="...")]`
|
// Parse `#[serde(skip_serializing_if="...")]`
|
||||||
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"skip_serializing_if" => {
|
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"skip_serializing_if" => {
|
||||||
let expr = wrap_skip_serializing(
|
let path = try!(parse_lit_into_path(cx, name, lit));
|
||||||
field_ident,
|
field_attrs.skip_serializing_if = Some(path);
|
||||||
try!(parse_lit_into_path(cx, name, lit)),
|
|
||||||
is_enum,
|
|
||||||
);
|
|
||||||
|
|
||||||
field_attrs.skip_serializing_field_if = Some(expr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse `#[serde(serialize_with="...")]`
|
// Parse `#[serde(serialize_with="...")]`
|
||||||
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"serialize_with" => {
|
ast::MetaItemKind::NameValue(ref name, ref lit) if name == &"serialize_with" => {
|
||||||
let expr = wrap_serialize_with(
|
let path = try!(parse_lit_into_path(cx, name, lit));
|
||||||
cx,
|
field_attrs.serialize_with = Some(path);
|
||||||
container_ty,
|
|
||||||
generics,
|
|
||||||
field_ident,
|
|
||||||
try!(parse_lit_into_path(cx, name, lit)),
|
|
||||||
is_enum,
|
|
||||||
);
|
|
||||||
|
|
||||||
field_attrs.serialize_with = Some(expr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse `#[serde(deserialize_with="...")]`
|
// Parse `#[serde(deserialize_with="...")]`
|
||||||
@ -316,15 +301,15 @@ impl FieldAttrs {
|
|||||||
self.skip_deserializing_field
|
self.skip_deserializing_field
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn skip_serializing_field_if(&self) -> Option<&P<ast::Expr>> {
|
pub fn skip_serializing_if(&self) -> Option<&ast::Path> {
|
||||||
self.skip_serializing_field_if.as_ref()
|
self.skip_serializing_if.as_ref()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn default_expr_if_missing(&self) -> Option<&P<ast::Expr>> {
|
pub fn default_expr_if_missing(&self) -> Option<&P<ast::Expr>> {
|
||||||
self.default_expr_if_missing.as_ref()
|
self.default_expr_if_missing.as_ref()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn serialize_with(&self) -> Option<&P<ast::Expr>> {
|
pub fn serialize_with(&self) -> Option<&ast::Path> {
|
||||||
self.serialize_with.as_ref()
|
self.serialize_with.as_ref()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -334,14 +319,17 @@ impl FieldAttrs {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Extract out the `#[serde(...)]` attributes from a struct field.
|
/// Zip together fields and `#[serde(...)]` attributes on those fields.
|
||||||
pub fn get_struct_field_attrs(cx: &ExtCtxt,
|
pub fn fields_with_attrs<'a>(
|
||||||
container_ty: &P<ast::Ty>,
|
cx: &ExtCtxt,
|
||||||
generics: &ast::Generics,
|
fields: &'a [ast::StructField],
|
||||||
fields: &[ast::StructField],
|
) -> Result<Vec<(&'a ast::StructField, FieldAttrs)>, Error> {
|
||||||
is_enum: bool) -> Result<Vec<FieldAttrs>, Error> {
|
|
||||||
fields.iter()
|
fields.iter()
|
||||||
.map(|field| FieldAttrs::from_field(cx, container_ty, generics, field, is_enum))
|
.enumerate()
|
||||||
|
.map(|(i, field)| {
|
||||||
|
let attrs = try!(FieldAttrs::from_field(cx, i, field));
|
||||||
|
Ok((field, attrs))
|
||||||
|
})
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -495,100 +483,3 @@ fn wrap_default(path: ast::Path) -> P<ast::Expr> {
|
|||||||
.build_path(path)
|
.build_path(path)
|
||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This function wraps the expression in `#[serde(skip_serializing_if="...")]` in a trait to
|
|
||||||
/// prevent it from accessing the internal `Serialize` state.
|
|
||||||
fn wrap_skip_serializing(field_ident: ast::Ident,
|
|
||||||
path: ast::Path,
|
|
||||||
is_enum: bool) -> P<ast::Expr> {
|
|
||||||
let builder = AstBuilder::new();
|
|
||||||
|
|
||||||
let expr = builder.expr()
|
|
||||||
.field(field_ident)
|
|
||||||
.field("value")
|
|
||||||
.self_();
|
|
||||||
|
|
||||||
let expr = if is_enum {
|
|
||||||
expr
|
|
||||||
} else {
|
|
||||||
builder.expr().ref_().build(expr)
|
|
||||||
};
|
|
||||||
|
|
||||||
builder.expr().call()
|
|
||||||
.build_path(path)
|
|
||||||
.arg().build(expr)
|
|
||||||
.build()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This function wraps the expression in `#[serde(serialize_with="...")]` in a trait to
|
|
||||||
/// prevent it from accessing the internal `Serialize` state.
|
|
||||||
fn wrap_serialize_with(cx: &ExtCtxt,
|
|
||||||
container_ty: &P<ast::Ty>,
|
|
||||||
generics: &ast::Generics,
|
|
||||||
field_ident: ast::Ident,
|
|
||||||
path: ast::Path,
|
|
||||||
is_enum: bool) -> P<ast::Expr> {
|
|
||||||
let builder = AstBuilder::new();
|
|
||||||
|
|
||||||
let expr = builder.expr()
|
|
||||||
.field(field_ident)
|
|
||||||
.self_();
|
|
||||||
|
|
||||||
let expr = if is_enum {
|
|
||||||
expr
|
|
||||||
} else {
|
|
||||||
builder.expr().ref_().build(expr)
|
|
||||||
};
|
|
||||||
|
|
||||||
let expr = builder.expr().call()
|
|
||||||
.build_path(path)
|
|
||||||
.arg().build(expr)
|
|
||||||
.arg()
|
|
||||||
.id("serializer")
|
|
||||||
.build();
|
|
||||||
|
|
||||||
let where_clause = &generics.where_clause;
|
|
||||||
|
|
||||||
quote_expr!(cx, {
|
|
||||||
trait __SerdeSerializeWith {
|
|
||||||
fn __serde_serialize_with<__S>(&self, serializer: &mut __S) -> Result<(), __S::Error>
|
|
||||||
where __S: _serde::ser::Serializer;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'__a, __T> __SerdeSerializeWith for &'__a __T
|
|
||||||
where __T: '__a + __SerdeSerializeWith,
|
|
||||||
{
|
|
||||||
fn __serde_serialize_with<__S>(&self, serializer: &mut __S) -> Result<(), __S::Error>
|
|
||||||
where __S: _serde::ser::Serializer
|
|
||||||
{
|
|
||||||
(**self).__serde_serialize_with(serializer)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl $generics __SerdeSerializeWith for $container_ty $where_clause {
|
|
||||||
fn __serde_serialize_with<__S>(&self, serializer: &mut __S) -> Result<(), __S::Error>
|
|
||||||
where __S: _serde::ser::Serializer
|
|
||||||
{
|
|
||||||
$expr
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct __SerdeSerializeWithStruct<'__a, __T: '__a> {
|
|
||||||
value: &'__a __T,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'__a, __T> _serde::ser::Serialize for __SerdeSerializeWithStruct<'__a, __T>
|
|
||||||
where __T: '__a + __SerdeSerializeWith
|
|
||||||
{
|
|
||||||
fn serialize<__S>(&self, serializer: &mut __S) -> Result<(), __S::Error>
|
|
||||||
where __S: _serde::ser::Serializer
|
|
||||||
{
|
|
||||||
self.value.__serde_serialize_with(serializer)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
__SerdeSerializeWithStruct {
|
|
||||||
value: &self.value,
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
@ -214,7 +214,7 @@ fn deserialize_item_struct(
|
|||||||
None,
|
None,
|
||||||
impl_generics,
|
impl_generics,
|
||||||
ty,
|
ty,
|
||||||
fields.len(),
|
fields,
|
||||||
container_attrs,
|
container_attrs,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -358,7 +358,7 @@ fn deserialize_tuple(
|
|||||||
variant_ident: Option<Ident>,
|
variant_ident: Option<Ident>,
|
||||||
impl_generics: &ast::Generics,
|
impl_generics: &ast::Generics,
|
||||||
ty: P<ast::Ty>,
|
ty: P<ast::Ty>,
|
||||||
fields: usize,
|
fields: &[ast::StructField],
|
||||||
container_attrs: &attr::ContainerAttrs,
|
container_attrs: &attr::ContainerAttrs,
|
||||||
) -> Result<P<ast::Expr>, Error> {
|
) -> Result<P<ast::Expr>, Error> {
|
||||||
let where_clause = &impl_generics.where_clause;
|
let where_clause = &impl_generics.where_clause;
|
||||||
@ -376,37 +376,43 @@ fn deserialize_tuple(
|
|||||||
None => builder.path().id(type_ident).build(),
|
None => builder.path().id(type_ident).build(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let visit_newtype_struct = if !is_enum && fields == 1 {
|
let nfields = fields.len();
|
||||||
Some(quote_tokens!(cx,
|
let fields_with_attrs = try!(attr::fields_with_attrs(cx, fields));
|
||||||
#[inline]
|
|
||||||
fn visit_newtype_struct<__E>(&mut self, deserializer: &mut __E) -> ::std::result::Result<Self::Value, __E::Error>
|
let visit_newtype_struct = if !is_enum && nfields == 1 {
|
||||||
where __E: _serde::de::Deserializer,
|
Some(try!(deserialize_newtype_struct(
|
||||||
{
|
cx,
|
||||||
let value = try!(_serde::de::Deserialize::deserialize(deserializer));
|
builder,
|
||||||
Ok($type_path(value))
|
type_ident,
|
||||||
}))
|
&type_path,
|
||||||
|
impl_generics,
|
||||||
|
&fields_with_attrs[0],
|
||||||
|
)))
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
let visit_seq_expr = deserialize_seq(
|
let visit_seq_expr = try!(deserialize_seq(
|
||||||
cx,
|
cx,
|
||||||
builder,
|
builder,
|
||||||
|
type_ident,
|
||||||
type_path,
|
type_path,
|
||||||
fields,
|
impl_generics,
|
||||||
);
|
&fields_with_attrs,
|
||||||
|
false,
|
||||||
|
));
|
||||||
|
|
||||||
let dispatch = if is_enum {
|
let dispatch = if is_enum {
|
||||||
quote_expr!(cx,
|
quote_expr!(cx,
|
||||||
visitor.visit_tuple($fields, $visitor_expr))
|
visitor.visit_tuple($nfields, $visitor_expr))
|
||||||
} else if fields == 1 {
|
} else if nfields == 1 {
|
||||||
let type_name = container_attrs.name().deserialize_name_expr();
|
let type_name = container_attrs.name().deserialize_name_expr();
|
||||||
quote_expr!(cx,
|
quote_expr!(cx,
|
||||||
deserializer.deserialize_newtype_struct($type_name, $visitor_expr))
|
deserializer.deserialize_newtype_struct($type_name, $visitor_expr))
|
||||||
} else {
|
} else {
|
||||||
let type_name = container_attrs.name().deserialize_name_expr();
|
let type_name = container_attrs.name().deserialize_name_expr();
|
||||||
quote_expr!(cx,
|
quote_expr!(cx,
|
||||||
deserializer.deserialize_tuple_struct($type_name, $fields, $visitor_expr))
|
deserializer.deserialize_tuple_struct($type_name, $nfields, $visitor_expr))
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(quote_expr!(cx, {
|
Ok(quote_expr!(cx, {
|
||||||
@ -430,46 +436,13 @@ fn deserialize_tuple(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_seq(
|
fn deserialize_seq(
|
||||||
cx: &ExtCtxt,
|
|
||||||
builder: &aster::AstBuilder,
|
|
||||||
struct_path: ast::Path,
|
|
||||||
fields: usize,
|
|
||||||
) -> P<ast::Expr> {
|
|
||||||
let let_values: Vec<ast::Stmt> = (0 .. fields)
|
|
||||||
.map(|i| {
|
|
||||||
let name = builder.id(format!("__field{}", i));
|
|
||||||
quote_stmt!(cx,
|
|
||||||
let $name = match try!(visitor.visit()) {
|
|
||||||
Some(value) => { value },
|
|
||||||
None => {
|
|
||||||
return Err(_serde::de::Error::end_of_stream());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
).unwrap()
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
let result = builder.expr().call()
|
|
||||||
.build_path(struct_path)
|
|
||||||
.with_args((0 .. fields).map(|i| builder.expr().id(format!("__field{}", i))))
|
|
||||||
.build();
|
|
||||||
|
|
||||||
quote_expr!(cx, {
|
|
||||||
$let_values
|
|
||||||
|
|
||||||
try!(visitor.end());
|
|
||||||
|
|
||||||
Ok($result)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn deserialize_struct_as_seq(
|
|
||||||
cx: &ExtCtxt,
|
cx: &ExtCtxt,
|
||||||
builder: &aster::AstBuilder,
|
builder: &aster::AstBuilder,
|
||||||
type_ident: Ident,
|
type_ident: Ident,
|
||||||
struct_path: ast::Path,
|
type_path: ast::Path,
|
||||||
impl_generics: &ast::Generics,
|
impl_generics: &ast::Generics,
|
||||||
fields: &[(&ast::StructField, attr::FieldAttrs)],
|
fields: &[(&ast::StructField, attr::FieldAttrs)],
|
||||||
|
is_struct: bool,
|
||||||
) -> Result<P<ast::Expr>, Error> {
|
) -> Result<P<ast::Expr>, Error> {
|
||||||
let let_values: Vec<_> = fields.iter()
|
let let_values: Vec<_> = fields.iter()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
@ -508,23 +481,30 @@ fn deserialize_struct_as_seq(
|
|||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let result = builder.expr().struct_path(struct_path)
|
let result = if is_struct {
|
||||||
.with_id_exprs(
|
builder.expr().struct_path(type_path)
|
||||||
fields.iter()
|
.with_id_exprs(
|
||||||
.enumerate()
|
fields.iter()
|
||||||
.map(|(i, &(field, _))| {
|
.enumerate()
|
||||||
(
|
.map(|(i, &(field, _))| {
|
||||||
match field.ident {
|
(
|
||||||
Some(name) => name.clone(),
|
match field.ident {
|
||||||
None => {
|
Some(name) => name.clone(),
|
||||||
cx.span_bug(field.span, "struct contains unnamed fields")
|
None => {
|
||||||
}
|
cx.span_bug(field.span, "struct contains unnamed fields")
|
||||||
},
|
}
|
||||||
builder.expr().id(format!("__field{}", i)),
|
},
|
||||||
)
|
builder.expr().id(format!("__field{}", i)),
|
||||||
})
|
)
|
||||||
)
|
})
|
||||||
.build();
|
)
|
||||||
|
.build()
|
||||||
|
} else {
|
||||||
|
builder.expr().call()
|
||||||
|
.build_path(type_path)
|
||||||
|
.with_args((0..fields.len()).map(|i| builder.expr().id(format!("__field{}", i))))
|
||||||
|
.build()
|
||||||
|
};
|
||||||
|
|
||||||
Ok(quote_expr!(cx, {
|
Ok(quote_expr!(cx, {
|
||||||
$let_values
|
$let_values
|
||||||
@ -535,6 +515,41 @@ fn deserialize_struct_as_seq(
|
|||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn deserialize_newtype_struct(
|
||||||
|
cx: &ExtCtxt,
|
||||||
|
builder: &aster::AstBuilder,
|
||||||
|
type_ident: Ident,
|
||||||
|
type_path: &ast::Path,
|
||||||
|
impl_generics: &ast::Generics,
|
||||||
|
field: &(&ast::StructField, attr::FieldAttrs),
|
||||||
|
) -> Result<Vec<ast::TokenTree>, Error> {
|
||||||
|
let &(field, ref attrs) = field;
|
||||||
|
let value = match attrs.deserialize_with() {
|
||||||
|
None => {
|
||||||
|
let field_ty = &field.ty;
|
||||||
|
quote_expr!(cx,
|
||||||
|
try!(<$field_ty as _serde::Deserialize>::deserialize(__e)))
|
||||||
|
}
|
||||||
|
Some(path) => {
|
||||||
|
let (wrapper, wrapper_impl, wrapper_ty) = wrap_deserialize_with(
|
||||||
|
cx, builder, type_ident, impl_generics, &field.ty, path);
|
||||||
|
quote_expr!(cx, {
|
||||||
|
$wrapper
|
||||||
|
$wrapper_impl
|
||||||
|
try!(<$wrapper_ty as _serde::Deserialize>::deserialize(__e)).value
|
||||||
|
})
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Ok(quote_tokens!(cx,
|
||||||
|
#[inline]
|
||||||
|
fn visit_newtype_struct<__E>(&mut self, __e: &mut __E) -> ::std::result::Result<Self::Value, __E::Error>
|
||||||
|
where __E: _serde::de::Deserializer,
|
||||||
|
{
|
||||||
|
Ok($type_path($value))
|
||||||
|
}
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
fn deserialize_struct(
|
fn deserialize_struct(
|
||||||
cx: &ExtCtxt,
|
cx: &ExtCtxt,
|
||||||
builder: &aster::AstBuilder,
|
builder: &aster::AstBuilder,
|
||||||
@ -559,16 +574,16 @@ fn deserialize_struct(
|
|||||||
None => builder.path().id(type_ident).build(),
|
None => builder.path().id(type_ident).build(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let is_enum = variant_ident.is_some();
|
let fields_with_attrs = try!(attr::fields_with_attrs(cx, fields));
|
||||||
let fields_with_attrs = try!(fields_with_attrs(cx, impl_generics, &ty, fields, is_enum));
|
|
||||||
|
|
||||||
let visit_seq_expr = try!(deserialize_struct_as_seq(
|
let visit_seq_expr = try!(deserialize_seq(
|
||||||
cx,
|
cx,
|
||||||
builder,
|
builder,
|
||||||
type_ident,
|
type_ident,
|
||||||
type_path.clone(),
|
type_path.clone(),
|
||||||
impl_generics,
|
impl_generics,
|
||||||
&fields_with_attrs,
|
&fields_with_attrs,
|
||||||
|
true,
|
||||||
));
|
));
|
||||||
|
|
||||||
let (field_visitor, fields_stmt, visit_map_expr) = try!(deserialize_struct_visitor(
|
let (field_visitor, fields_stmt, visit_map_expr) = try!(deserialize_struct_visitor(
|
||||||
@ -581,6 +596,7 @@ fn deserialize_struct(
|
|||||||
container_attrs,
|
container_attrs,
|
||||||
));
|
));
|
||||||
|
|
||||||
|
let is_enum = variant_ident.is_some();
|
||||||
let dispatch = if is_enum {
|
let dispatch = if is_enum {
|
||||||
quote_expr!(cx,
|
quote_expr!(cx,
|
||||||
visitor.visit_struct(FIELDS, $visitor_expr))
|
visitor.visit_struct(FIELDS, $visitor_expr))
|
||||||
@ -737,10 +753,14 @@ fn deserialize_variant(
|
|||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
ast::VariantData::Tuple(ref fields, _) if fields.len() == 1 => {
|
ast::VariantData::Tuple(ref fields, _) if fields.len() == 1 => {
|
||||||
Ok(quote_expr!(cx, {
|
deserialize_newtype_variant(
|
||||||
let val = try!(visitor.visit_newtype());
|
cx,
|
||||||
Ok($type_ident::$variant_ident(val))
|
builder,
|
||||||
}))
|
type_ident,
|
||||||
|
variant_ident,
|
||||||
|
generics,
|
||||||
|
&fields[0],
|
||||||
|
)
|
||||||
}
|
}
|
||||||
ast::VariantData::Tuple(ref fields, _) => {
|
ast::VariantData::Tuple(ref fields, _) => {
|
||||||
deserialize_tuple(
|
deserialize_tuple(
|
||||||
@ -750,7 +770,7 @@ fn deserialize_variant(
|
|||||||
Some(variant_ident),
|
Some(variant_ident),
|
||||||
generics,
|
generics,
|
||||||
ty,
|
ty,
|
||||||
fields.len(),
|
fields,
|
||||||
container_attrs,
|
container_attrs,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -769,6 +789,33 @@ fn deserialize_variant(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn deserialize_newtype_variant(
|
||||||
|
cx: &ExtCtxt,
|
||||||
|
builder: &aster::AstBuilder,
|
||||||
|
type_ident: Ident,
|
||||||
|
variant_ident: Ident,
|
||||||
|
impl_generics: &ast::Generics,
|
||||||
|
field: &ast::StructField,
|
||||||
|
) -> Result<P<ast::Expr>, Error> {
|
||||||
|
let attrs = try!(attr::FieldAttrs::from_field(cx, 0, field));
|
||||||
|
let visit = match attrs.deserialize_with() {
|
||||||
|
None => {
|
||||||
|
let field_ty = &field.ty;
|
||||||
|
quote_expr!(cx, try!(visitor.visit_newtype::<$field_ty>()))
|
||||||
|
}
|
||||||
|
Some(path) => {
|
||||||
|
let (wrapper, wrapper_impl, wrapper_ty) = wrap_deserialize_with(
|
||||||
|
cx, builder, type_ident, impl_generics, &field.ty, path);
|
||||||
|
quote_expr!(cx, {
|
||||||
|
$wrapper
|
||||||
|
$wrapper_impl
|
||||||
|
try!(visitor.visit_newtype::<$wrapper_ty>()).value
|
||||||
|
})
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Ok(quote_expr!(cx, Ok($type_ident::$variant_ident($visit))))
|
||||||
|
}
|
||||||
|
|
||||||
fn deserialize_field_visitor(
|
fn deserialize_field_visitor(
|
||||||
cx: &ExtCtxt,
|
cx: &ExtCtxt,
|
||||||
builder: &aster::AstBuilder,
|
builder: &aster::AstBuilder,
|
||||||
@ -1117,21 +1164,6 @@ fn deserialize_map(
|
|||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fields_with_attrs<'a>(
|
|
||||||
cx: &ExtCtxt,
|
|
||||||
generics: &ast::Generics,
|
|
||||||
ty: &P<ast::Ty>,
|
|
||||||
fields: &'a [ast::StructField],
|
|
||||||
is_enum: bool
|
|
||||||
) -> Result<Vec<(&'a ast::StructField, attr::FieldAttrs)>, Error> {
|
|
||||||
fields.iter()
|
|
||||||
.map(|field| {
|
|
||||||
let attrs = try!(attr::FieldAttrs::from_field(cx, &ty, generics, field, is_enum));
|
|
||||||
Ok((field, attrs))
|
|
||||||
})
|
|
||||||
.collect()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This function wraps the expression in `#[serde(deserialize_with="...")]` in
|
/// This function wraps the expression in `#[serde(deserialize_with="...")]` in
|
||||||
/// a trait to prevent it from accessing the internal `Deserialize` state.
|
/// a trait to prevent it from accessing the internal `Deserialize` state.
|
||||||
fn wrap_deserialize_with(
|
fn wrap_deserialize_with(
|
||||||
|
@ -185,6 +185,10 @@ fn serialize_item_struct(
|
|||||||
ast::VariantData::Tuple(ref fields, _) if fields.len() == 1 => {
|
ast::VariantData::Tuple(ref fields, _) if fields.len() == 1 => {
|
||||||
serialize_newtype_struct(
|
serialize_newtype_struct(
|
||||||
cx,
|
cx,
|
||||||
|
&builder,
|
||||||
|
impl_generics,
|
||||||
|
ty,
|
||||||
|
&fields[0],
|
||||||
container_attrs,
|
container_attrs,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -198,7 +202,7 @@ fn serialize_item_struct(
|
|||||||
&builder,
|
&builder,
|
||||||
impl_generics,
|
impl_generics,
|
||||||
ty,
|
ty,
|
||||||
fields.len(),
|
fields,
|
||||||
container_attrs,
|
container_attrs,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -232,12 +236,24 @@ fn serialize_unit_struct(
|
|||||||
|
|
||||||
fn serialize_newtype_struct(
|
fn serialize_newtype_struct(
|
||||||
cx: &ExtCtxt,
|
cx: &ExtCtxt,
|
||||||
|
builder: &aster::AstBuilder,
|
||||||
|
impl_generics: &ast::Generics,
|
||||||
|
container_ty: P<ast::Ty>,
|
||||||
|
field: &ast::StructField,
|
||||||
container_attrs: &attr::ContainerAttrs,
|
container_attrs: &attr::ContainerAttrs,
|
||||||
) -> Result<P<ast::Expr>, Error> {
|
) -> Result<P<ast::Expr>, Error> {
|
||||||
let type_name = container_attrs.name().serialize_name_expr();
|
let type_name = container_attrs.name().serialize_name_expr();
|
||||||
|
|
||||||
|
let attrs = try!(attr::FieldAttrs::from_field(cx, 0, field));
|
||||||
|
|
||||||
|
let mut field_expr = quote_expr!(cx, &self.0);
|
||||||
|
if let Some(path) = attrs.serialize_with() {
|
||||||
|
field_expr = wrap_serialize_with(cx, builder,
|
||||||
|
&container_ty, impl_generics, &field.ty, path, field_expr);
|
||||||
|
}
|
||||||
|
|
||||||
Ok(quote_expr!(cx,
|
Ok(quote_expr!(cx,
|
||||||
_serializer.serialize_newtype_struct($type_name, &self.0)
|
_serializer.serialize_newtype_struct($type_name, $field_expr)
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -246,10 +262,10 @@ fn serialize_tuple_struct(
|
|||||||
builder: &aster::AstBuilder,
|
builder: &aster::AstBuilder,
|
||||||
impl_generics: &ast::Generics,
|
impl_generics: &ast::Generics,
|
||||||
ty: P<ast::Ty>,
|
ty: P<ast::Ty>,
|
||||||
fields: usize,
|
fields: &[ast::StructField],
|
||||||
container_attrs: &attr::ContainerAttrs,
|
container_attrs: &attr::ContainerAttrs,
|
||||||
) -> Result<P<ast::Expr>, Error> {
|
) -> Result<P<ast::Expr>, Error> {
|
||||||
let (visitor_struct, visitor_impl) = serialize_tuple_struct_visitor(
|
let (visitor_struct, visitor_impl) = try!(serialize_tuple_struct_visitor(
|
||||||
cx,
|
cx,
|
||||||
builder,
|
builder,
|
||||||
ty.clone(),
|
ty.clone(),
|
||||||
@ -260,7 +276,8 @@ fn serialize_tuple_struct(
|
|||||||
builder.id("serialize_tuple_struct_elt"),
|
builder.id("serialize_tuple_struct_elt"),
|
||||||
fields,
|
fields,
|
||||||
impl_generics,
|
impl_generics,
|
||||||
);
|
false,
|
||||||
|
));
|
||||||
|
|
||||||
let type_name = container_attrs.name().serialize_name_expr();
|
let type_name = container_attrs.name().serialize_name_expr();
|
||||||
|
|
||||||
@ -362,12 +379,8 @@ fn serialize_variant(
|
|||||||
|
|
||||||
match variant.node.data {
|
match variant.node.data {
|
||||||
ast::VariantData::Unit(_) => {
|
ast::VariantData::Unit(_) => {
|
||||||
let pat = builder.pat().path()
|
|
||||||
.id(type_ident).id(variant_ident)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
Ok(quote_arm!(cx,
|
Ok(quote_arm!(cx,
|
||||||
$pat => {
|
$type_ident::$variant_ident => {
|
||||||
_serde::ser::Serializer::serialize_unit_variant(
|
_serde::ser::Serializer::serialize_unit_variant(
|
||||||
_serializer,
|
_serializer,
|
||||||
$type_name,
|
$type_name,
|
||||||
@ -378,23 +391,19 @@ fn serialize_variant(
|
|||||||
))
|
))
|
||||||
},
|
},
|
||||||
ast::VariantData::Tuple(ref fields, _) if fields.len() == 1 => {
|
ast::VariantData::Tuple(ref fields, _) if fields.len() == 1 => {
|
||||||
let field = builder.id("__simple_value");
|
let expr = try!(serialize_newtype_variant(
|
||||||
let field = builder.pat().ref_id(field);
|
cx,
|
||||||
let pat = builder.pat().enum_()
|
builder,
|
||||||
.id(type_ident).id(variant_ident).build()
|
type_name,
|
||||||
.with_pats(Some(field).into_iter())
|
variant_index,
|
||||||
.build();
|
variant_name,
|
||||||
|
ty,
|
||||||
|
generics,
|
||||||
|
&fields[0],
|
||||||
|
));
|
||||||
|
|
||||||
Ok(quote_arm!(cx,
|
Ok(quote_arm!(cx,
|
||||||
$pat => {
|
$type_ident::$variant_ident(ref __simple_value) => { $expr }
|
||||||
_serde::ser::Serializer::serialize_newtype_variant(
|
|
||||||
_serializer,
|
|
||||||
$type_name,
|
|
||||||
$variant_index,
|
|
||||||
$variant_name,
|
|
||||||
__simple_value,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
))
|
))
|
||||||
},
|
},
|
||||||
ast::VariantData::Tuple(ref fields, _) => {
|
ast::VariantData::Tuple(ref fields, _) => {
|
||||||
@ -410,7 +419,7 @@ fn serialize_variant(
|
|||||||
)
|
)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
let expr = serialize_tuple_variant(
|
let expr = try!(serialize_tuple_variant(
|
||||||
cx,
|
cx,
|
||||||
builder,
|
builder,
|
||||||
type_name,
|
type_name,
|
||||||
@ -420,7 +429,7 @@ fn serialize_variant(
|
|||||||
ty,
|
ty,
|
||||||
fields,
|
fields,
|
||||||
field_names,
|
field_names,
|
||||||
);
|
));
|
||||||
|
|
||||||
Ok(quote_arm!(cx,
|
Ok(quote_arm!(cx,
|
||||||
$pat => { $expr }
|
$pat => { $expr }
|
||||||
@ -468,6 +477,35 @@ fn serialize_variant(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn serialize_newtype_variant(
|
||||||
|
cx: &ExtCtxt,
|
||||||
|
builder: &aster::AstBuilder,
|
||||||
|
type_name: P<ast::Expr>,
|
||||||
|
variant_index: usize,
|
||||||
|
variant_name: P<ast::Expr>,
|
||||||
|
container_ty: P<ast::Ty>,
|
||||||
|
generics: &ast::Generics,
|
||||||
|
field: &ast::StructField,
|
||||||
|
) -> Result<P<ast::Expr>, Error> {
|
||||||
|
let attrs = try!(attr::FieldAttrs::from_field(cx, 0, field));
|
||||||
|
|
||||||
|
let mut field_expr = quote_expr!(cx, __simple_value);
|
||||||
|
if let Some(path) = attrs.serialize_with() {
|
||||||
|
field_expr = wrap_serialize_with(cx, builder,
|
||||||
|
&container_ty, generics, &field.ty, path, field_expr);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(quote_expr!(cx,
|
||||||
|
_serde::ser::Serializer::serialize_newtype_variant(
|
||||||
|
_serializer,
|
||||||
|
$type_name,
|
||||||
|
$variant_index,
|
||||||
|
$variant_name,
|
||||||
|
$field_expr,
|
||||||
|
)
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
fn serialize_tuple_variant(
|
fn serialize_tuple_variant(
|
||||||
cx: &ExtCtxt,
|
cx: &ExtCtxt,
|
||||||
builder: &aster::AstBuilder,
|
builder: &aster::AstBuilder,
|
||||||
@ -478,7 +516,7 @@ fn serialize_tuple_variant(
|
|||||||
structure_ty: P<ast::Ty>,
|
structure_ty: P<ast::Ty>,
|
||||||
fields: &[ast::StructField],
|
fields: &[ast::StructField],
|
||||||
field_names: Vec<Ident>,
|
field_names: Vec<Ident>,
|
||||||
) -> P<ast::Expr> {
|
) -> Result<P<ast::Expr>, Error> {
|
||||||
let variant_ty = builder.ty().tuple()
|
let variant_ty = builder.ty().tuple()
|
||||||
.with_tys(
|
.with_tys(
|
||||||
fields.iter().map(|field| {
|
fields.iter().map(|field| {
|
||||||
@ -490,15 +528,16 @@ fn serialize_tuple_variant(
|
|||||||
)
|
)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
let (visitor_struct, visitor_impl) = serialize_tuple_struct_visitor(
|
let (visitor_struct, visitor_impl) = try!(serialize_tuple_struct_visitor(
|
||||||
cx,
|
cx,
|
||||||
builder,
|
builder,
|
||||||
structure_ty.clone(),
|
structure_ty.clone(),
|
||||||
variant_ty,
|
variant_ty,
|
||||||
builder.id("serialize_tuple_variant_elt"),
|
builder.id("serialize_tuple_variant_elt"),
|
||||||
fields.len(),
|
fields,
|
||||||
generics,
|
generics,
|
||||||
);
|
true,
|
||||||
|
));
|
||||||
|
|
||||||
let value_expr = builder.expr().tuple()
|
let value_expr = builder.expr().tuple()
|
||||||
.with_exprs(
|
.with_exprs(
|
||||||
@ -508,7 +547,7 @@ fn serialize_tuple_variant(
|
|||||||
)
|
)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
quote_expr!(cx, {
|
Ok(quote_expr!(cx, {
|
||||||
$visitor_struct
|
$visitor_struct
|
||||||
$visitor_impl
|
$visitor_impl
|
||||||
_serializer.serialize_tuple_variant($type_name, $variant_index, $variant_name, Visitor {
|
_serializer.serialize_tuple_variant($type_name, $variant_index, $variant_name, Visitor {
|
||||||
@ -516,7 +555,7 @@ fn serialize_tuple_variant(
|
|||||||
state: 0,
|
state: 0,
|
||||||
_structure_ty: ::std::marker::PhantomData::<&$structure_ty>,
|
_structure_ty: ::std::marker::PhantomData::<&$structure_ty>,
|
||||||
})
|
})
|
||||||
})
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_struct_variant(
|
fn serialize_struct_variant(
|
||||||
@ -614,20 +653,33 @@ fn serialize_tuple_struct_visitor(
|
|||||||
structure_ty: P<ast::Ty>,
|
structure_ty: P<ast::Ty>,
|
||||||
variant_ty: P<ast::Ty>,
|
variant_ty: P<ast::Ty>,
|
||||||
serializer_method: ast::Ident,
|
serializer_method: ast::Ident,
|
||||||
fields: usize,
|
fields: &[ast::StructField],
|
||||||
generics: &ast::Generics
|
generics: &ast::Generics,
|
||||||
) -> (P<ast::Item>, P<ast::Item>) {
|
is_enum: bool,
|
||||||
let arms: Vec<ast::Arm> = (0 .. fields)
|
) -> Result<(P<ast::Item>, P<ast::Item>), Error> {
|
||||||
.map(|i| {
|
let fields_with_attrs = try!(attr::fields_with_attrs(cx, fields));
|
||||||
let expr = builder.expr().method_call(serializer_method)
|
|
||||||
.id("_serializer")
|
let arms: Vec<_> = fields_with_attrs.iter()
|
||||||
.arg().ref_().tup_field(i).field("value").self_()
|
.enumerate()
|
||||||
.build();
|
.map(|(i, &(field, ref attrs))| {
|
||||||
|
let mut field_expr = builder.expr().tup_field(i).field("value").self_();
|
||||||
|
if !is_enum {
|
||||||
|
field_expr = quote_expr!(cx, &$field_expr);
|
||||||
|
}
|
||||||
|
|
||||||
|
let continue_if_skip = attrs.skip_serializing_if()
|
||||||
|
.map(|path| quote_stmt!(cx, if $path($field_expr) { continue }));
|
||||||
|
|
||||||
|
if let Some(path) = attrs.serialize_with() {
|
||||||
|
field_expr = wrap_serialize_with(cx, builder,
|
||||||
|
&structure_ty, generics, &field.ty, path, field_expr);
|
||||||
|
}
|
||||||
|
|
||||||
quote_arm!(cx,
|
quote_arm!(cx,
|
||||||
$i => {
|
$i => {
|
||||||
self.state += 1;
|
self.state += 1;
|
||||||
Ok(Some(try!($expr)))
|
$continue_if_skip
|
||||||
|
Ok(Some(try!(_serializer.$serializer_method($field_expr))))
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
@ -644,7 +696,9 @@ fn serialize_tuple_struct_visitor(
|
|||||||
.strip_bounds()
|
.strip_bounds()
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
(
|
let nfields = fields.len();
|
||||||
|
|
||||||
|
Ok((
|
||||||
quote_item!(cx,
|
quote_item!(cx,
|
||||||
struct Visitor $visitor_impl_generics $where_clause {
|
struct Visitor $visitor_impl_generics $where_clause {
|
||||||
state: usize,
|
state: usize,
|
||||||
@ -669,11 +723,11 @@ fn serialize_tuple_struct_visitor(
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn len(&self) -> Option<usize> {
|
fn len(&self) -> Option<usize> {
|
||||||
Some($fields)
|
Some($nfields)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
).unwrap(),
|
).unwrap(),
|
||||||
)
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_struct_visitor(
|
fn serialize_struct_visitor(
|
||||||
@ -686,28 +740,27 @@ fn serialize_struct_visitor(
|
|||||||
generics: &ast::Generics,
|
generics: &ast::Generics,
|
||||||
is_enum: bool,
|
is_enum: bool,
|
||||||
) -> Result<(P<ast::Item>, P<ast::Item>), Error> {
|
) -> Result<(P<ast::Item>, P<ast::Item>), Error> {
|
||||||
let field_attrs = try!(
|
let fields_with_attrs = try!(attr::fields_with_attrs(cx, fields));
|
||||||
attr::get_struct_field_attrs(cx, &structure_ty, generics, fields, is_enum)
|
|
||||||
);
|
|
||||||
|
|
||||||
let arms: Vec<ast::Arm> = fields.iter().zip(field_attrs.iter())
|
let arms: Vec<ast::Arm> = fields_with_attrs.iter()
|
||||||
.filter(|&(_, ref field_attr)| !field_attr.skip_serializing_field())
|
.filter(|&&(_, ref attrs)| !attrs.skip_serializing_field())
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.map(|(i, (ref field, ref field_attr))| {
|
.map(|(i, &(field, ref attrs))| {
|
||||||
let name = field.ident.expect("struct has unnamed field");
|
let ident = field.ident.expect("struct has unnamed field");
|
||||||
|
let mut field_expr = quote_expr!(cx, self.value.$ident);
|
||||||
|
if !is_enum {
|
||||||
|
field_expr = quote_expr!(cx, &$field_expr);
|
||||||
|
}
|
||||||
|
|
||||||
let key_expr = field_attr.name().serialize_name_expr();
|
let key_expr = attrs.name().serialize_name_expr();
|
||||||
|
|
||||||
let stmt = if let Some(expr) = field_attr.skip_serializing_field_if() {
|
let continue_if_skip = attrs.skip_serializing_if()
|
||||||
Some(quote_stmt!(cx, if $expr { continue; }))
|
.map(|path| quote_stmt!(cx, if $path($field_expr) { continue }));
|
||||||
} else {
|
|
||||||
None
|
|
||||||
};
|
|
||||||
|
|
||||||
let field_expr = match field_attr.serialize_with() {
|
if let Some(path) = attrs.serialize_with() {
|
||||||
Some(expr) => expr.clone(),
|
field_expr = wrap_serialize_with(cx, builder,
|
||||||
None => quote_expr!(cx, &self.value.$name),
|
&structure_ty, generics, &field.ty, path, field_expr)
|
||||||
};
|
}
|
||||||
|
|
||||||
let expr = quote_expr!(cx,
|
let expr = quote_expr!(cx,
|
||||||
_serializer.$serializer_method($key_expr, $field_expr)
|
_serializer.$serializer_method($key_expr, $field_expr)
|
||||||
@ -716,7 +769,7 @@ fn serialize_struct_visitor(
|
|||||||
quote_arm!(cx,
|
quote_arm!(cx,
|
||||||
$i => {
|
$i => {
|
||||||
self.state += 1;
|
self.state += 1;
|
||||||
$stmt
|
$continue_if_skip
|
||||||
return Ok(Some(try!($expr)));
|
return Ok(Some(try!($expr)));
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@ -734,16 +787,18 @@ fn serialize_struct_visitor(
|
|||||||
.strip_bounds()
|
.strip_bounds()
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
let len = field_attrs.iter()
|
let len = fields_with_attrs.iter()
|
||||||
.filter(|field_attr| !field_attr.skip_serializing_field())
|
.filter(|&&(_, ref attrs)| !attrs.skip_serializing_field())
|
||||||
.map(|field_attr| {
|
.map(|&(field, ref attrs)| {
|
||||||
match field_attr.skip_serializing_field_if() {
|
let ident = field.ident.expect("struct has unnamed fields");
|
||||||
Some(expr) => {
|
let mut field_expr = quote_expr!(cx, self.value.$ident);
|
||||||
quote_expr!(cx, if $expr { 0 } else { 1 })
|
if !is_enum {
|
||||||
}
|
field_expr = quote_expr!(cx, &$field_expr);
|
||||||
None => {
|
}
|
||||||
quote_expr!(cx, 1)
|
|
||||||
}
|
match attrs.skip_serializing_if() {
|
||||||
|
Some(path) => quote_expr!(cx, if $path($field_expr) { 0 } else { 1 }),
|
||||||
|
None => quote_expr!(cx, 1),
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.fold(quote_expr!(cx, 0), |sum, expr| quote_expr!(cx, $sum + $expr));
|
.fold(quote_expr!(cx, 0), |sum, expr| quote_expr!(cx, $sum + $expr));
|
||||||
@ -782,3 +837,46 @@ fn serialize_struct_visitor(
|
|||||||
).unwrap(),
|
).unwrap(),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn wrap_serialize_with(
|
||||||
|
cx: &ExtCtxt,
|
||||||
|
builder: &aster::AstBuilder,
|
||||||
|
container_ty: &P<ast::Ty>,
|
||||||
|
generics: &ast::Generics,
|
||||||
|
field_ty: &P<ast::Ty>,
|
||||||
|
path: &ast::Path,
|
||||||
|
value: P<ast::Expr>,
|
||||||
|
) -> P<ast::Expr> {
|
||||||
|
let where_clause = &generics.where_clause;
|
||||||
|
|
||||||
|
let wrapper_generics = builder.from_generics(generics.clone())
|
||||||
|
.add_lifetime_bound("'__a")
|
||||||
|
.lifetime_name("'__a")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
let wrapper_ty = builder.path()
|
||||||
|
.segment("__SerializeWith")
|
||||||
|
.with_generics(wrapper_generics.clone())
|
||||||
|
.build()
|
||||||
|
.build();
|
||||||
|
|
||||||
|
quote_expr!(cx, {
|
||||||
|
struct __SerializeWith $wrapper_generics $where_clause {
|
||||||
|
value: &'__a $field_ty,
|
||||||
|
phantom: ::std::marker::PhantomData<$container_ty>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl $wrapper_generics _serde::ser::Serialize for $wrapper_ty $where_clause {
|
||||||
|
fn serialize<__S>(&self, __s: &mut __S) -> Result<(), __S::Error>
|
||||||
|
where __S: _serde::ser::Serializer
|
||||||
|
{
|
||||||
|
$path(self.value, __s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
__SerializeWith {
|
||||||
|
value: $value,
|
||||||
|
phantom: ::std::marker::PhantomData::<$container_ty>,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
// These just test that serde_codegen is able to produce code that compiles
|
// These just test that serde_codegen is able to produce code that compiles
|
||||||
// successfully when there are a variety of generics involved.
|
// successfully when there are a variety of generics and non-(de)serializable
|
||||||
|
// types involved.
|
||||||
|
|
||||||
extern crate serde;
|
extern crate serde;
|
||||||
use self::serde::ser::{Serialize, Serializer};
|
use self::serde::ser::{Serialize, Serializer};
|
||||||
@ -10,16 +11,16 @@ use self::serde::de::{Deserialize, Deserializer};
|
|||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
struct With<T> {
|
struct With<T> {
|
||||||
t: T,
|
t: T,
|
||||||
#[serde(serialize_with="ser_i32", deserialize_with="de_i32")]
|
#[serde(serialize_with="ser_x", deserialize_with="de_x")]
|
||||||
i: i32,
|
x: X,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
struct WithRef<'a, T: 'a> {
|
struct WithRef<'a, T: 'a> {
|
||||||
#[serde(skip_deserializing)]
|
#[serde(skip_deserializing)]
|
||||||
t: Option<&'a T>,
|
t: Option<&'a T>,
|
||||||
#[serde(serialize_with="ser_i32", deserialize_with="de_i32")]
|
#[serde(serialize_with="ser_x", deserialize_with="de_x")]
|
||||||
i: i32,
|
x: X,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
@ -40,13 +41,18 @@ struct NoBounds<T> {
|
|||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
enum EnumWith<T> {
|
enum EnumWith<T> {
|
||||||
A(
|
Unit,
|
||||||
#[serde(serialize_with="ser_i32", deserialize_with="de_i32")]
|
Newtype(
|
||||||
i32),
|
#[serde(serialize_with="ser_x", deserialize_with="de_x")]
|
||||||
B {
|
X),
|
||||||
|
Tuple(
|
||||||
|
T,
|
||||||
|
#[serde(serialize_with="ser_x", deserialize_with="de_x")]
|
||||||
|
X),
|
||||||
|
Struct {
|
||||||
t: T,
|
t: T,
|
||||||
#[serde(serialize_with="ser_i32", deserialize_with="de_i32")]
|
#[serde(serialize_with="ser_x", deserialize_with="de_x")]
|
||||||
i: i32 },
|
x: X },
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
@ -55,8 +61,23 @@ struct MultipleRef<'a, 'b, 'c, T> where T: 'c, 'c: 'b, 'b: 'a {
|
|||||||
rrrt: &'a &'b &'c T,
|
rrrt: &'a &'b &'c T,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
struct Newtype(
|
||||||
|
#[serde(serialize_with="ser_x", deserialize_with="de_x")]
|
||||||
|
X
|
||||||
|
);
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
struct Tuple<T>(
|
||||||
|
T,
|
||||||
|
#[serde(serialize_with="ser_x", deserialize_with="de_x")]
|
||||||
|
X,
|
||||||
|
);
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
fn ser_i32<S: Serializer>(_: &i32, _: &mut S) -> Result<(), S::Error> { panic!() }
|
// Implements neither Serialize nor Deserialize
|
||||||
|
struct X;
|
||||||
|
fn ser_x<S: Serializer>(_: &X, _: &mut S) -> Result<(), S::Error> { panic!() }
|
||||||
|
fn de_x<D: Deserializer>(_: &mut D) -> Result<X, D::Error> { panic!() }
|
||||||
|
|
||||||
fn de_i32<D: Deserializer>(_: &mut D) -> Result<i32, D::Error> { panic!() }
|
|
||||||
|
Loading…
Reference in New Issue
Block a user