Skip to content

Commit 4f0b24a

Browse files
author
bors-servo
authored
Auto merge of servo#16960 - servo:derive-all-the-things, r=emilio
Derive HasViewportPercentage 🍷 <!-- Reviewable:start --> This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/16960) <!-- Reviewable:end -->
2 parents 3ca7f4f + 00bfc6b commit 4f0b24a

40 files changed

+258
-574
lines changed

Cargo.lock

+15-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/style/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ serde_derive = {version = "0.9", optional = true}
6363
servo_atoms = {path = "../atoms", optional = true}
6464
servo_config = {path = "../config", optional = true}
6565
smallvec = "0.3.3"
66+
style_derive = {path = "../style_derive"}
6667
style_traits = {path = "../style_traits"}
6768
servo_url = {path = "../url", optional = true}
6869
time = "0.1"

components/style/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ extern crate selectors;
7979
#[cfg(feature = "servo")] extern crate servo_url;
8080
extern crate smallvec;
8181
#[macro_use]
82+
extern crate style_derive;
83+
#[macro_use]
8284
extern crate style_traits;
8385
extern crate time;
8486
#[allow(unused_extern_crates)]

components/style/macros.rs

-13
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,6 @@ macro_rules! define_numbered_css_keyword_enum {
3838
}
3939
}
4040

41-
/// A macro used to implement HasViewportPercentage trait
42-
/// for a given type that may never contain viewport units.
43-
macro_rules! no_viewport_percentage {
44-
($name: ident) => {
45-
impl $crate::values::HasViewportPercentage for $name {
46-
#[inline]
47-
fn has_viewport_percentage(&self) -> bool {
48-
false
49-
}
50-
}
51-
};
52-
}
53-
5441
/// A macro for implementing `ComputedValueAsSpecified`, `Parse`
5542
/// and `HasViewportPercentage` traits for the enums defined
5643
/// using `define_css_keyword_enum` macro.

components/style/properties/helpers.mako.rs

+26-12
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@
1313
<%def name="predefined_type_inner(name, type, initial_value, parse_method)">
1414
#[allow(unused_imports)]
1515
use app_units::Au;
16+
#[allow(unused_imports)]
1617
use cssparser::{Color as CSSParserColor, RGBA};
18+
#[allow(unused_imports)]
1719
use values::specified::AllowQuirks;
20+
#[allow(unused_imports)]
1821
use smallvec::SmallVec;
1922
pub use values::specified::${type} as SpecifiedValue;
2023
pub mod computed_value {
@@ -78,22 +81,22 @@
7881
% if not gecko_only:
7982
use smallvec::SmallVec;
8083
use std::fmt;
84+
#[allow(unused_imports)]
8185
use values::HasViewportPercentage;
8286
use style_traits::ToCss;
8387

84-
impl HasViewportPercentage for SpecifiedValue {
85-
fn has_viewport_percentage(&self) -> bool {
86-
let &SpecifiedValue(ref vec) = self;
87-
vec.iter().any(|ref x| x.has_viewport_percentage())
88-
}
89-
}
90-
9188
pub mod single_value {
89+
#[allow(unused_imports)]
9290
use cssparser::Parser;
91+
#[allow(unused_imports)]
9392
use parser::{Parse, ParserContext};
93+
#[allow(unused_imports)]
9494
use properties::ShorthandId;
95+
#[allow(unused_imports)]
9596
use values::computed::{Context, ToComputedValue};
97+
#[allow(unused_imports)]
9698
use values::{computed, specified};
99+
#[allow(unused_imports)]
97100
use values::{Auto, Either, None_, Normal};
98101
${caller.body()}
99102
}
@@ -168,7 +171,7 @@
168171
}
169172

170173
/// The specified value of ${name}.
171-
#[derive(Debug, Clone, PartialEq)]
174+
#[derive(Clone, Debug, HasViewportPercentage, PartialEq)]
172175
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
173176
pub struct SpecifiedValue(pub Vec<single_value::SpecifiedValue>);
174177

@@ -205,6 +208,7 @@
205208
}
206209

