Skip to content

Commit

Permalink
style: Part 1: Support field_bound on Animate.
Browse files Browse the repository at this point in the history
So we can derive Animate on more generic types.

Differential Revision: https://phabricator.services.mozilla.com/D16339
  • Loading branch information
BorisChiou authored and emilio committed Jan 13, 2019
1 parent 7efbd9c commit a8943d2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
3 changes: 2 additions & 1 deletion components/style/values/animated/mod.rs
Expand Up @@ -113,7 +113,8 @@ pub fn animate_multiplicative_factor(
/// function has been specified through `#[animate(fallback)]`.
///
/// Trait bounds for type parameter `Foo` can be opted out of with
/// `#[animation(no_bound(Foo))]` on the type definition.
/// `#[animation(no_bound(Foo))]` on the type definition, trait bounds for
/// fields can be opted into with `#[animation(field_bound)]` on the field.
pub trait Animate: Sized {
/// Animate a value towards another one, given an animation procedure.
fn animate(&self, other: &Self, procedure: Procedure) -> Result<Self, ()>;
Expand Down
41 changes: 26 additions & 15 deletions components/style_derive/animate.rs
Expand Up @@ -6,7 +6,7 @@ use crate::cg;
use darling::util::IdentList;
use proc_macro2::TokenStream;
use quote::TokenStreamExt;
use syn::{DeriveInput, Path};
use syn::{DeriveInput, Path, WhereClause};
use synstructure::{Structure, VariantInfo};

pub fn derive(mut input: DeriveInput) -> TokenStream {
Expand All @@ -21,21 +21,24 @@ pub fn derive(mut input: DeriveInput) -> TokenStream {
);
}
}
input.generics.where_clause = where_clause;
let (mut match_body, append_error_clause) = {
let s = Structure::new(&input);
let mut append_error_clause = s.variants().len() > 1;

let s = Structure::new(&input);
let mut append_error_clause = s.variants().len() > 1;
let mut match_body = s.variants().iter().fold(quote!(), |body, variant| {
let arm = match derive_variant_arm(variant, &mut where_clause) {
Ok(arm) => arm,
Err(()) => {
append_error_clause = true;
return body;
},
};
quote! { #body #arm }
});
(match_body, append_error_clause)
};

let mut match_body = s.variants().iter().fold(quote!(), |body, variant| {
let arm = match derive_variant_arm(variant) {
Ok(arm) => arm,
Err(()) => {
append_error_clause = true;
return body;
},
};
quote! { #body #arm }
});
input.generics.where_clause = where_clause;

if append_error_clause {
let input_attrs = cg::parse_input_attrs::<AnimateInputAttrs>(&input);
Expand Down Expand Up @@ -68,7 +71,10 @@ pub fn derive(mut input: DeriveInput) -> TokenStream {
}
}

fn derive_variant_arm(variant: &VariantInfo) -> Result<TokenStream, ()> {
fn derive_variant_arm(
variant: &VariantInfo,
where_clause: &mut Option<WhereClause>,
) -> Result<TokenStream, ()> {
let variant_attrs = cg::parse_variant_attrs_from_ast::<AnimationVariantAttrs>(&variant.ast());
if variant_attrs.error {
return Err(());
Expand All @@ -80,6 +86,10 @@ fn derive_variant_arm(variant: &VariantInfo) -> Result<TokenStream, ()> {
let iter = result_info.iter().zip(this_info.iter().zip(&other_info));
computations.append_all(iter.map(|(result, (this, other))| {
let field_attrs = cg::parse_field_attrs::<AnimationFieldAttrs>(&result.ast());
if field_attrs.field_bound {
let ty = &this.ast().ty;
cg::add_predicate(where_clause, parse_quote!(#ty: crate::values::animated::Animate));
}
if field_attrs.constant {
quote! {
if #this != #other {
Expand Down Expand Up @@ -127,4 +137,5 @@ pub struct AnimationVariantAttrs {
#[derive(Default, FromField)]
pub struct AnimationFieldAttrs {
pub constant: bool,
pub field_bound: bool,
}

0 comments on commit a8943d2

Please sign in to comment.