Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Self does not work in a field with serialize_with #1969

Closed
dtolnay opened this issue Jan 25, 2021 · 0 comments · Fixed by #1970
Closed

Self does not work in a field with serialize_with #1969

dtolnay opened this issue Jan 25, 2021 · 0 comments · Fixed by #1970
Labels

Comments

@dtolnay
Copy link
Member

dtolnay commented Jan 25, 2021

Closely related to #1506, #1565, #1830.

use serde::{Serialize, Serializer};

#[derive(Serialize)]
struct Struct {
    #[serde(serialize_with = "vec_first_element")]
    vec: Vec<Self>,  // works if written as Vec<Struct>
}

fn vec_first_element<S>(vec: &Vec<Struct>, serializer: S) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    vec.first().serialize(serializer)
}
error[E0308]: mismatched types
 --> src/main.rs:3:10
  |
3 | #[derive(Serialize)]
  |          ^^^^^^^^^ expected struct `__SerializeWith`, found struct `Struct`
  |
  = note: expected reference `&Vec<__SerializeWith<'_>>`
             found reference `&Vec<Struct>`
  = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0308]: mismatched types
 --> src/main.rs:3:10
  |
3 | #[derive(Serialize)]
  |          ^^^^^^^^^ expected struct `Struct`, found struct `__SerializeWith`
  |
  = note: expected reference `&Vec<Struct>`
             found reference `&'__a Vec<__SerializeWith<'__a>>`
  = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging a pull request may close this issue.

1 participant