From 79775541f28ee976c69ac79aedc4b83680d396fa Mon Sep 17 00:00:00 2001 From: Anthony Ramine Date: Fri, 9 Feb 2018 22:34:59 +0100 Subject: [PATCH] Make AnimationValue have the same variants as PropertyDeclaration By making AnimationValue have the same representation as PropertyDeclaration and Void variants for non-animatable properties, we know by constructions that all properties have the same discriminant in both. --- Cargo.lock | 2 + components/malloc_size_of/Cargo.toml | 1 + components/malloc_size_of/lib.rs | 9 +++ components/style/Cargo.toml | 1 + components/style/lib.rs | 1 + .../helpers/animated_properties.mako.rs | 63 +++++++++++-------- .../style/properties/properties.mako.rs | 16 ++--- 7 files changed, 58 insertions(+), 35 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9b07d56bd6a..7cad7427505 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1562,6 +1562,7 @@ dependencies = [ "smallvec 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "string_cache 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", "url 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)", + "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "webrender_api 0.57.0 (git+https://github.com/servo/webrender)", "xml5ever 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -2926,6 +2927,7 @@ dependencies = [ "uluru 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "unicode-bidi 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "unicode-segmentation 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "walkdir 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)", ] diff --git a/components/malloc_size_of/Cargo.toml b/components/malloc_size_of/Cargo.toml index 9ff69d28945..2a4ab316f4b 100644 --- a/components/malloc_size_of/Cargo.toml +++ b/components/malloc_size_of/Cargo.toml @@ -26,3 +26,4 @@ string_cache = { version = "0.7", optional = true } url = { version = "1.2", optional = true } webrender_api = { git = "https://github.com/servo/webrender", features = ["ipc"], optional = true } xml5ever = { version = "0.12", optional = true } +void = "1.0.2" diff --git a/components/malloc_size_of/lib.rs b/components/malloc_size_of/lib.rs index 074ebd6b675..3801669ab5d 100644 --- a/components/malloc_size_of/lib.rs +++ b/components/malloc_size_of/lib.rs @@ -60,6 +60,7 @@ extern crate string_cache; extern crate url; #[cfg(feature = "webrender_api")] extern crate webrender_api; +extern crate void; #[cfg(feature = "servo")] extern crate xml5ever; @@ -67,6 +68,7 @@ use std::hash::{BuildHasher, Hash}; use std::mem::size_of; use std::ops::Range; use std::os::raw::c_void; +use void::Void; /// A C function that takes a pointer to a heap allocation and returns its size. type VoidPtrToSizeFn = unsafe extern "C" fn(ptr: *const c_void) -> usize; @@ -746,6 +748,13 @@ impl MallocSizeOf for selectors::parser::AncestorHashes { } } +impl MallocSizeOf for Void { + #[inline] + fn size_of(&self, _ops: &mut MallocSizeOfOps) -> usize { + void::unreachable(*self) + } +} + #[cfg(feature = "servo")] impl MallocSizeOf for string_cache::Atom { fn size_of(&self, _ops: &mut MallocSizeOfOps) -> usize { diff --git a/components/style/Cargo.toml b/components/style/Cargo.toml index ac7d71f6478..915308030b6 100644 --- a/components/style/Cargo.toml +++ b/components/style/Cargo.toml @@ -70,6 +70,7 @@ time = "0.1" uluru = "0.2" unicode-bidi = "0.3" unicode-segmentation = "1.0" +void = "1.0.2" [target.'cfg(windows)'.dependencies] kernel32-sys = "0.2" diff --git a/components/style/lib.rs b/components/style/lib.rs index cbe8a12ee02..b7d062b3bdb 100644 --- a/components/style/lib.rs +++ b/components/style/lib.rs @@ -78,6 +78,7 @@ extern crate uluru; extern crate unicode_bidi; #[allow(unused_extern_crates)] extern crate unicode_segmentation; +extern crate void; #[macro_use] mod macros; diff --git a/components/style/properties/helpers/animated_properties.mako.rs b/components/style/properties/helpers/animated_properties.mako.rs index af32c77a64e..703b6be9fe5 100644 --- a/components/style/properties/helpers/animated_properties.mako.rs +++ b/components/style/properties/helpers/animated_properties.mako.rs @@ -58,6 +58,7 @@ use values::generics::position as generic_position; use values::generics::svg::{SVGLength, SvgLengthOrPercentageOrNumber, SVGPaint}; use values::generics::svg::{SVGPaintKind, SVGStrokeDashArray, SVGOpacity}; use values::specified::font::FontTag; +use void::{self, Void}; /// pub trait RepeatableListAnimatable: Animate {} @@ -343,16 +344,20 @@ unsafe impl HasSimpleFFI for AnimationValueMap {} /// this (is a similar path to that of PropertyDeclaration). #[derive(Clone, Debug, PartialEq)] #[cfg_attr(feature = "servo", derive(MallocSizeOf))] +#[repr(u16)] pub enum AnimationValue { % for prop in data.longhands: - % if prop.animatable: - /// ${prop.name} - % if prop.is_animatable_with_computed_value: - ${prop.camel_case}(longhands::${prop.ident}::computed_value::T), - % else: - ${prop.camel_case}(::AnimatedValue), - % endif - % endif + % if prop.animatable: + /// `${prop.name}` + % if prop.is_animatable_with_computed_value: + ${prop.camel_case}(longhands::${prop.ident}::computed_value::T), + % else: + ${prop.camel_case}(::AnimatedValue), + % endif + % else: + /// `${prop.name}` (not animatable) + ${prop.camel_case}(Void), + % endif % endfor } @@ -362,7 +367,9 @@ impl AnimationValue { match *self { % for prop in data.longhands: % if prop.animatable: - AnimationValue::${prop.camel_case}(..) => LonghandId::${prop.camel_case}, + AnimationValue::${prop.camel_case}(..) => LonghandId::${prop.camel_case}, + % else: + AnimationValue::${prop.camel_case}(void) => void::unreachable(void), % endif % endfor } @@ -374,24 +381,26 @@ impl AnimationValue { use properties::longhands; match *self { % for prop in data.longhands: - % if prop.animatable: - AnimationValue::${prop.camel_case}(ref from) => { - PropertyDeclaration::${prop.camel_case}( - % if prop.boxed: - Box::new( - % endif - longhands::${prop.ident}::SpecifiedValue::from_computed_value( - % if prop.is_animatable_with_computed_value: - from - % else: - &ToAnimatedValue::from_animated_value(from.clone()) - % endif - )) - % if prop.boxed: - ) - % endif - } - % endif + % if prop.animatable: + AnimationValue::${prop.camel_case}(ref from) => { + PropertyDeclaration::${prop.camel_case}( + % if prop.boxed: + Box::new( + % endif + longhands::${prop.ident}::SpecifiedValue::from_computed_value( + % if prop.is_animatable_with_computed_value: + from + % else: + &ToAnimatedValue::from_animated_value(from.clone()) + % endif + )) + % if prop.boxed: + ) + % endif + } + % else: + AnimationValue::${prop.camel_case}(void) => void::unreachable(void), + % endif % endfor } } diff --git a/components/style/properties/properties.mako.rs b/components/style/properties/properties.mako.rs index 01d32eb0e49..254bfc254c1 100644 --- a/components/style/properties/properties.mako.rs +++ b/components/style/properties/properties.mako.rs @@ -195,14 +195,6 @@ pub mod shorthands { %> } -/// A module with all the code related to animated properties. -/// -/// This needs to be "included" by mako at least after all longhand modules, -/// given they populate the global data. -pub mod animated_properties { - <%include file="/helpers/animated_properties.mako.rs" /> -} - <% from itertools import groupby @@ -413,6 +405,14 @@ impl PropertyDeclaration { } } +/// A module with all the code related to animated properties. +/// +/// This needs to be "included" by mako at least after all longhand modules, +/// given they populate the global data. +pub mod animated_properties { + <%include file="/helpers/animated_properties.mako.rs" /> +} + /// A longhand or shorthand porperty #[derive(Clone, Copy, Debug)] pub struct NonCustomPropertyId(usize);