Bug 1322780 - Part 1: Use cbindgen for ExtremumLength. r=emilio
authorBoris Chiou <boris.chiou@gmail.com>
Tue, 18 Dec 2018 18:47:35 +0000
changeset 451213 1edf365b731a68dae4543ad652c123d32c537d4c
parent 451212 41e4eb2f9e476f279defec74ef328f2b707b0b65
child 451214 3511904f12cf4e71e3e2baaa20c1845b936b3cbe
push id74944
push userboris.chiou@gmail.com
push dateTue, 18 Dec 2018 21:44:59 +0000
treeherderautoland@1ecaff8ff57d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersemilio
bugs1322780
milestone66.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1322780 - Part 1: Use cbindgen for ExtremumLength. r=emilio ExtremumLength is the keyword type for css sizing properties, so we could use cbindgen. In Gecko, we use nsStyleCoord to store the sizing properties, and use integer values to check the enum values, so I keep the macros in nsStyleConsts. Even though we need to convert the enum type into integer, we still have benefits to reduce the complexity of converting Rust into C++, and leave the simplified mappings in C++ for better readability. Differential Revision: https://phabricator.services.mozilla.com/D7535
layout/style/ServoBindings.toml
layout/style/nsStyleConsts.h
servo/components/style/cbindgen.toml
servo/components/style/gecko/values.rs
servo/components/style/values/computed/length.rs
--- a/layout/style/ServoBindings.toml
+++ b/layout/style/ServoBindings.toml
@@ -386,16 +386,17 @@ opaque-types = [
 cbindgen-types = [
     { gecko = "StyleAppearance", servo = "values::specified::Appearance" },
     { gecko = "StyleComputedFontStretchRange", servo = "font_face::ComputedFontStretchRange" },
     { gecko = "StyleComputedFontStyleDescriptor", servo = "font_face::ComputedFontStyleDescriptor" },
     { gecko = "StyleComputedFontWeightRange", servo = "font_face::ComputedFontWeightRange" },
     { gecko = "StyleComputedTimingFunction", servo = "values::computed::easing::TimingFunction" },
     { gecko = "StyleDisplay", servo = "values::specified::Display" },
     { gecko = "StyleDisplayMode", servo = "gecko::media_features::DisplayMode" },
+    { gecko = "StyleExtremumLength", servo = "values::computed::length::ExtremumLength" },
     { gecko = "StyleFillRule", servo = "values::generics::basic_shape::FillRule" },
     { gecko = "StyleFontDisplay", servo = "font_face::FontDisplay" },
     { gecko = "StyleFontFaceSourceListComponent", servo = "font_face::FontFaceSourceListComponent" },
     { gecko = "StyleFontLanguageOverride", servo = "values::computed::font::FontLanguageOverride" },
     { gecko = "StylePathCommand", servo = "values::specified::svg_path::PathCommand" },
     { gecko = "StyleUnicodeRange", servo = "cssparser::UnicodeRange" },
     { gecko = "StyleOverflowWrap", servo = "values::computed::OverflowWrap" },
     { gecko = "StyleUserSelect", servo = "values::computed::UserSelect" },
--- a/layout/style/nsStyleConsts.h
+++ b/layout/style/nsStyleConsts.h
@@ -512,26 +512,35 @@ enum class StyleGridTrackBreadth : uint8
 #define NS_MATHML_MATHVARIANT_LOOPED 17
 #define NS_MATHML_MATHVARIANT_STRETCHED 18
 
 // See nsStyleFont::mMathDisplay
 #define NS_MATHML_DISPLAYSTYLE_INLINE 0
 #define NS_MATHML_DISPLAYSTYLE_BLOCK 1
 
 // See nsStylePosition::mWidth, mMinWidth, mMaxWidth
-#define NS_STYLE_WIDTH_MAX_CONTENT 0
-#define NS_STYLE_WIDTH_MIN_CONTENT 1
-#define NS_STYLE_WIDTH_FIT_CONTENT 2
-#define NS_STYLE_WIDTH_AVAILABLE 3
+#define NS_STYLE_WIDTH_MAX_CONTENT \
+  ((uint8_t)mozilla::StyleExtremumLength::MozMaxContent)
+#define NS_STYLE_WIDTH_MIN_CONTENT \
+  ((uint8_t)mozilla::StyleExtremumLength::MozMinContent)
+#define NS_STYLE_WIDTH_FIT_CONTENT \
+  ((uint8_t)mozilla::StyleExtremumLength::MozFitContent)
+#define NS_STYLE_WIDTH_AVAILABLE \
+  ((uint8_t)mozilla::StyleExtremumLength::MozAvailable)
 // The 'content' keyword is only valid for 'flex-basis' (not for 'width').  But
 // aside from that, the 'flex-basis' property accepts exactly the same values
 // as 'width'. So I'm listing this one 'flex-basis'-specific enumerated value
 // alongside the 'width' ones, to be sure we don't accidentally overload this
 // numeric value with two different meanings if new 'width' keywords are added.
 #define NS_STYLE_FLEX_BASIS_CONTENT 4
+static_assert(NS_STYLE_FLEX_BASIS_CONTENT != NS_STYLE_WIDTH_MAX_CONTENT &&
+                  NS_STYLE_FLEX_BASIS_CONTENT != NS_STYLE_WIDTH_MIN_CONTENT &&
+                  NS_STYLE_FLEX_BASIS_CONTENT != NS_STYLE_WIDTH_FIT_CONTENT &&
+                  NS_STYLE_FLEX_BASIS_CONTENT != NS_STYLE_WIDTH_AVAILABLE,
+              "Should use different enum value for flex basis content");
 
 // See nsStyleDisplay.mPosition
 #define NS_STYLE_POSITION_STATIC 0
 #define NS_STYLE_POSITION_RELATIVE 1
 #define NS_STYLE_POSITION_ABSOLUTE 2
 #define NS_STYLE_POSITION_FIXED 3
 #define NS_STYLE_POSITION_STICKY 4
 
--- a/servo/components/style/cbindgen.toml
+++ b/servo/components/style/cbindgen.toml
@@ -45,16 +45,17 @@ include = [
   "BorderStyle",
   "OutlineStyle",
   "ComputedFontStretchRange",
   "ComputedFontStyleDescriptor",
   "ComputedFontWeightRange",
   "ComputedTimingFunction",
   "Display",
   "DisplayMode",
+  "ExtremumLength",
   "FillRule",
   "FontDisplay",
   "FontFaceSourceListComponent",
   "FontLanguageOverride",
   "OverflowWrap",
   "TimingFunction",
   "PathCommand",
   "UnicodeRange",
--- a/servo/components/style/gecko/values.rs
+++ b/servo/components/style/gecko/values.rs
@@ -357,50 +357,23 @@ impl GeckoStyleCoordConvertible for Norm
         } else {
             None
         }
     }
 }
 
 impl GeckoStyleCoordConvertible for ExtremumLength {
     fn to_gecko_style_coord<T: CoordDataMut>(&self, coord: &mut T) {
-        use crate::gecko_bindings::structs::{
-            NS_STYLE_WIDTH_AVAILABLE, NS_STYLE_WIDTH_FIT_CONTENT,
-        };
-        use crate::gecko_bindings::structs::{
-            NS_STYLE_WIDTH_MAX_CONTENT, NS_STYLE_WIDTH_MIN_CONTENT,
-        };
-        coord.set_value(CoordDataValue::Enumerated(match *self {
-            ExtremumLength::MozMaxContent => NS_STYLE_WIDTH_MAX_CONTENT,
-            ExtremumLength::MozMinContent => NS_STYLE_WIDTH_MIN_CONTENT,
-            ExtremumLength::MozFitContent => NS_STYLE_WIDTH_FIT_CONTENT,
-            ExtremumLength::MozAvailable => NS_STYLE_WIDTH_AVAILABLE,
-        }))
+        coord.set_value(CoordDataValue::Enumerated(*self as u32));
     }
 
     fn from_gecko_style_coord<T: CoordData>(coord: &T) -> Option<Self> {
-        use crate::gecko_bindings::structs::{
-            NS_STYLE_WIDTH_AVAILABLE, NS_STYLE_WIDTH_FIT_CONTENT,
-        };
-        use crate::gecko_bindings::structs::{
-            NS_STYLE_WIDTH_MAX_CONTENT, NS_STYLE_WIDTH_MIN_CONTENT,
-        };
+        use num_traits::FromPrimitive;
         match coord.as_value() {
-            CoordDataValue::Enumerated(NS_STYLE_WIDTH_MAX_CONTENT) => {
-                Some(ExtremumLength::MozMaxContent)
-            },
-            CoordDataValue::Enumerated(NS_STYLE_WIDTH_MIN_CONTENT) => {
-                Some(ExtremumLength::MozMinContent)
-            },
-            CoordDataValue::Enumerated(NS_STYLE_WIDTH_FIT_CONTENT) => {
-                Some(ExtremumLength::MozFitContent)
-            },
-            CoordDataValue::Enumerated(NS_STYLE_WIDTH_AVAILABLE) => {
-                Some(ExtremumLength::MozAvailable)
-            },
+            CoordDataValue::Enumerated(v) => ExtremumLength::from_u32(v),
             _ => None,
         }
     }
 }
 
 impl GeckoStyleCoordConvertible for ComputedMozLength {
     fn to_gecko_style_coord<T: CoordDataMut>(&self, coord: &mut T) {
         match *self {
--- a/servo/components/style/values/computed/length.rs
+++ b/servo/components/style/values/computed/length.rs
@@ -970,23 +970,25 @@ pub type NonNegativeLengthOrPercentageOr
 /// block-size, and inline-size.
 #[allow(missing_docs)]
 #[cfg_attr(feature = "servo", derive(Deserialize, Serialize))]
 #[derive(
     Clone,
     Copy,
     Debug,
     Eq,
+    FromPrimitive,
     MallocSizeOf,
     Parse,
     PartialEq,
     SpecifiedValueInfo,
     ToComputedValue,
     ToCss,
 )]
+#[repr(u8)]
 pub enum ExtremumLength {
     MozMaxContent,
     MozMinContent,
     MozFitContent,
     MozAvailable,
 }
 
 /// A computed value for `min-width`, `min-height`, `width` or `height` property.