style: Share more code between ToAnimatedValue and ToComputedValue derive.

I'm going to add a ToResolvedValue, and I don't want to add more copy-pasta.

This shouldn't change behavior.

Differential Revision: https://phabricator.services.mozilla.com/D26289
This commit is contained in:
Emilio Cobos Álvarez 2019-04-09 09:37:26 +00:00
parent ae32e4df40
commit c3ab3f0963
3 changed files with 124 additions and 103 deletions

View file

@ -32,8 +32,8 @@ use synstructure::{self, BindStyle, BindingInfo, VariantAst, VariantInfo};
pub fn propagate_clauses_to_output_type( pub fn propagate_clauses_to_output_type(
where_clause: &mut Option<syn::WhereClause>, where_clause: &mut Option<syn::WhereClause>,
generics: &syn::Generics, generics: &syn::Generics,
trait_path: Path, trait_path: &Path,
trait_output: Ident, trait_output: &Ident,
) { ) {
let where_clause = match *where_clause { let where_clause = match *where_clause {
Some(ref mut clause) => clause, Some(ref mut clause) => clause,
@ -104,7 +104,7 @@ where
}) })
} }
pub fn fmap_trait_output(input: &DeriveInput, trait_path: &Path, trait_output: Ident) -> Path { pub fn fmap_trait_output(input: &DeriveInput, trait_path: &Path, trait_output: &Ident) -> Path {
let segment = PathSegment { let segment = PathSegment {
ident: input.ident.clone(), ident: input.ident.clone(),
arguments: PathArguments::AngleBracketed(AngleBracketedGenericArguments { arguments: PathArguments::AngleBracketed(AngleBracketedGenericArguments {

View file

@ -2,64 +2,42 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use derive_common::cg; use proc_macro2::TokenStream;
use proc_macro2::{Span, TokenStream}; use syn::DeriveInput;
use syn::{DeriveInput, Ident};
use synstructure::BindStyle; use synstructure::BindStyle;
use to_computed_value;
pub fn derive(mut input: DeriveInput) -> TokenStream { pub fn derive(input: DeriveInput) -> TokenStream {
let mut where_clause = input.generics.where_clause.take(); let trait_impl = |from_body, to_body| {
cg::propagate_clauses_to_output_type( quote! {
&mut where_clause,
&input.generics,
parse_quote!(crate::values::animated::ToAnimatedValue),
parse_quote!(AnimatedValue),
);
for param in input.generics.type_params() {
cg::add_predicate(
&mut where_clause,
parse_quote!(#param: crate::values::animated::ToAnimatedValue),
);
}
let to_body = cg::fmap_match(
&input,
BindStyle::Move,
|binding| quote!(crate::values::animated::ToAnimatedValue::to_animated_value(#binding)),
);
let from_body = cg::fmap_match(
&input,
BindStyle::Move,
|binding| quote!(crate::values::animated::ToAnimatedValue::from_animated_value(#binding)),
);
input.generics.where_clause = where_clause;
let name = &input.ident;
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
let animated_value_type = cg::fmap_trait_output(
&input,
&parse_quote!(crate::values::animated::ToAnimatedValue),
Ident::new("AnimatedValue", Span::call_site()),
);
quote! {
impl #impl_generics crate::values::animated::ToAnimatedValue for #name #ty_generics #where_clause {
type AnimatedValue = #animated_value_type;
#[allow(unused_variables)]
#[inline]
fn to_animated_value(self) -> Self::AnimatedValue {
match self {
#to_body
}
}
#[inline] #[inline]
fn from_animated_value(animated: Self::AnimatedValue) -> Self { fn from_animated_value(animated: Self::AnimatedValue) -> Self {
match animated { match animated {
#from_body #from_body
} }
} }
}
} #[inline]
fn to_animated_value(self) -> Self::AnimatedValue {
match self {
#to_body
}
}
}
};
// TODO(emilio): Consider optimizing away non-generic cases as well?
let non_generic_implementation = || None;
to_computed_value::derive_to_value(
input,
parse_quote!(crate::values::animated::ToAnimatedValue),
parse_quote!(AnimatedValue),
BindStyle::Move,
|_| false,
|binding| quote!(crate::values::animated::ToAnimatedValue::from_animated_value(#binding)),
|binding| quote!(crate::values::animated::ToAnimatedValue::to_animated_value(#binding)),
trait_impl,
non_generic_implementation,
)
} }

View file

@ -3,53 +3,70 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use derive_common::cg; use derive_common::cg;
use proc_macro2::{Span, TokenStream}; use proc_macro2::TokenStream;
use syn::{DeriveInput, Ident}; use syn::{DeriveInput, Ident, Path};
use synstructure::BindStyle; use synstructure::{BindStyle, BindingInfo};
pub fn derive(mut input: DeriveInput) -> TokenStream { pub fn derive_to_value(
mut input: DeriveInput,
trait_path: Path,
output_type_name: Ident,
bind_style: BindStyle,
// Returns whether to apply the field bound for a given item.
mut field_bound: impl FnMut(&BindingInfo) -> bool,
// Returns a token stream of the form: trait_path::from_foo(#binding)
mut call_from: impl FnMut(&BindingInfo) -> TokenStream,
mut call_to: impl FnMut(&BindingInfo) -> TokenStream,
// Returns a tokenstream of the form:
// fn from_function_syntax(foobar) -> Baz {
// #first_arg
// }
//
// fn to_function_syntax(foobar) -> Baz {
// #second_arg
// }
mut trait_impl: impl FnMut(TokenStream, TokenStream) -> TokenStream,
// if this is provided, the derive for non-generic types will be simplified
// to this token stream, which should be the body of the impl block.
non_generic_implementation: impl FnOnce() -> Option<TokenStream>,
) -> TokenStream {
let mut where_clause = input.generics.where_clause.take(); let mut where_clause = input.generics.where_clause.take();
cg::propagate_clauses_to_output_type( cg::propagate_clauses_to_output_type(
&mut where_clause, &mut where_clause,
&input.generics, &input.generics,
parse_quote!(crate::values::computed::ToComputedValue), &trait_path,
parse_quote!(ComputedValue), &output_type_name,
); );
let (to_body, from_body) = { let (to_body, from_body) = {
let params = input.generics.type_params().collect::<Vec<_>>(); let params = input.generics.type_params().collect::<Vec<_>>();
for param in &params { for param in &params {
cg::add_predicate( cg::add_predicate(
&mut where_clause, &mut where_clause,
parse_quote!(#param: crate::values::computed::ToComputedValue), parse_quote!(#param: #trait_path),
); );
} }
let to_body = cg::fmap_match(&input, BindStyle::Ref, |binding| { let to_body = cg::fmap_match(&input, bind_style, |binding| {
let attrs = cg::parse_field_attrs::<ComputedValueAttrs>(&binding.ast()); if field_bound(&binding) {
if attrs.field_bound {
let ty = &binding.ast().ty; let ty = &binding.ast().ty;
let output_type = cg::map_type_params( let output_type = cg::map_type_params(
ty, ty,
&params, &params,
&mut |ident| parse_quote!(<#ident as crate::values::computed::ToComputedValue>::ComputedValue), &mut |ident| parse_quote!(<#ident as #trait_path>::#output_type_name),
); );
cg::add_predicate( cg::add_predicate(
&mut where_clause, &mut where_clause,
parse_quote!( parse_quote!(
#ty: crate::values::computed::ToComputedValue<ComputedValue = #output_type> #ty: #trait_path<#output_type_name = #output_type>
), ),
); );
} }
quote! { call_to(&binding)
crate::values::computed::ToComputedValue::to_computed_value(#binding, context)
}
}); });
let from_body = cg::fmap_match(&input, BindStyle::Ref, |binding| { let from_body = cg::fmap_match(&input, bind_style, |binding| {
quote! { call_from(&binding)
crate::values::computed::ToComputedValue::from_computed_value(#binding)
}
}); });
(to_body, from_body) (to_body, from_body)
@ -58,39 +75,44 @@ pub fn derive(mut input: DeriveInput) -> TokenStream {
input.generics.where_clause = where_clause; input.generics.where_clause = where_clause;
let name = &input.ident; let name = &input.ident;
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl(); let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
if input.generics.type_params().next().is_none() { if input.generics.type_params().next().is_none() {
return quote! { if let Some(non_generic_implementation) = non_generic_implementation() {
impl #impl_generics crate::values::computed::ToComputedValue for #name #ty_generics return quote! {
#where_clause impl #impl_generics #trait_path for #name #ty_generics
{ #where_clause
type ComputedValue = Self; {
#non_generic_implementation
#[inline]
fn to_computed_value(
&self,
_context: &crate::values::computed::Context,
) -> Self::ComputedValue {
std::clone::Clone::clone(self)
} }
};
#[inline] }
fn from_computed_value(computed: &Self::ComputedValue) -> Self {
std::clone::Clone::clone(computed)
}
}
};
} }
let computed_value_type = cg::fmap_trait_output( let computed_value_type = cg::fmap_trait_output(
&input, &input,
&parse_quote!(crate::values::computed::ToComputedValue), &trait_path,
Ident::new("ComputedValue", Span::call_site()), &output_type_name,
); );
let impl_ = trait_impl(from_body, to_body);
quote! { quote! {
impl #impl_generics crate::values::computed::ToComputedValue for #name #ty_generics #where_clause { impl #impl_generics #trait_path for #name #ty_generics #where_clause {
type ComputedValue = #computed_value_type; type #output_type_name = #computed_value_type;
#impl_
}
}
}
pub fn derive(input: DeriveInput) -> TokenStream {
let trait_impl = |from_body, to_body| {
quote! {
#[inline]
fn from_computed_value(computed: &Self::ComputedValue) -> Self {
match *computed {
#from_body
}
}
#[allow(unused_variables)] #[allow(unused_variables)]
#[inline] #[inline]
@ -99,15 +121,36 @@ pub fn derive(mut input: DeriveInput) -> TokenStream {
#to_body #to_body
} }
} }
}
};
let non_generic_implementation = || {
Some(quote! {
type ComputedValue = Self;
#[inline]
fn to_computed_value(&self, _: &crate::values::computed::Context) -> Self::ComputedValue {
std::clone::Clone::clone(self)
}
#[inline] #[inline]
fn from_computed_value(computed: &Self::ComputedValue) -> Self { fn from_computed_value(computed: &Self::ComputedValue) -> Self {
match *computed { std::clone::Clone::clone(computed)
#from_body
}
} }
} })
} };
derive_to_value(
input,
parse_quote!(crate::values::computed::ToComputedValue),
parse_quote!(ComputedValue),
BindStyle::Ref,
|binding| cg::parse_field_attrs::<ComputedValueAttrs>(&binding.ast()).field_bound,
|binding| quote!(crate::values::computed::ToComputedValue::from_computed_value(#binding)),
|binding| quote!(crate::values::computed::ToComputedValue::to_computed_value(#binding, context)),
trait_impl,
non_generic_implementation,
)
} }
#[darling(attributes(compute), default)] #[darling(attributes(compute), default)]