Bug 1331213: Add sugar for nsCSSValue and nsCSSValue::Array. r?heycam r?Manishearth draft
authorEmilio Cobos Álvarez <emilio@crisal.io>
Sun, 15 Jan 2017 21:04:28 +0100
changeset 461260 191cf9d56945ad8e1172f09712f0927f97adcc8a
parent 461259 f21fc9b92c2ef7bb92868a63ceb0e35363ed4ce2
child 461261 9384b11082049358489ef35407219c23ae158357
push id41620
push userbmo:emilio+bugs@crisal.io
push dateMon, 16 Jan 2017 09:15:13 +0000
reviewersheycam, Manishearth
bugs1331213
milestone53.0a1
Bug 1331213: Add sugar for nsCSSValue and nsCSSValue::Array. r?heycam r?Manishearth I will use this soon to implement the media query evaluation code. Please review carefully. MozReview-Commit-ID: HXelawXBfH8 Signed-off-by: Emilio Cobos Álvarez <emilio@crisal.io>
servo/components/style/gecko_bindings/sugar/mod.rs
servo/components/style/gecko_bindings/sugar/ns_css_value.rs
--- a/servo/components/style/gecko_bindings/sugar/mod.rs
+++ b/servo/components/style/gecko_bindings/sugar/mod.rs
@@ -1,15 +1,16 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * 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/. */
 
 //! Rust sugar and convenience methods for Gecko types.
 
 mod ns_com_ptr;
 mod ns_css_shadow_array;
+mod ns_css_value;
 mod ns_style_auto_array;
 pub mod ns_style_coord;
 mod ns_t_array;
 mod ns_timing_function;
 pub mod ownership;
 pub mod refptr;
 mod style_complex_color;
new file mode 100644
--- /dev/null
+++ b/servo/components/style/gecko_bindings/sugar/ns_css_value.rs
@@ -0,0 +1,77 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * 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/. */
+
+//! Little helpers for `nsCSSValue`.
+
+use gecko_bindings::bindings::Gecko_CSSValue_Drop;
+use gecko_bindings::structs::{nsCSSValue, nsCSSUnit, nsCSSValue_Array};
+use std::mem;
+use std::ops::Index;
+use std::slice;
+
+impl nsCSSValue {
+    /// Create a CSSValue with null unit, useful to be used as a return value.
+    #[inline]
+    pub fn null() -> Self {
+        unsafe { mem::zeroed() }
+    }
+
+    /// Returns this nsCSSValue value as an integer, unchecked in release
+    /// builds.
+    pub fn integer_unchecked(&self) -> i32 {
+        debug_assert!(self.mUnit == nsCSSUnit::eCSSUnit_Integer ||
+                      self.mUnit == nsCSSUnit::eCSSUnit_Enumerated ||
+                      self.mUnit == nsCSSUnit::eCSSUnit_EnumColor);
+        unsafe { *self.mValue.mInt.as_ref() }
+    }
+
+    /// Returns this nsCSSValue value as a floating point value, unchecked in
+    /// release builds.
+    pub fn float_unchecked(&self) -> f32 {
+        debug_assert!(nsCSSUnit::eCSSUnit_Number as u32 <= self.mUnit as u32);
+        unsafe { *self.mValue.mFloat.as_ref() }
+    }
+
+    /// Returns this nsCSSValue as a nsCSSValue::Array, unchecked in release
+    /// builds.
+    pub unsafe fn array_unchecked(&self) -> &nsCSSValue_Array {
+        debug_assert!(self.mUnit == nsCSSUnit::eCSSUnit_Array);
+        let array = unsafe { *self.mValue.mArray.as_ref() };
+        debug_assert!(!array.is_null());
+        unsafe { &*array }
+    }
+}
+
+impl Drop for nsCSSValue {
+    fn drop(&mut self) {
+        unsafe { Gecko_CSSValue_Drop(self) };
+    }
+}
+
+impl nsCSSValue_Array {
+    /// Return the length of this `nsCSSShadowArray`
+    #[inline]
+    pub fn len(&self) -> usize {
+        self.mCount
+    }
+
+    #[inline]
+    fn buffer(&self) -> *const nsCSSValue {
+        self.mArray.as_ptr()
+    }
+
+    /// Get the array as a slice of nsCSSValues.
+    #[inline]
+    pub fn as_slice(&self) -> &[nsCSSValue] {
+        unsafe { slice::from_raw_parts(self.buffer(), self.len()) }
+    }
+}
+
+impl Index<usize> for nsCSSValue_Array {
+    type Output = nsCSSValue;
+    #[inline]
+    fn index(&self, i: usize) -> &nsCSSValue {
+        &self.as_slice()[i]
+    }
+}