Merge pull request #1970 from serde-rs/self
Support `Self` inside fields that use serialize_with
This commit is contained in:
commit
29cdf888c0
@ -79,8 +79,8 @@ mod try;
|
|||||||
|
|
||||||
#[proc_macro_derive(Serialize, attributes(serde))]
|
#[proc_macro_derive(Serialize, attributes(serde))]
|
||||||
pub fn derive_serialize(input: TokenStream) -> TokenStream {
|
pub fn derive_serialize(input: TokenStream) -> TokenStream {
|
||||||
let input = parse_macro_input!(input as DeriveInput);
|
let mut input = parse_macro_input!(input as DeriveInput);
|
||||||
ser::expand_derive_serialize(&input)
|
ser::expand_derive_serialize(&mut input)
|
||||||
.unwrap_or_else(to_compile_errors)
|
.unwrap_or_else(to_compile_errors)
|
||||||
.into()
|
.into()
|
||||||
}
|
}
|
||||||
|
@ -6,10 +6,14 @@ use bound;
|
|||||||
use dummy;
|
use dummy;
|
||||||
use fragment::{Fragment, Match, Stmts};
|
use fragment::{Fragment, Match, Stmts};
|
||||||
use internals::ast::{Container, Data, Field, Style, Variant};
|
use internals::ast::{Container, Data, Field, Style, Variant};
|
||||||
use internals::{attr, Ctxt, Derive};
|
use internals::{attr, replace_receiver, Ctxt, Derive};
|
||||||
use pretend;
|
use pretend;
|
||||||
|
|
||||||
pub fn expand_derive_serialize(input: &syn::DeriveInput) -> Result<TokenStream, Vec<syn::Error>> {
|
pub fn expand_derive_serialize(
|
||||||
|
input: &mut syn::DeriveInput,
|
||||||
|
) -> Result<TokenStream, Vec<syn::Error>> {
|
||||||
|
replace_receiver(input);
|
||||||
|
|
||||||
let ctxt = Ctxt::new();
|
let ctxt = Ctxt::new();
|
||||||
let cont = match Container::from_ast(&ctxt, input, Derive::Serialize) {
|
let cont = match Container::from_ast(&ctxt, input, Derive::Serialize) {
|
||||||
Some(cont) => cont,
|
Some(cont) => cont,
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
#![allow(
|
#![allow(
|
||||||
unknown_lints,
|
unknown_lints,
|
||||||
mixed_script_confusables,
|
mixed_script_confusables,
|
||||||
|
clippy::ptr_arg,
|
||||||
clippy::trivially_copy_pass_by_ref
|
clippy::trivially_copy_pass_by_ref
|
||||||
)]
|
)]
|
||||||
|
|
||||||
@ -735,6 +736,12 @@ fn test_gen() {
|
|||||||
#[serde(borrow = "'a")]
|
#[serde(borrow = "'a")]
|
||||||
f: mac!(Cow<'a, str>),
|
f: mac!(Cow<'a, str>),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
struct Struct {
|
||||||
|
#[serde(serialize_with = "vec_first_element")]
|
||||||
|
vec: Vec<Self>,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
@ -808,3 +815,11 @@ where
|
|||||||
pub fn is_zero(n: &u8) -> bool {
|
pub fn is_zero(n: &u8) -> bool {
|
||||||
*n == 0
|
*n == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn vec_first_element<T, S>(vec: &Vec<T>, serializer: S) -> StdResult<S::Ok, S::Error>
|
||||||
|
where
|
||||||
|
T: Serialize,
|
||||||
|
S: Serializer,
|
||||||
|
{
|
||||||
|
vec.first().serialize(serializer)
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user