Introduce #[animation(constant)] for the Animate trait

This allows us to handle fields that should be the same during animations.
This commit is contained in:
Anthony Ramine 2017-08-25 23:51:12 +02:00
parent e49dbc4dfa
commit eaf2f1ec33
5 changed files with 59 additions and 52 deletions

View file

@ -28,9 +28,30 @@ pub fn derive(input: syn::DeriveInput) -> quote::Tokens {
let mut computations = quote!();
let iter = result_info.iter().zip(this_info.iter().zip(&other_info));
computations.append_all(iter.map(|(result, (this, other))| {
where_clause.add_trait_bound(this.field.ty.clone());
quote! {
let #result = ::values::animated::Animate::animate(#this, #other, procedure)?;
let field_attrs = cg::parse_field_attrs::<AnimateFieldAttrs>(&result.field);
if field_attrs.constant {
if cg::is_parameterized(&result.field.ty, where_clause.params) {
where_clause.inner.predicates.push(cg::where_predicate(
result.field.ty.clone(),
&["std", "cmp", "PartialEq"],
));
where_clause.inner.predicates.push(cg::where_predicate(
result.field.ty.clone(),
&["std", "clone", "Clone"],
));
}
quote! {
if #this != #other {
return Err(());
}
let #result = ::std::clone::Clone::clone(#this);
}
} else {
where_clause.add_trait_bound(result.field.ty.clone());
quote! {
let #result =
::values::animated::Animate::animate(#this, #other, procedure)?;
}
}
}));
Some(quote! {
@ -67,3 +88,9 @@ pub fn derive(input: syn::DeriveInput) -> quote::Tokens {
pub struct AnimateAttrs {
pub error: bool,
}
#[derive(Default, FromField)]
#[darling(attributes(animation), default)]
pub struct AnimateFieldAttrs {
pub constant: bool,
}