mirror of
https://github.com/servo/servo.git
synced 2025-08-03 20:50:07 +01:00
Implement #[distance(fallback)] for #[derive(ComputeSquaredDistance)]
This commit is contained in:
parent
4faadb489f
commit
41c3be54ea
3 changed files with 49 additions and 51 deletions
|
@ -278,7 +278,9 @@ impl ToComputedValue for specified::CalcLengthOrPercentage {
|
||||||
#[animate(fallback = "Self::animate_fallback")]
|
#[animate(fallback = "Self::animate_fallback")]
|
||||||
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
|
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
|
||||||
#[css(derive_debug)]
|
#[css(derive_debug)]
|
||||||
#[derive(Animate, Clone, Copy, PartialEq, ToAnimatedZero, ToCss)]
|
#[derive(Animate, Clone, ComputeSquaredDistance, Copy, PartialEq)]
|
||||||
|
#[derive(ToAnimatedZero, ToCss)]
|
||||||
|
#[distance(fallback = "Self::compute_squared_distance_fallback")]
|
||||||
pub enum LengthOrPercentage {
|
pub enum LengthOrPercentage {
|
||||||
Length(Au),
|
Length(Au),
|
||||||
Percentage(Percentage),
|
Percentage(Percentage),
|
||||||
|
@ -304,22 +306,16 @@ impl LengthOrPercentage {
|
||||||
let other = CalcLengthOrPercentage::from(*other);
|
let other = CalcLengthOrPercentage::from(*other);
|
||||||
Ok(LengthOrPercentage::Calc(this.animate(&other, procedure)?))
|
Ok(LengthOrPercentage::Calc(this.animate(&other, procedure)?))
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl ComputeSquaredDistance for LengthOrPercentage {
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
|
fn compute_squared_distance_fallback(
|
||||||
match (self, other) {
|
&self,
|
||||||
(&LengthOrPercentage::Length(ref this), &LengthOrPercentage::Length(ref other)) => {
|
other: &Self,
|
||||||
this.compute_squared_distance(other)
|
) -> Result<SquaredDistance, ()> {
|
||||||
},
|
CalcLengthOrPercentage::compute_squared_distance(
|
||||||
(&LengthOrPercentage::Percentage(ref this), &LengthOrPercentage::Percentage(ref other)) => {
|
&(*self).into(),
|
||||||
this.compute_squared_distance(other)
|
&(*other).into(),
|
||||||
},
|
)
|
||||||
(this, other) => {
|
|
||||||
CalcLengthOrPercentage::compute_squared_distance(&(*this).into(), &(*other).into())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -432,7 +428,8 @@ impl ToComputedValue for specified::LengthOrPercentage {
|
||||||
#[animate(fallback = "Self::animate_fallback")]
|
#[animate(fallback = "Self::animate_fallback")]
|
||||||
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
|
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
|
||||||
#[css(derive_debug)]
|
#[css(derive_debug)]
|
||||||
#[derive(Animate, Clone, Copy, PartialEq, ToCss)]
|
#[derive(Animate, Clone, ComputeSquaredDistance, Copy, PartialEq, ToCss)]
|
||||||
|
#[distance(fallback = "Self::compute_squared_distance_fallback")]
|
||||||
pub enum LengthOrPercentageOrAuto {
|
pub enum LengthOrPercentageOrAuto {
|
||||||
Length(Au),
|
Length(Au),
|
||||||
Percentage(Percentage),
|
Percentage(Percentage),
|
||||||
|
@ -453,22 +450,16 @@ impl LengthOrPercentageOrAuto {
|
||||||
this.animate(&other, procedure)?.ok_or(())?,
|
this.animate(&other, procedure)?.ok_or(())?,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl ComputeSquaredDistance for LengthOrPercentageOrAuto {
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
|
fn compute_squared_distance_fallback(
|
||||||
match (self, other) {
|
&self,
|
||||||
(&LengthOrPercentageOrAuto::Length(ref this), &LengthOrPercentageOrAuto::Length(ref other)) => {
|
other: &Self,
|
||||||
this.compute_squared_distance(other)
|
) -> Result<SquaredDistance, ()> {
|
||||||
},
|
<Option<CalcLengthOrPercentage>>::compute_squared_distance(
|
||||||
(&LengthOrPercentageOrAuto::Percentage(ref this), &LengthOrPercentageOrAuto::Percentage(ref other)) => {
|
&(*self).into(),
|
||||||
this.compute_squared_distance(other)
|
&(*other).into(),
|
||||||
},
|
)
|
||||||
(this, other) => {
|
|
||||||
<Option<CalcLengthOrPercentage>>::compute_squared_distance(&(*this).into(), &(*other).into())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -533,7 +524,8 @@ impl ToComputedValue for specified::LengthOrPercentageOrAuto {
|
||||||
#[animate(fallback = "Self::animate_fallback")]
|
#[animate(fallback = "Self::animate_fallback")]
|
||||||
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
|
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
|
||||||
#[css(derive_debug)]
|
#[css(derive_debug)]
|
||||||
#[derive(Animate, Clone, Copy, PartialEq, ToCss)]
|
#[derive(Animate, Clone, ComputeSquaredDistance, Copy, PartialEq, ToCss)]
|
||||||
|
#[distance(fallback = "Self::compute_squared_distance_fallback")]
|
||||||
pub enum LengthOrPercentageOrNone {
|
pub enum LengthOrPercentageOrNone {
|
||||||
Length(Au),
|
Length(Au),
|
||||||
Percentage(Percentage),
|
Percentage(Percentage),
|
||||||
|
@ -554,22 +546,15 @@ impl LengthOrPercentageOrNone {
|
||||||
this.animate(&other, procedure)?.ok_or(())?,
|
this.animate(&other, procedure)?.ok_or(())?,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl ComputeSquaredDistance for LengthOrPercentageOrNone {
|
fn compute_squared_distance_fallback(
|
||||||
#[inline]
|
&self,
|
||||||
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
|
other: &Self,
|
||||||
match (self, other) {
|
) -> Result<SquaredDistance, ()> {
|
||||||
(&LengthOrPercentageOrNone::Length(ref this), &LengthOrPercentageOrNone::Length(ref other)) => {
|
<Option<CalcLengthOrPercentage>>::compute_squared_distance(
|
||||||
this.compute_squared_distance(other)
|
&(*self).into(),
|
||||||
},
|
&(*other).into(),
|
||||||
(&LengthOrPercentageOrNone::Percentage(ref this), &LengthOrPercentageOrNone::Percentage(ref other)) => {
|
)
|
||||||
this.compute_squared_distance(other)
|
|
||||||
},
|
|
||||||
(this, other) => {
|
|
||||||
<Option<CalcLengthOrPercentage>>::compute_squared_distance(&(*this).into(), &(*other).into())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,15 +4,16 @@
|
||||||
|
|
||||||
use animate::AnimationVariantAttrs;
|
use animate::AnimationVariantAttrs;
|
||||||
use cg;
|
use cg;
|
||||||
use quote;
|
use quote::Tokens;
|
||||||
use syn;
|
use syn::{DeriveInput, Path};
|
||||||
|
|
||||||
pub fn derive(input: syn::DeriveInput) -> quote::Tokens {
|
pub fn derive(input: DeriveInput) -> Tokens {
|
||||||
let name = &input.ident;
|
let name = &input.ident;
|
||||||
let trait_path = &["values", "distance", "ComputeSquaredDistance"];
|
let trait_path = &["values", "distance", "ComputeSquaredDistance"];
|
||||||
let (impl_generics, ty_generics, mut where_clause) =
|
let (impl_generics, ty_generics, mut where_clause) =
|
||||||
cg::trait_parts(&input, trait_path);
|
cg::trait_parts(&input, trait_path);
|
||||||
|
|
||||||
|
let input_attrs = cg::parse_input_attrs::<DistanceInputAttrs>(&input);
|
||||||
let variants = cg::variants(&input);
|
let variants = cg::variants(&input);
|
||||||
let mut match_body = quote!();
|
let mut match_body = quote!();
|
||||||
let mut append_error_clause = variants.len() > 1;
|
let mut append_error_clause = variants.len() > 1;
|
||||||
|
@ -45,7 +46,13 @@ pub fn derive(input: syn::DeriveInput) -> quote::Tokens {
|
||||||
}));
|
}));
|
||||||
|
|
||||||
if append_error_clause {
|
if append_error_clause {
|
||||||
match_body = quote! { #match_body, _ => Err(()), };
|
if let Some(fallback) = input_attrs.fallback {
|
||||||
|
match_body.append(quote! {
|
||||||
|
(this, other) => #fallback(this, other)
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
match_body.append(quote! { _ => Err(()) });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
|
@ -63,3 +70,9 @@ pub fn derive(input: syn::DeriveInput) -> quote::Tokens {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[darling(attributes(distance), default)]
|
||||||
|
#[derive(Default, FromDeriveInput)]
|
||||||
|
struct DistanceInputAttrs {
|
||||||
|
fallback: Option<Path>,
|
||||||
|
}
|
||||||
|
|
|
@ -25,7 +25,7 @@ pub fn derive_animate(stream: TokenStream) -> TokenStream {
|
||||||
animate::derive(input).to_string().parse().unwrap()
|
animate::derive(input).to_string().parse().unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[proc_macro_derive(ComputeSquaredDistance, attributes(animation))]
|
#[proc_macro_derive(ComputeSquaredDistance, attributes(animation, distance))]
|
||||||
pub fn derive_compute_squared_distance(stream: TokenStream) -> TokenStream {
|
pub fn derive_compute_squared_distance(stream: TokenStream) -> TokenStream {
|
||||||
let input = syn::parse_derive_input(&stream.to_string()).unwrap();
|
let input = syn::parse_derive_input(&stream.to_string()).unwrap();
|
||||||
compute_squared_distance::derive(input).to_string().parse().unwrap()
|
compute_squared_distance::derive(input).to_string().parse().unwrap()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue