diff options
Diffstat (limited to 'servo/components/style/stylesheets/margin_rule.rs')
-rw-r--r-- | servo/components/style/stylesheets/margin_rule.rs | 42 |
1 files changed, 35 insertions, 7 deletions
diff --git a/servo/components/style/stylesheets/margin_rule.rs b/servo/components/style/stylesheets/margin_rule.rs index ab46283151..19928c04ba 100644 --- a/servo/components/style/stylesheets/margin_rule.rs +++ b/servo/components/style/stylesheets/margin_rule.rs @@ -21,19 +21,21 @@ macro_rules! margin_rule_types { /// [`@margin`][margin] rule names. /// /// https://drafts.csswg.org/css-page-3/#margin-at-rules - #[derive(Clone, Copy, Debug, Eq, MallocSizeOf, PartialEq, ToShmem)] + #[derive(Clone, Copy, Eq, MallocSizeOf, PartialEq, ToShmem)] #[repr(u8)] pub enum MarginRuleType { $($(#[$($meta)+])* $id,)+ } + /// All [`@margin`][margin] rule names, with a preceding '@'. + /// + /// This array lets us have just one single memory region used for + /// to_str, name, and the Debug implementation. + const MARGIN_RULE_AT_NAMES:&[&'static str] = &[ + $( concat!('@', $val), )+ + ]; + impl MarginRuleType { - #[inline] - fn to_str(&self) -> &'static str { - match *self { - $(MarginRuleType::$id => concat!('@', $val),)+ - } - } /// Matches the rule type for this name. This does not expect a /// leading '@'. pub fn match_name(name: &str) -> Option<Self> { @@ -113,6 +115,27 @@ margin_rule_types! { RightBottom => "right-bottom", } +impl MarginRuleType { + #[inline] + fn to_str(&self) -> &'static str { + &MARGIN_RULE_AT_NAMES[*self as usize] + } + #[inline] + fn name(&self) -> &'static str { + // Use the at-name array, skipping the first character to get + // the name without the @ sign. + &MARGIN_RULE_AT_NAMES[*self as usize][1..] + } +} + +// Implement Debug manually so that it will share the same string memory as +// MarginRuleType::name and MarginRuleType::to_str. +impl fmt::Debug for MarginRuleType { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { + f.write_str(self.name()) + } +} + /// A [`@margin`][margin] rule. /// /// [margin]: https://drafts.csswg.org/css-page-3/#margin-at-rules @@ -134,6 +157,11 @@ impl MarginRule { self.block.unconditional_shallow_size_of(ops) + self.block.read_with(guard).size_of(ops) } + /// Gets the name for this margin rule. + #[inline] + pub fn name(&self) -> &'static str { + self.rule_type.name() + } } impl ToCssWithGuard for MarginRule { |