mirror of
https://github.com/servo/servo.git
synced 2025-08-06 22:15:33 +01:00
Opt into field bounds for #[derive(ToComputedValue)]
This commit is contained in:
parent
2efd06c12d
commit
4d0bf2ddf9
5 changed files with 55 additions and 46 deletions
|
@ -666,7 +666,7 @@ pub enum GridTemplateComponent<L, I> {
|
||||||
/// `none` value.
|
/// `none` value.
|
||||||
None,
|
None,
|
||||||
/// The grid `<track-list>`
|
/// The grid `<track-list>`
|
||||||
TrackList(TrackList<L, I>),
|
TrackList(#[compute(field_bound)] TrackList<L, I>),
|
||||||
/// A `subgrid <line-name-list>?`
|
/// A `subgrid <line-name-list>?`
|
||||||
Subgrid(LineNameList),
|
Subgrid(LineNameList),
|
||||||
}
|
}
|
||||||
|
|
|
@ -247,7 +247,6 @@ pub enum TransformOperation<Angle, Number, Length, Integer, LengthOrPercentage>
|
||||||
#[allow(missing_docs)]
|
#[allow(missing_docs)]
|
||||||
#[css(comma, function = "interpolatematrix")]
|
#[css(comma, function = "interpolatematrix")]
|
||||||
InterpolateMatrix {
|
InterpolateMatrix {
|
||||||
#[compute(ignore_bound)]
|
|
||||||
from_list: Transform<
|
from_list: Transform<
|
||||||
TransformOperation<
|
TransformOperation<
|
||||||
Angle,
|
Angle,
|
||||||
|
@ -257,7 +256,6 @@ pub enum TransformOperation<Angle, Number, Length, Integer, LengthOrPercentage>
|
||||||
LengthOrPercentage,
|
LengthOrPercentage,
|
||||||
>,
|
>,
|
||||||
>,
|
>,
|
||||||
#[compute(ignore_bound)]
|
|
||||||
to_list: Transform<
|
to_list: Transform<
|
||||||
TransformOperation<
|
TransformOperation<
|
||||||
Angle,
|
Angle,
|
||||||
|
@ -273,7 +271,6 @@ pub enum TransformOperation<Angle, Number, Length, Integer, LengthOrPercentage>
|
||||||
#[allow(missing_docs)]
|
#[allow(missing_docs)]
|
||||||
#[css(comma, function = "accumulatematrix")]
|
#[css(comma, function = "accumulatematrix")]
|
||||||
AccumulateMatrix {
|
AccumulateMatrix {
|
||||||
#[compute(ignore_bound)]
|
|
||||||
from_list: Transform<
|
from_list: Transform<
|
||||||
TransformOperation<
|
TransformOperation<
|
||||||
Angle,
|
Angle,
|
||||||
|
@ -283,7 +280,6 @@ pub enum TransformOperation<Angle, Number, Length, Integer, LengthOrPercentage>
|
||||||
LengthOrPercentage,
|
LengthOrPercentage,
|
||||||
>,
|
>,
|
||||||
>,
|
>,
|
||||||
#[compute(ignore_bound)]
|
|
||||||
to_list: Transform<
|
to_list: Transform<
|
||||||
TransformOperation<
|
TransformOperation<
|
||||||
Angle,
|
Angle,
|
||||||
|
|
|
@ -111,7 +111,7 @@ where
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fmap_output_type(
|
pub fn fmap_output_type(
|
||||||
ty: Type,
|
ty: Type,
|
||||||
trait_path: &Path,
|
trait_path: &Path,
|
||||||
trait_output: Ident,
|
trait_output: Ident,
|
||||||
|
@ -119,17 +119,6 @@ fn fmap_output_type(
|
||||||
parse_quote!(<#ty as ::#trait_path>::#trait_output)
|
parse_quote!(<#ty as ::#trait_path>::#trait_output)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn fmap_trait_parts<'input, 'path>(
|
|
||||||
input: &'input DeriveInput,
|
|
||||||
trait_path: &'path Path,
|
|
||||||
trait_output: Ident,
|
|
||||||
) -> (ImplGenerics<'input>, TypeGenerics<'input>, WhereClause<'input, 'path>, Path) {
|
|
||||||
let (impl_generics, ty_generics, mut where_clause) = trait_parts(input, trait_path);
|
|
||||||
where_clause.trait_output = Some(trait_output);
|
|
||||||
let output_ty = fmap_trait_output(input, trait_path, trait_output);
|
|
||||||
(impl_generics, ty_generics, where_clause, output_ty)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn fmap_trait_output(
|
pub fn fmap_trait_output(
|
||||||
input: &DeriveInput,
|
input: &DeriveInput,
|
||||||
trait_path: &Path,
|
trait_path: &Path,
|
||||||
|
|
|
@ -15,7 +15,6 @@ pub fn derive(mut input: DeriveInput) -> quote::Tokens {
|
||||||
parse_quote!(#param: ::values::animated::ToAnimatedValue),
|
parse_quote!(#param: ::values::animated::ToAnimatedValue),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
input.generics.where_clause = where_clause;
|
|
||||||
|
|
||||||
let to_body = cg::fmap_match(&input, BindStyle::Move, |binding| {
|
let to_body = cg::fmap_match(&input, BindStyle::Move, |binding| {
|
||||||
quote!(::values::animated::ToAnimatedValue::to_animated_value(#binding))
|
quote!(::values::animated::ToAnimatedValue::to_animated_value(#binding))
|
||||||
|
@ -24,6 +23,7 @@ pub fn derive(mut input: DeriveInput) -> quote::Tokens {
|
||||||
quote!(::values::animated::ToAnimatedValue::from_animated_value(#binding))
|
quote!(::values::animated::ToAnimatedValue::from_animated_value(#binding))
|
||||||
});
|
});
|
||||||
|
|
||||||
|
input.generics.where_clause = where_clause;
|
||||||
let name = &input.ident;
|
let name = &input.ident;
|
||||||
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
|
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
|
||||||
let animated_value_type = cg::fmap_trait_output(
|
let animated_value_type = cg::fmap_trait_output(
|
||||||
|
|
|
@ -4,21 +4,59 @@
|
||||||
|
|
||||||
use cg;
|
use cg;
|
||||||
use quote::Tokens;
|
use quote::Tokens;
|
||||||
use syn::{Ident, DeriveInput};
|
use syn::DeriveInput;
|
||||||
use synstructure::BindStyle;
|
use synstructure::BindStyle;
|
||||||
|
|
||||||
pub fn derive(input: DeriveInput) -> Tokens {
|
pub fn derive(mut input: DeriveInput) -> Tokens {
|
||||||
let name = &input.ident;
|
let mut where_clause = input.generics.where_clause.take();
|
||||||
let trait_path = parse_quote!(values::computed::ToComputedValue);
|
let (to_body, from_body) = {
|
||||||
let (impl_generics, ty_generics, mut where_clause, computed_value_type) =
|
let params = input.generics.type_params().collect::<Vec<_>>();
|
||||||
cg::fmap_trait_parts(&input, &trait_path, Ident::from("ComputedValue"));
|
for param in ¶ms {
|
||||||
|
cg::add_predicate(
|
||||||
|
&mut where_clause,
|
||||||
|
parse_quote!(#param: ::values::computed::ToComputedValue),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
if input.generics.params.is_empty() {
|
let to_body = cg::fmap_match(&input, BindStyle::Ref, |binding| {
|
||||||
|
let attrs = cg::parse_field_attrs::<ComputedValueAttrs>(&binding.ast());
|
||||||
|
if attrs.field_bound {
|
||||||
|
let ty = &binding.ast().ty;
|
||||||
|
|
||||||
|
let output_type = cg::map_type_params(ty, ¶ms, &mut |ident| {
|
||||||
|
parse_quote!(<#ident as ::values::computed::ToComputedValue>::ComputedValue)
|
||||||
|
});
|
||||||
|
|
||||||
|
cg::add_predicate(
|
||||||
|
&mut where_clause,
|
||||||
|
parse_quote!(
|
||||||
|
#ty: ::values::computed::ToComputedValue<ComputedValue = #output_type>
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
quote! {
|
||||||
|
::values::computed::ToComputedValue::to_computed_value(#binding, context)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
let from_body = cg::fmap_match(&input, BindStyle::Ref, |binding| {
|
||||||
|
quote! {
|
||||||
|
::values::computed::ToComputedValue::from_computed_value(#binding)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
(to_body, from_body)
|
||||||
|
};
|
||||||
|
|
||||||
|
input.generics.where_clause = where_clause;
|
||||||
|
let name = &input.ident;
|
||||||
|
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
|
||||||
|
|
||||||
|
if input.generics.type_params().next().is_none() {
|
||||||
return quote! {
|
return quote! {
|
||||||
impl #impl_generics ::values::computed::ToComputedValue for #name #ty_generics
|
impl #impl_generics ::values::computed::ToComputedValue for #name #ty_generics
|
||||||
#where_clause
|
#where_clause
|
||||||
{
|
{
|
||||||
type ComputedValue = #computed_value_type;
|
type ComputedValue = Self;
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn to_computed_value(
|
fn to_computed_value(
|
||||||
|
@ -36,25 +74,11 @@ pub fn derive(input: DeriveInput) -> Tokens {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let to_body = cg::fmap_match(&input, BindStyle::Ref, |binding| {
|
let computed_value_type = cg::fmap_trait_output(
|
||||||
let attrs = cg::parse_field_attrs::<ComputedValueAttrs>(&binding.ast());
|
&input,
|
||||||
if !attrs.ignore_bound {
|
&parse_quote!(values::computed::ToComputedValue),
|
||||||
where_clause.add_trait_bound(&binding.ast().ty);
|
"ComputedValue".into(),
|
||||||
}
|
);
|
||||||
quote! {
|
|
||||||
::values::computed::ToComputedValue::to_computed_value(#binding, context)
|
|
||||||
}
|
|
||||||
});
|
|
||||||
let from_body = cg::fmap_match(&input, BindStyle::Ref, |binding| {
|
|
||||||
let attrs = cg::parse_field_attrs::<ComputedValueAttrs>(&binding.ast());
|
|
||||||
if attrs.clone {
|
|
||||||
quote! { ::std::clone::Clone::clone(#binding) }
|
|
||||||
} else {
|
|
||||||
quote! {
|
|
||||||
::values::computed::ToComputedValue::from_computed_value(#binding)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
impl #impl_generics ::values::computed::ToComputedValue for #name #ty_generics #where_clause {
|
impl #impl_generics ::values::computed::ToComputedValue for #name #ty_generics #where_clause {
|
||||||
|
@ -81,5 +105,5 @@ pub fn derive(input: DeriveInput) -> Tokens {
|
||||||
#[darling(attributes(compute), default)]
|
#[darling(attributes(compute), default)]
|
||||||
#[derive(Default, FromField)]
|
#[derive(Default, FromField)]
|
||||||
struct ComputedValueAttrs {
|
struct ComputedValueAttrs {
|
||||||
ignore_bound: bool,
|
field_bound: bool,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue