Skip to content
Permalink
Browse files

style: Part 1: Support field_bound on Animate.

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 12, 2019
1 parent 7efbd9c commit a8943d2ec519c4fce68fffcee4e6512e63eb4602
Showing with 28 additions and 16 deletions.
  1. +2 −1 components/style/values/animated/mod.rs
  2. +26 −15 components/style_derive/animate.rs
@@ -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, ()>;
@@ -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 {
@@ -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);
@@ -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(());
@@ -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 {
@@ -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.
You can’t perform that action at this time.