mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-04-29 10:37:55 +00:00
Change serde_path to crate
Also changed the generated code to have at least one thing refer to the path directly, rather than via `use` -- This shows that the impl *can* work without `use`, but doesn't actually do all the work to remove the `use` lines unless we decide we need this feature to work on the 2015 edition
This commit is contained in:
@@ -60,7 +60,7 @@ pub fn expand_derive_deserialize(input: &syn::DeriveInput) -> Result<TokenStream
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(dummy::wrap_in_const(cont.attrs.serde_path(), "DESERIALIZE", ident, impl_block))
|
Ok(dummy::wrap_in_const(cont.attrs.custom_serde_path(), "DESERIALIZE", ident, impl_block))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn precondition(cx: &Ctxt, cont: &Container) {
|
fn precondition(cx: &Ctxt, cont: &Container) {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
use internals::Ctxt;
|
use internals::Ctxt;
|
||||||
use proc_macro2::{Group, Span, TokenStream, TokenTree};
|
use proc_macro2::{Group, Span, TokenStream, TokenTree};
|
||||||
use quote::ToTokens;
|
use quote::ToTokens;
|
||||||
|
use std::borrow::Cow;
|
||||||
use std::collections::BTreeSet;
|
use std::collections::BTreeSet;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use syn;
|
use syn;
|
||||||
@@ -299,7 +300,7 @@ impl Container {
|
|||||||
let mut remote = Attr::none(cx, "remote");
|
let mut remote = Attr::none(cx, "remote");
|
||||||
let mut field_identifier = BoolAttr::none(cx, "field_identifier");
|
let mut field_identifier = BoolAttr::none(cx, "field_identifier");
|
||||||
let mut variant_identifier = BoolAttr::none(cx, "variant_identifier");
|
let mut variant_identifier = BoolAttr::none(cx, "variant_identifier");
|
||||||
let mut serde_path = Attr::none(cx, "serde_path");
|
let mut serde_path = Attr::none(cx, "crate");
|
||||||
|
|
||||||
for meta_items in item.attrs.iter().filter_map(get_serde_meta_items) {
|
for meta_items in item.attrs.iter().filter_map(get_serde_meta_items) {
|
||||||
for meta_item in meta_items {
|
for meta_item in meta_items {
|
||||||
@@ -584,8 +585,8 @@ impl Container {
|
|||||||
variant_identifier.set_true(word);
|
variant_identifier.set_true(word);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse `#[serde(serde_path = "foo")]`
|
// Parse `#[serde(crate = "foo")]`
|
||||||
Meta(NameValue(ref m)) if m.ident == "serde_path" => {
|
Meta(NameValue(ref m)) if m.ident == "crate" => {
|
||||||
if let Ok(path) = parse_lit_into_path(cx, &m.ident, &m.lit) {
|
if let Ok(path) = parse_lit_into_path(cx, &m.ident, &m.lit) {
|
||||||
serde_path.set(&m.ident, path)
|
serde_path.set(&m.ident, path)
|
||||||
}
|
}
|
||||||
@@ -682,9 +683,15 @@ impl Container {
|
|||||||
self.has_flatten = true;
|
self.has_flatten = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn serde_path(&self) -> Option<&syn::Path> {
|
pub fn custom_serde_path(&self) -> Option<&syn::Path> {
|
||||||
self.serde_path.as_ref()
|
self.serde_path.as_ref()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn serde_path<'a>(&'a self) -> Cow<'a, syn::Path> {
|
||||||
|
self.custom_serde_path()
|
||||||
|
.map(Cow::Borrowed)
|
||||||
|
.unwrap_or_else(|| Cow::Owned(parse_quote!(_serde)))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn decide_tag(
|
fn decide_tag(
|
||||||
|
|||||||
@@ -22,15 +22,16 @@ pub fn expand_derive_serialize(input: &syn::DeriveInput) -> Result<TokenStream,
|
|||||||
let params = Parameters::new(&cont);
|
let params = Parameters::new(&cont);
|
||||||
let (impl_generics, ty_generics, where_clause) = params.generics.split_for_impl();
|
let (impl_generics, ty_generics, where_clause) = params.generics.split_for_impl();
|
||||||
let body = Stmts(serialize_body(&cont, ¶ms));
|
let body = Stmts(serialize_body(&cont, ¶ms));
|
||||||
|
let serde = cont.attrs.serde_path();
|
||||||
|
|
||||||
let impl_block = if let Some(remote) = cont.attrs.remote() {
|
let impl_block = if let Some(remote) = cont.attrs.remote() {
|
||||||
let vis = &input.vis;
|
let vis = &input.vis;
|
||||||
let used = pretend::pretend_used(&cont);
|
let used = pretend::pretend_used(&cont);
|
||||||
quote! {
|
quote! {
|
||||||
impl #impl_generics #ident #ty_generics #where_clause {
|
impl #impl_generics #ident #ty_generics #where_clause {
|
||||||
#vis fn serialize<__S>(__self: &#remote #ty_generics, __serializer: __S) -> _serde::export::Result<__S::Ok, __S::Error>
|
#vis fn serialize<__S>(__self: &#remote #ty_generics, __serializer: __S) -> #serde::export::Result<__S::Ok, __S::Error>
|
||||||
where
|
where
|
||||||
__S: _serde::Serializer,
|
__S: #serde::Serializer,
|
||||||
{
|
{
|
||||||
#used
|
#used
|
||||||
#body
|
#body
|
||||||
@@ -40,10 +41,10 @@ pub fn expand_derive_serialize(input: &syn::DeriveInput) -> Result<TokenStream,
|
|||||||
} else {
|
} else {
|
||||||
quote! {
|
quote! {
|
||||||
#[automatically_derived]
|
#[automatically_derived]
|
||||||
impl #impl_generics _serde::Serialize for #ident #ty_generics #where_clause {
|
impl #impl_generics #serde::Serialize for #ident #ty_generics #where_clause {
|
||||||
fn serialize<__S>(&self, __serializer: __S) -> _serde::export::Result<__S::Ok, __S::Error>
|
fn serialize<__S>(&self, __serializer: __S) -> #serde::export::Result<__S::Ok, __S::Error>
|
||||||
where
|
where
|
||||||
__S: _serde::Serializer,
|
__S: #serde::Serializer,
|
||||||
{
|
{
|
||||||
#body
|
#body
|
||||||
}
|
}
|
||||||
@@ -51,7 +52,7 @@ pub fn expand_derive_serialize(input: &syn::DeriveInput) -> Result<TokenStream,
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(dummy::wrap_in_const(cont.attrs.serde_path(), "SERIALIZE", ident, impl_block))
|
Ok(dummy::wrap_in_const(cont.attrs.custom_serde_path(), "SERIALIZE", ident, impl_block))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn precondition(cx: &Ctxt, cont: &Container) {
|
fn precondition(cx: &Ctxt, cont: &Container) {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_gen_custom_serde() {
|
fn test_gen_custom_serde() {
|
||||||
#[derive(serde::Serialize, serde::Deserialize)]
|
#[derive(serde::Serialize, serde::Deserialize)]
|
||||||
#[serde(serde_path = "fake_serde")]
|
#[serde(crate = "fake_serde")]
|
||||||
struct Foo;
|
struct Foo;
|
||||||
|
|
||||||
// Would be overlapping if serde::Serialize were implemented
|
// Would be overlapping if serde::Serialize were implemented
|
||||||
|
|||||||
Reference in New Issue
Block a user