Skip to content

Commit 29cdf88

Browse files
authored
Merge pull request #1970 from serde-rs/self
Support `Self` inside fields that use serialize_with
2 parents b054ea4 + 2ba9739 commit 29cdf88

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

serde_derive/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ mod try;
7979

8080
#[proc_macro_derive(Serialize, attributes(serde))]
8181
pub fn derive_serialize(input: TokenStream) -> TokenStream {
82-
let input = parse_macro_input!(input as DeriveInput);
83-
ser::expand_derive_serialize(&input)
82+
let mut input = parse_macro_input!(input as DeriveInput);
83+
ser::expand_derive_serialize(&mut input)
8484
.unwrap_or_else(to_compile_errors)
8585
.into()
8686
}

serde_derive/src/ser.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@ use bound;
66
use dummy;
77
use fragment::{Fragment, Match, Stmts};
88
use internals::ast::{Container, Data, Field, Style, Variant};
9-
use internals::{attr, Ctxt, Derive};
9+
use internals::{attr, replace_receiver, Ctxt, Derive};
1010
use pretend;
1111

12-
pub fn expand_derive_serialize(input: &syn::DeriveInput) -> Result<TokenStream, Vec<syn::Error>> {
12+
pub fn expand_derive_serialize(
13+
input: &mut syn::DeriveInput,
14+
) -> Result<TokenStream, Vec<syn::Error>> {
15+
replace_receiver(input);
16+
1317
let ctxt = Ctxt::new();
1418
let cont = match Container::from_ast(&ctxt, input, Derive::Serialize) {
1519
Some(cont) => cont,

test_suite/tests/test_gen.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#![allow(
88
unknown_lints,
99
mixed_script_confusables,
10+
clippy::ptr_arg,
1011
clippy::trivially_copy_pass_by_ref
1112
)]
1213

@@ -735,6 +736,12 @@ fn test_gen() {
735736
#[serde(borrow = "'a")]
736737
f: mac!(Cow<'a, str>),
737738
}
739+
740+
#[derive(Serialize)]
741+
struct Struct {
742+
#[serde(serialize_with = "vec_first_element")]
743+
vec: Vec<Self>,
744+
}
738745
}
739746

740747
//////////////////////////////////////////////////////////////////////////
@@ -808,3 +815,11 @@ where
808815
pub fn is_zero(n: &u8) -> bool {
809816
*n == 0
810817
}
818+
819+
fn vec_first_element<T, S>(vec: &Vec<T>, serializer: S) -> StdResult<S::Ok, S::Error>
820+
where
821+
T: Serialize,
822+
S: Serializer,
823+
{
824+
vec.first().serialize(serializer)
825+
}

0 commit comments

Comments
 (0)