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

Support #[derive(Serialize, Deserialize)] when using associated types #1213

Merged
merged 6 commits into from Apr 13, 2018

Conversation

Osspial
Copy link
Contributor

@Osspial Osspial commented Apr 10, 2018

Prior to this, structs and enums that had fields using associated types wouldn't have the appropriate where bounds set. This fixes that, meaning that this now builds:

trait Trait {
    type Assoc;
}

#[derive(Serialize)]
struct Struct<T: Trait> {
    assoc: T::Assoc,
}

Fixes #1208.

.flat_map(|field| {
let field_ty = field.ty;
let field_bound: Option<syn::WherePredicate> = match (field_ty, gen_bound_where(&field.attrs)) {
(&syn::Type::Path(ref ty_path), true) => match (ty_path.path.segments.first(), generics.params.first()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make this work even if the associated type is not from the first generic parameter.

#[derive(Serialize, Deserialize)]
struct AssocDerive<S, T: AssocSerde> {
    s: S,
    assoc: T::Assoc,
}

) -> syn::Generics
where
F: Fn(&attr::Field) -> Option<&[syn::WherePredicate]>,
W: for<'a> Fn(&attr::Field) -> bool,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 'a lifetime parameter is never used.

@dtolnay
Copy link
Member

dtolnay commented Apr 12, 2018

Thanks btw! This is going to fix a quite common stumbling block.

@Osspial
Copy link
Contributor Author

Osspial commented Apr 12, 2018

Those errors should be fixed now.

Copy link
Member

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Thank you.

@dtolnay dtolnay merged commit a6e94e7 into serde-rs:master Apr 13, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Infer correct trait bounds for use of a type parameter associated type
2 participants