mirror of
https://github.com/servo/servo.git
synced 2025-08-04 05:00:08 +01:00
Opt into field bounds when deriving ToCss, instead of opting out
This commit is contained in:
parent
8133f788cf
commit
859002a81f
8 changed files with 89 additions and 67 deletions
|
@ -62,9 +62,9 @@ pub enum ShapeSource<BasicShape, ReferenceBox, ImageOrUrl> {
|
||||||
#[derive(Animate, Clone, ComputeSquaredDistance, Debug, MallocSizeOf, PartialEq)]
|
#[derive(Animate, Clone, ComputeSquaredDistance, Debug, MallocSizeOf, PartialEq)]
|
||||||
#[derive(ToComputedValue, ToCss)]
|
#[derive(ToComputedValue, ToCss)]
|
||||||
pub enum BasicShape<H, V, LengthOrPercentage> {
|
pub enum BasicShape<H, V, LengthOrPercentage> {
|
||||||
Inset(InsetRect<LengthOrPercentage>),
|
Inset(#[css(field_bound)] InsetRect<LengthOrPercentage>),
|
||||||
Circle(Circle<H, V, LengthOrPercentage>),
|
Circle(#[css(field_bound)] Circle<H, V, LengthOrPercentage>),
|
||||||
Ellipse(Ellipse<H, V, LengthOrPercentage>),
|
Ellipse(#[css(field_bound)] Ellipse<H, V, LengthOrPercentage>),
|
||||||
Polygon(Polygon<LengthOrPercentage>),
|
Polygon(Polygon<LengthOrPercentage>),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -32,7 +32,7 @@ pub struct BorderImageSlice<NumberOrPercentage> {
|
||||||
/// A generic value for the `border-*-radius` longhand properties.
|
/// A generic value for the `border-*-radius` longhand properties.
|
||||||
#[derive(Animate, Clone, ComputeSquaredDistance, Copy, Debug)]
|
#[derive(Animate, Clone, ComputeSquaredDistance, Copy, Debug)]
|
||||||
#[derive(MallocSizeOf, PartialEq, ToComputedValue, ToCss)]
|
#[derive(MallocSizeOf, PartialEq, ToComputedValue, ToCss)]
|
||||||
pub struct BorderCornerRadius<L>(pub Size<L>);
|
pub struct BorderCornerRadius<L>(#[css(field_bound)] pub Size<L>);
|
||||||
|
|
||||||
impl<L> BorderCornerRadius<L> {
|
impl<L> BorderCornerRadius<L> {
|
||||||
/// Trivially create a `BorderCornerRadius`.
|
/// Trivially create a `BorderCornerRadius`.
|
||||||
|
@ -44,7 +44,7 @@ impl<L> BorderCornerRadius<L> {
|
||||||
/// A generic value for the `border-spacing` property.
|
/// A generic value for the `border-spacing` property.
|
||||||
#[derive(Animate, Clone, ComputeSquaredDistance, Copy, Debug, MallocSizeOf)]
|
#[derive(Animate, Clone, ComputeSquaredDistance, Copy, Debug, MallocSizeOf)]
|
||||||
#[derive(PartialEq, ToAnimatedValue, ToAnimatedZero, ToComputedValue, ToCss)]
|
#[derive(PartialEq, ToAnimatedValue, ToAnimatedZero, ToComputedValue, ToCss)]
|
||||||
pub struct BorderSpacing<L>(pub Size<L>);
|
pub struct BorderSpacing<L>(#[css(field_bound)] pub Size<L>);
|
||||||
|
|
||||||
impl<L> BorderSpacing<L> {
|
impl<L> BorderSpacing<L> {
|
||||||
/// Trivially create a `BorderCornerRadius`.
|
/// Trivially create a `BorderCornerRadius`.
|
||||||
|
|
|
@ -250,7 +250,6 @@ pub enum TransformOperation<Angle, Number, Length, Integer, LengthOrPercentage>
|
||||||
#[css(comma, function = "interpolatematrix")]
|
#[css(comma, function = "interpolatematrix")]
|
||||||
InterpolateMatrix {
|
InterpolateMatrix {
|
||||||
#[compute(ignore_bound)]
|
#[compute(ignore_bound)]
|
||||||
#[css(ignore_bound)]
|
|
||||||
from_list: Transform<
|
from_list: Transform<
|
||||||
TransformOperation<
|
TransformOperation<
|
||||||
Angle,
|
Angle,
|
||||||
|
@ -261,7 +260,6 @@ pub enum TransformOperation<Angle, Number, Length, Integer, LengthOrPercentage>
|
||||||
>,
|
>,
|
||||||
>,
|
>,
|
||||||
#[compute(ignore_bound)]
|
#[compute(ignore_bound)]
|
||||||
#[css(ignore_bound)]
|
|
||||||
to_list: Transform<
|
to_list: Transform<
|
||||||
TransformOperation<
|
TransformOperation<
|
||||||
Angle,
|
Angle,
|
||||||
|
@ -279,7 +277,6 @@ pub enum TransformOperation<Angle, Number, Length, Integer, LengthOrPercentage>
|
||||||
#[css(comma, function = "accumulatematrix")]
|
#[css(comma, function = "accumulatematrix")]
|
||||||
AccumulateMatrix {
|
AccumulateMatrix {
|
||||||
#[compute(ignore_bound)]
|
#[compute(ignore_bound)]
|
||||||
#[css(ignore_bound)]
|
|
||||||
from_list: Transform<
|
from_list: Transform<
|
||||||
TransformOperation<
|
TransformOperation<
|
||||||
Angle,
|
Angle,
|
||||||
|
@ -290,7 +287,6 @@ pub enum TransformOperation<Angle, Number, Length, Integer, LengthOrPercentage>
|
||||||
>,
|
>,
|
||||||
>,
|
>,
|
||||||
#[compute(ignore_bound)]
|
#[compute(ignore_bound)]
|
||||||
#[css(ignore_bound)]
|
|
||||||
to_list: Transform<
|
to_list: Transform<
|
||||||
TransformOperation<
|
TransformOperation<
|
||||||
Angle,
|
Angle,
|
||||||
|
|
|
@ -32,16 +32,22 @@ pub fn derive(input: DeriveInput) -> Tokens {
|
||||||
let field_attrs = cg::parse_field_attrs::<AnimationFieldAttrs>(&result.ast());
|
let field_attrs = cg::parse_field_attrs::<AnimationFieldAttrs>(&result.ast());
|
||||||
if field_attrs.constant {
|
if field_attrs.constant {
|
||||||
if cg::is_parameterized(&result.ast().ty, &where_clause.params, None) {
|
if cg::is_parameterized(&result.ast().ty, &where_clause.params, None) {
|
||||||
where_clause.add_predicate(cg::where_predicate(
|
cg::add_predicate(
|
||||||
|
&mut where_clause.inner,
|
||||||
|
cg::where_predicate(
|
||||||
result.ast().ty.clone(),
|
result.ast().ty.clone(),
|
||||||
&parse_quote!(std::cmp::PartialEq),
|
&parse_quote!(std::cmp::PartialEq),
|
||||||
None,
|
None,
|
||||||
));
|
),
|
||||||
where_clause.add_predicate(cg::where_predicate(
|
);
|
||||||
|
cg::add_predicate(
|
||||||
|
&mut where_clause.inner,
|
||||||
|
cg::where_predicate(
|
||||||
result.ast().ty.clone(),
|
result.ast().ty.clone(),
|
||||||
&parse_quote!(std::clone::Clone),
|
&parse_quote!(std::clone::Clone),
|
||||||
None,
|
None,
|
||||||
));
|
),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
quote! {
|
quote! {
|
||||||
if #this != #other {
|
if #this != #other {
|
||||||
|
|
|
@ -5,11 +5,11 @@
|
||||||
use darling::{FromDeriveInput, FromField, FromVariant};
|
use darling::{FromDeriveInput, FromField, FromVariant};
|
||||||
use quote::{ToTokens, Tokens};
|
use quote::{ToTokens, Tokens};
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
use syn::{self, DeriveInput, Field, Ident};
|
use syn::{self, AngleBracketedGenericArguments, Binding, DeriveInput, Field};
|
||||||
use syn::{ImplGenerics, Path, PathArguments, PathSegment, AngleBracketedGenericArguments, GenericParam};
|
use syn::{GenericArgument, GenericParam, Ident, ImplGenerics, Path};
|
||||||
use syn::{QSelf, Type, TypeGenerics, TypeParam};
|
use syn::{PathArguments, PathSegment, QSelf, Type, TypeArray, TypeGenerics};
|
||||||
use syn::{TypeSlice, TypeArray, TypeTuple, TypePath, TypeParen};
|
use syn::{TypeParam, TypeParen, TypePath, TypeSlice, TypeTuple};
|
||||||
use syn::{Variant, WherePredicate, GenericArgument, Binding};
|
use syn::{Variant, WherePredicate};
|
||||||
use syn::visit::{self, Visit};
|
use syn::visit::{self, Visit};
|
||||||
use synstructure::{self, BindingInfo, BindStyle, VariantAst, VariantInfo};
|
use synstructure::{self, BindingInfo, BindStyle, VariantAst, VariantInfo};
|
||||||
|
|
||||||
|
@ -42,13 +42,13 @@ impl<'input, 'path> WhereClause<'input, 'path> {
|
||||||
let output = if let Some(output) = self.trait_output {
|
let output = if let Some(output) = self.trait_output {
|
||||||
output
|
output
|
||||||
} else {
|
} else {
|
||||||
self.add_predicate(where_predicate(ty.clone(), trait_path, None));
|
add_predicate(&mut self.inner, where_predicate(ty.clone(), trait_path, None));
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Type::Path(syn::TypePath { ref path, .. }) = *ty {
|
if let Type::Path(syn::TypePath { ref path, .. }) = *ty {
|
||||||
if path_to_ident(path).is_some() {
|
if path_to_ident(path).is_some() {
|
||||||
self.add_predicate(where_predicate(ty.clone(), trait_path, None));
|
add_predicate(&mut self.inner, where_predicate(ty.clone(), trait_path, None));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -64,29 +64,28 @@ impl<'input, 'path> WhereClause<'input, 'path> {
|
||||||
Some((output, output_type)),
|
Some((output, output_type)),
|
||||||
);
|
);
|
||||||
|
|
||||||
self.add_predicate(pred);
|
add_predicate(&mut self.inner, pred);
|
||||||
|
|
||||||
if let Some(found) = found {
|
if let Some(found) = found {
|
||||||
for ident in found {
|
for ident in found {
|
||||||
let ty = Type::Path(syn::TypePath { qself: None, path: ident.into() });
|
let ty = Type::Path(syn::TypePath { qself: None, path: ident.into() });
|
||||||
if !self.bounded_types.contains(&ty) {
|
if !self.bounded_types.contains(&ty) {
|
||||||
self.bounded_types.insert(ty.clone());
|
self.bounded_types.insert(ty.clone());
|
||||||
self.add_predicate(
|
add_predicate(
|
||||||
|
&mut self.inner,
|
||||||
where_predicate(ty, trait_path, None),
|
where_predicate(ty, trait_path, None),
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn add_predicate(&mut self, pred: WherePredicate) {
|
pub fn add_predicate(
|
||||||
if let Some(ref mut inner) = self.inner {
|
where_clause: &mut Option<syn::WhereClause>,
|
||||||
inner.predicates.push(pred);
|
pred: WherePredicate,
|
||||||
} else {
|
) {
|
||||||
self.inner = Some(parse_quote!(where));
|
where_clause.get_or_insert(parse_quote!(where)).predicates.push(pred);
|
||||||
self.add_predicate(pred);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn fmap_match<F>(
|
pub fn fmap_match<F>(
|
||||||
|
|
|
@ -27,11 +27,14 @@ pub fn derive(input: syn::DeriveInput) -> quote::Tokens {
|
||||||
let field_attrs = cg::parse_field_attrs::<AnimationFieldAttrs>(&binding.ast());
|
let field_attrs = cg::parse_field_attrs::<AnimationFieldAttrs>(&binding.ast());
|
||||||
if field_attrs.constant {
|
if field_attrs.constant {
|
||||||
if cg::is_parameterized(&binding.ast().ty, &where_clause.params, None) {
|
if cg::is_parameterized(&binding.ast().ty, &where_clause.params, None) {
|
||||||
where_clause.add_predicate(cg::where_predicate(
|
cg::add_predicate(
|
||||||
|
&mut where_clause.inner,
|
||||||
|
cg::where_predicate(
|
||||||
binding.ast().ty.clone(),
|
binding.ast().ty.clone(),
|
||||||
&parse_quote!(std::clone::Clone),
|
&parse_quote!(std::clone::Clone),
|
||||||
None,
|
None,
|
||||||
));
|
),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
quote! {
|
quote! {
|
||||||
let #mapped_binding = ::std::clone::Clone::clone(#binding);
|
let #mapped_binding = ::std::clone::Clone::clone(#binding);
|
||||||
|
|
|
@ -40,11 +40,14 @@ pub fn derive(input: DeriveInput) -> Tokens {
|
||||||
let attrs = cg::parse_field_attrs::<ComputedValueAttrs>(&binding.ast());
|
let attrs = cg::parse_field_attrs::<ComputedValueAttrs>(&binding.ast());
|
||||||
if attrs.clone {
|
if attrs.clone {
|
||||||
if cg::is_parameterized(&binding.ast().ty, &where_clause.params, None) {
|
if cg::is_parameterized(&binding.ast().ty, &where_clause.params, None) {
|
||||||
where_clause.add_predicate(cg::where_predicate(
|
cg::add_predicate(
|
||||||
|
&mut where_clause.inner,
|
||||||
|
cg::where_predicate(
|
||||||
binding.ast().ty.clone(),
|
binding.ast().ty.clone(),
|
||||||
&parse_quote!(std::clone::Clone),
|
&parse_quote!(std::clone::Clone),
|
||||||
None,
|
None,
|
||||||
));
|
),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
quote! { ::std::clone::Clone::clone(#binding) }
|
quote! { ::std::clone::Clone::clone(#binding) }
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -2,28 +2,41 @@
|
||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
use cg::{self, WhereClause};
|
use cg;
|
||||||
use darling::util::Override;
|
use darling::util::Override;
|
||||||
use quote::{ToTokens, Tokens};
|
use quote::{ToTokens, Tokens};
|
||||||
use syn::{self, Data, Path};
|
use syn::{self, Data, GenericParam, Path, WhereClause};
|
||||||
use synstructure::{BindingInfo, Structure, VariantInfo};
|
use synstructure::{BindingInfo, Structure, VariantInfo};
|
||||||
|
|
||||||
pub fn derive(input: syn::DeriveInput) -> Tokens {
|
pub fn derive(mut input: syn::DeriveInput) -> Tokens {
|
||||||
let name = &input.ident;
|
let mut where_clause = input.generics.where_clause.take();
|
||||||
let trait_path = parse_quote!(::style_traits::ToCss);
|
for param in &input.generics.params {
|
||||||
let (impl_generics, ty_generics, mut where_clause) =
|
let param = match *param {
|
||||||
cg::trait_parts(&input, &trait_path);
|
GenericParam::Type(ref param) => param,
|
||||||
|
_ => continue,
|
||||||
|
};
|
||||||
|
cg::add_predicate(
|
||||||
|
&mut where_clause,
|
||||||
|
parse_quote!(#param: ::style_traits::ToCss),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
let input_attrs = cg::parse_input_attrs::<CssInputAttrs>(&input);
|
let input_attrs = cg::parse_input_attrs::<CssInputAttrs>(&input);
|
||||||
if let Data::Enum(_) = input.data {
|
if let Data::Enum(_) = input.data {
|
||||||
assert!(input_attrs.function.is_none(), "#[css(function)] is not allowed on enums");
|
assert!(input_attrs.function.is_none(), "#[css(function)] is not allowed on enums");
|
||||||
assert!(!input_attrs.comma, "#[css(comma)] is not allowed on enums");
|
assert!(!input_attrs.comma, "#[css(comma)] is not allowed on enums");
|
||||||
}
|
}
|
||||||
let s = Structure::new(&input);
|
|
||||||
|
|
||||||
let match_body = s.each_variant(|variant| {
|
let match_body = {
|
||||||
|
let s = Structure::new(&input);
|
||||||
|
s.each_variant(|variant| {
|
||||||
derive_variant_arm(variant, &mut where_clause)
|
derive_variant_arm(variant, &mut where_clause)
|
||||||
});
|
})
|
||||||
|
};
|
||||||
|
input.generics.where_clause = where_clause;
|
||||||
|
|
||||||
|
let name = &input.ident;
|
||||||
|
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
|
||||||
|
|
||||||
let mut impls = quote! {
|
let mut impls = quote! {
|
||||||
impl #impl_generics ::style_traits::ToCss for #name #ty_generics #where_clause {
|
impl #impl_generics ::style_traits::ToCss for #name #ty_generics #where_clause {
|
||||||
|
@ -34,7 +47,7 @@ pub fn derive(input: syn::DeriveInput) -> Tokens {
|
||||||
dest: &mut ::style_traits::CssWriter<W>,
|
dest: &mut ::style_traits::CssWriter<W>,
|
||||||
) -> ::std::fmt::Result
|
) -> ::std::fmt::Result
|
||||||
where
|
where
|
||||||
W: ::std::fmt::Write
|
W: ::std::fmt::Write,
|
||||||
{
|
{
|
||||||
match *self {
|
match *self {
|
||||||
#match_body
|
#match_body
|
||||||
|
@ -61,7 +74,7 @@ pub fn derive(input: syn::DeriveInput) -> Tokens {
|
||||||
|
|
||||||
fn derive_variant_arm(
|
fn derive_variant_arm(
|
||||||
variant: &VariantInfo,
|
variant: &VariantInfo,
|
||||||
where_clause: &mut WhereClause,
|
generics: &mut Option<WhereClause>,
|
||||||
) -> Tokens {
|
) -> Tokens {
|
||||||
let bindings = variant.bindings();
|
let bindings = variant.bindings();
|
||||||
let identifier = cg::to_css_identifier(variant.ast().ident.as_ref());
|
let identifier = cg::to_css_identifier(variant.ast().ident.as_ref());
|
||||||
|
@ -83,7 +96,7 @@ fn derive_variant_arm(
|
||||||
::std::fmt::Write::write_str(dest, #keyword)
|
::std::fmt::Write::write_str(dest, #keyword)
|
||||||
}
|
}
|
||||||
} else if !bindings.is_empty() {
|
} else if !bindings.is_empty() {
|
||||||
derive_variant_fields_expr(bindings, where_clause, separator)
|
derive_variant_fields_expr(bindings, generics, separator)
|
||||||
} else {
|
} else {
|
||||||
quote! {
|
quote! {
|
||||||
::std::fmt::Write::write_str(dest, #identifier)
|
::std::fmt::Write::write_str(dest, #identifier)
|
||||||
|
@ -109,7 +122,7 @@ fn derive_variant_arm(
|
||||||
|
|
||||||
fn derive_variant_fields_expr(
|
fn derive_variant_fields_expr(
|
||||||
bindings: &[BindingInfo],
|
bindings: &[BindingInfo],
|
||||||
where_clause: &mut WhereClause,
|
where_clause: &mut Option<WhereClause>,
|
||||||
separator: &str,
|
separator: &str,
|
||||||
) -> Tokens {
|
) -> Tokens {
|
||||||
let mut iter = bindings.iter().filter_map(|binding| {
|
let mut iter = bindings.iter().filter_map(|binding| {
|
||||||
|
@ -125,8 +138,9 @@ fn derive_variant_fields_expr(
|
||||||
None => return quote! { Ok(()) },
|
None => return quote! { Ok(()) },
|
||||||
};
|
};
|
||||||
if !attrs.iterable && iter.peek().is_none() {
|
if !attrs.iterable && iter.peek().is_none() {
|
||||||
if !attrs.ignore_bound {
|
if attrs.field_bound {
|
||||||
where_clause.add_trait_bound(&first.ast().ty);
|
let ty = &first.ast().ty;
|
||||||
|
cg::add_predicate(where_clause, parse_quote!(#ty: ::style_traits::ToCss));
|
||||||
}
|
}
|
||||||
let mut expr = quote! { ::style_traits::ToCss::to_css(#first, dest) };
|
let mut expr = quote! { ::style_traits::ToCss::to_css(#first, dest) };
|
||||||
if let Some(condition) = attrs.skip_if {
|
if let Some(condition) = attrs.skip_if {
|
||||||
|
@ -154,7 +168,7 @@ fn derive_variant_fields_expr(
|
||||||
fn derive_single_field_expr(
|
fn derive_single_field_expr(
|
||||||
field: &BindingInfo,
|
field: &BindingInfo,
|
||||||
attrs: CssFieldAttrs,
|
attrs: CssFieldAttrs,
|
||||||
where_clause: &mut WhereClause,
|
where_clause: &mut Option<WhereClause>,
|
||||||
) -> Tokens {
|
) -> Tokens {
|
||||||
let mut expr = if attrs.iterable {
|
let mut expr = if attrs.iterable {
|
||||||
if let Some(if_empty) = attrs.if_empty {
|
if let Some(if_empty) = attrs.if_empty {
|
||||||
|
@ -177,8 +191,9 @@ fn derive_single_field_expr(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if !attrs.ignore_bound {
|
if attrs.field_bound {
|
||||||
where_clause.add_trait_bound(&field.ast().ty);
|
let ty = &field.ast().ty;
|
||||||
|
cg::add_predicate(where_clause, parse_quote!(#ty: ::style_traits::ToCss));
|
||||||
}
|
}
|
||||||
quote! { writer.item(#field)?; }
|
quote! { writer.item(#field)?; }
|
||||||
};
|
};
|
||||||
|
@ -218,7 +233,7 @@ pub struct CssVariantAttrs {
|
||||||
#[derive(Default, FromField)]
|
#[derive(Default, FromField)]
|
||||||
struct CssFieldAttrs {
|
struct CssFieldAttrs {
|
||||||
if_empty: Option<String>,
|
if_empty: Option<String>,
|
||||||
ignore_bound: bool,
|
field_bound: bool,
|
||||||
iterable: bool,
|
iterable: bool,
|
||||||
skip: bool,
|
skip: bool,
|
||||||
skip_if: Option<Path>,
|
skip_if: Option<Path>,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue