mirror of
https://github.com/servo/servo.git
synced 2025-08-03 20:50:07 +01:00
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
This commit is contained in:
parent
7efbd9cde6
commit
a8943d2ec5
2 changed files with 29 additions and 17 deletions
|
@ -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,22 +21,25 @@ pub fn derive(mut input: DeriveInput) -> TokenStream {
|
|||
);
|
||||
}
|
||||
}
|
||||
let (mut match_body, append_error_clause) = {
|
||||
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)
|
||||
};
|
||||
|
||||
input.generics.where_clause = where_clause;
|
||||
|
||||
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) {
|
||||
Ok(arm) => arm,
|
||||
Err(()) => {
|
||||
append_error_clause = true;
|
||||
return body;
|
||||
},
|
||||
};
|
||||
quote! { #body #arm }
|
||||
});
|
||||
|
||||
if append_error_clause {
|
||||
let input_attrs = cg::parse_input_attrs::<AnimateInputAttrs>(&input);
|
||||
if let Some(fallback) = input_attrs.fallback {
|
||||
|
@ -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,
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue