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

@ -2,64 +2,42 @@
* 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/. */
use derive_common::cg;
use proc_macro2::{Span, TokenStream};
use syn::{DeriveInput, Ident};
use proc_macro2::TokenStream;
use syn::DeriveInput;
use synstructure::BindStyle;
use to_computed_value;
pub fn derive(mut input: DeriveInput) -> TokenStream {
let mut where_clause = input.generics.where_clause.take();
cg::propagate_clauses_to_output_type(
&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
}
}
pub fn derive(input: DeriveInput) -> TokenStream {
let trait_impl = |from_body, to_body| {
quote! {
#[inline]
fn from_animated_value(animated: Self::AnimatedValue) -> Self {
match animated {
#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,
)
}