Merge pull request #1196 from serde-rs/self

Special case remote = "Self"
This commit is contained in:
David Tolnay
2018-03-27 11:12:04 +02:00
committed by GitHub
2 changed files with 20 additions and 9 deletions
+16 -9
View File
@@ -332,7 +332,11 @@ impl Container {
// Parse `#[serde(remote = "...")]` // Parse `#[serde(remote = "...")]`
Meta(NameValue(ref m)) if m.ident == "remote" => { Meta(NameValue(ref m)) if m.ident == "remote" => {
if let Ok(path) = parse_lit_into_path(cx, m.ident.as_ref(), &m.lit) { if let Ok(path) = parse_lit_into_path(cx, m.ident.as_ref(), &m.lit) {
remote.set(path); if is_primitive_path(&path, "Self") {
remote.set(item.ident.into());
} else {
remote.set(path);
}
} }
} }
@@ -1291,29 +1295,32 @@ fn is_rptr(ty: &syn::Type, elem: fn(&syn::Type) -> bool) -> bool {
} }
fn is_str(ty: &syn::Type) -> bool { fn is_str(ty: &syn::Type) -> bool {
is_primitive_path(ty, "str") is_primitive_type(ty, "str")
} }
fn is_slice_u8(ty: &syn::Type) -> bool { fn is_slice_u8(ty: &syn::Type) -> bool {
match *ty { match *ty {
syn::Type::Slice(ref ty) => is_primitive_path(&ty.elem, "u8"), syn::Type::Slice(ref ty) => is_primitive_type(&ty.elem, "u8"),
_ => false, _ => false,
} }
} }
fn is_primitive_path(ty: &syn::Type, primitive: &str) -> bool { fn is_primitive_type(ty: &syn::Type, primitive: &str) -> bool {
match *ty { match *ty {
syn::Type::Path(ref ty) => { syn::Type::Path(ref ty) => {
ty.qself.is_none() ty.qself.is_none() && is_primitive_path(&ty.path, primitive)
&& ty.path.leading_colon.is_none()
&& ty.path.segments.len() == 1
&& ty.path.segments[0].ident == primitive
&& ty.path.segments[0].arguments.is_empty()
} }
_ => false, _ => false,
} }
} }
fn is_primitive_path(path: &syn::Path, primitive: &str) -> bool {
path.leading_colon.is_none()
&& path.segments.len() == 1
&& path.segments[0].ident == primitive
&& path.segments[0].arguments.is_empty()
}
// All lifetimes that this type could borrow from a Deserializer. // All lifetimes that this type could borrow from a Deserializer.
// //
// For example a type `S<'a, 'b>` could borrow `'a` and `'b`. On the other hand // For example a type `S<'a, 'b>` could borrow `'a` and `'b`. On the other hand
+4
View File
@@ -385,6 +385,10 @@ fn test_gen() {
s: vis::S, s: vis::S,
} }
#[derive(Serialize, Deserialize)]
#[serde(remote = "Self")]
struct RemoteSelf;
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
enum ExternallyTaggedVariantWith { enum ExternallyTaggedVariantWith {
#[allow(dead_code)] #[allow(dead_code)]