207210
pub fn parse(context: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue, ()> {
211+
#[allow(unused_imports)]
208212
use parser::parse_space_or_comma_separated;
209213

210214
<%
@@ -260,22 +264,35 @@
260264
%>
261265
/// ${property.spec}
262266
pub mod ${property.ident} {
263-
#![allow(unused_imports)]
264267
% if not property.derived_from:
268+
#[allow(unused_imports)]
265269
use cssparser::Parser;
270+
#[allow(unused_imports)]
266271
use parser::{Parse, ParserContext};
272+
#[allow(unused_imports)]
267273
use properties::{UnparsedValue, ShorthandId};
268274
% endif
275+
#[allow(unused_imports)]
269276
use values::{Auto, Either, None_, Normal};
277+
#[allow(unused_imports)]
270278
use cascade_info::CascadeInfo;
279+
#[allow(unused_imports)]
271280
use error_reporting::ParseErrorReporter;
281+
#[allow(unused_imports)]
272282
use properties::longhands;
283+
#[allow(unused_imports)]
273284
use properties::{DeclaredValue, LonghandId, LonghandIdSet};
285+
#[allow(unused_imports)]
274286
use properties::{CSSWideKeyword, ComputedValues, PropertyDeclaration};
287+
#[allow(unused_imports)]
275288
use properties::style_structs;
289+
#[allow(unused_imports)]
276290
use stylearc::Arc;
291+
#[allow(unused_imports)]
277292
use values::computed::{Context, ToComputedValue};
293+
#[allow(unused_imports)]
278294
use values::{computed, generics, specified};
295+
#[allow(unused_imports)]
279296
use Atom;
280297
${caller.body()}
281298
#[allow(unused_variables)]
@@ -461,7 +478,6 @@
461478
keyword = keyword=Keyword(name, values, **keyword_kwargs)
462479
%>
463480
<%call expr="longhand(name, keyword=Keyword(name, values, **keyword_kwargs), **kwargs)">
464-
use values::HasViewportPercentage;
465481
use properties::longhands::system_font::SystemFont;
466482
use std::fmt;
467483
use style_traits::ToCss;
@@ -583,7 +599,6 @@
583599
impl ComputedValueAsSpecified for SpecifiedValue {}
584600
% endif
585601

586-
use values::HasViewportPercentage;
587602
no_viewport_percentage!(SpecifiedValue);
588603
</%call>
589604
</%def>
@@ -745,7 +760,6 @@
745760
% if shorthand:
746761
/// ${shorthand.spec}
747762
pub mod ${shorthand.ident} {
748-
#[allow(unused_imports)]
749763
use cssparser::Parser;
750764
use parser::ParserContext;
751765
use properties::{PropertyDeclaration, SourcePropertyDeclaration, MaybeBoxed};

components/style/properties/longhand/background.mako.rs

+2-21
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ ${helpers.predefined_type("background-image", "ImageLayer",
3434
spec="https://drafts.csswg.org/css-backgrounds/#the-background-repeat">
3535
use std::fmt;
3636
use style_traits::ToCss;
37-
use values::HasViewportPercentage;
3837

3938
define_css_keyword_enum!(RepeatKeyword:
4039
"repeat" => Repeat,
@@ -163,11 +162,8 @@ ${helpers.single_keyword("background-origin",
163162

164163
<%helpers:vector_longhand name="background-size" animation_value_type="ComputedValue" extra_prefixes="webkit"
165164
spec="https://drafts.csswg.org/css-backgrounds/#the-background-size">
166-
use cssparser::Token;
167-
use std::ascii::AsciiExt;
168165
use std::fmt;
169166
use style_traits::ToCss;
170-
use values::HasViewportPercentage;
171167

172168
#[allow(missing_docs)]
173169
pub mod computed_value {
@@ -236,13 +232,7 @@ ${helpers.single_keyword("background-origin",
236232
}
237233
}
238234

239-
impl HasViewportPercentage for ExplicitSize {
240-
fn has_viewport_percentage(&self) -> bool {
241-
return self.width.has_viewport_percentage() || self.height.has_viewport_percentage();
242-
}
243-
}
244-
245-
#[derive(Clone, PartialEq, Debug)]
235+
#[derive(Clone, Debug, HasViewportPercentage, PartialEq)]
246236
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
247237
#[allow(missing_docs)]
248238
pub struct ExplicitSize {
@@ -266,16 +256,7 @@ ${helpers.single_keyword("background-origin",
266256
}
267257
}
268258

269-
impl HasViewportPercentage for SpecifiedValue {
270-
fn has_viewport_percentage(&self) -> bool {
271-
match *self {
272-
SpecifiedValue::Explicit(ref explicit_size) => explicit_size.has_viewport_percentage(),
273-
_ => false
274-
}
275-
}
276-
}
277-
278-
#[derive(Clone, PartialEq, Debug)]
259+
#[derive(Clone, Debug, HasViewportPercentage, PartialEq)]
279260
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
280261
pub enum SpecifiedValue {
281262
Explicit(ExplicitSize),

components/style/properties/longhand/border.mako.rs

+3-34
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ ${helpers.gecko_keyword_conversion(Keyword('border-style',
6262
products="gecko">
6363
use std::fmt;
6464
use style_traits::ToCss;
65-
use values::HasViewportPercentage;
6665
use values::specified::CSSColor;
6766
no_viewport_percentage!(SpecifiedValue);
6867

@@ -205,20 +204,8 @@ ${helpers.predefined_type("border-image-source", "ImageLayer",
205204
spec="https://drafts.csswg.org/css-backgrounds/#border-image-outset">
206205
use std::fmt;
207206
use style_traits::ToCss;
208-
use values::HasViewportPercentage;
209207
use values::specified::{LengthOrNumber, Number};
210208

211-
impl HasViewportPercentage for SpecifiedValue {
212-
fn has_viewport_percentage(&self) -> bool {
213-
let mut viewport_percentage = false;
214-
for value in self.0.iter() {
215-
let vp = value.has_viewport_percentage();
216-
viewport_percentage = vp || viewport_percentage;
217-
}
218-
viewport_percentage
219-
}
220-
}
221-
222209
pub mod computed_value {
223210
use values::computed::LengthOrNumber;
224211
#[derive(Debug, Clone, PartialEq)]
@@ -227,7 +214,7 @@ ${helpers.predefined_type("border-image-source", "ImageLayer",
227214
pub LengthOrNumber, pub LengthOrNumber);
228215
}
229216

230-
#[derive(Debug, Clone, PartialEq)]
217+
#[derive(Clone, Debug, HasViewportPercentage, PartialEq)]
231218
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
232219
pub struct SpecifiedValue(pub Vec<LengthOrNumber>);
233220

@@ -321,13 +308,11 @@ ${helpers.predefined_type("border-image-source", "ImageLayer",
321308
spec="https://drafts.csswg.org/css-backgrounds/#border-image-repeat">
322309
use std::fmt;
323310
use style_traits::ToCss;
324-
use values::HasViewportPercentage;
325311

326312
no_viewport_percentage!(SpecifiedValue);
327313

328314
pub mod computed_value {
329315
pub use super::RepeatKeyword;
330-
use values::computed;
331316

332317
#[derive(Debug, Clone, PartialEq)]
333318
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
@@ -399,23 +384,8 @@ ${helpers.predefined_type("border-image-source", "ImageLayer",
399384
spec="https://drafts.csswg.org/css-backgrounds/#border-image-width">
400385
use std::fmt;
401386
use style_traits::ToCss;
402-
use values::HasViewportPercentage;
403387
use values::specified::{LengthOrPercentage, Number};
404388

405-
impl HasViewportPercentage for SpecifiedValue {
406-
fn has_viewport_percentage(&self) -> bool {
407-
let mut viewport_percentage = false;
408-
for value in self.0.clone() {
409-
let vp = match value {
410-
SingleSpecifiedValue::LengthOrPercentage(len) => len.has_viewport_percentage(),
411-
_ => false,
412-
};
413-
viewport_percentage = vp || viewport_percentage;
414-
}
415-
viewport_percentage
416-
}
417-
}
418-
419389
pub mod computed_value {
420390
use values::computed::{LengthOrPercentage, Number};
421391
#[derive(Debug, Clone, PartialEq)]
@@ -432,7 +402,7 @@ ${helpers.predefined_type("border-image-source", "ImageLayer",
432402
}
433403
}
434404

435-
#[derive(Debug, Clone, PartialEq)]
405+
#[derive(Clone, Debug, HasViewportPercentage, PartialEq)]
436406
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
437407
pub struct SpecifiedValue(pub Vec<SingleSpecifiedValue>);
438408

@@ -458,7 +428,7 @@ ${helpers.predefined_type("border-image-source", "ImageLayer",
458428
}
459429
}
460430

461-
#[derive(Debug, Clone, PartialEq)]
431+
#[derive(Clone, Debug, HasViewportPercentage, PartialEq)]
462432
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
463433
pub enum SingleSpecifiedValue {
464434
LengthOrPercentage(LengthOrPercentage),
@@ -599,7 +569,6 @@ ${helpers.predefined_type("border-image-source", "ImageLayer",
599569
spec="https://drafts.csswg.org/css-backgrounds/#border-image-slice">
600570
use std::fmt;
601571
use style_traits::ToCss;
602-
use values::HasViewportPercentage;
603572
use values::computed::NumberOrPercentage as ComputedNumberOrPercentage;
604573
use values::specified::{NumberOrPercentage, Percentage};
605574

0 commit comments

Comments
 (0)