Improve handling of trait bounds when deriving fmap-like traits

This commit is contained in:
Anthony Ramine 2017-08-26 12:40:28 +02:00
parent 8101887d31
commit efc852f6e3
12 changed files with 229 additions and 136 deletions

View file

@ -3,26 +3,26 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use cg;
use quote;
use syn;
use quote::Tokens;
use syn::DeriveInput;
use synstructure::BindStyle;
pub fn derive(input: syn::DeriveInput) -> quote::Tokens {
pub fn derive(input: DeriveInput) -> Tokens {
let name = &input.ident;
let trait_path = &["values", "computed", "ToComputedValue"];
let (impl_generics, ty_generics, mut where_clause, computed_value_type) =
cg::fmap_trait_parts(&input, trait_path, "ComputedValue");
for param in &input.generics.ty_params {
where_clause.add_trait_bound(
syn::Ty::Path(None, param.ident.clone().into()),
);
}
let to_body = cg::fmap_match(&input, BindStyle::Ref, |binding| {
quote!(::values::computed::ToComputedValue::to_computed_value(#binding, context))
where_clause.add_trait_bound(&binding.field.ty);
quote! {
::values::computed::ToComputedValue::to_computed_value(#binding, context)
}
});
let from_body = cg::fmap_match(&input, BindStyle::Ref, |binding| {
quote!(::values::computed::ToComputedValue::from_computed_value(#binding))
quote! {
::values::computed::ToComputedValue::from_computed_value(#binding)
}
});
quote! {