style: Use Rust types for Position.

This one should be much easier to review / much more pleasant to see :-)

Differential Revision: https://phabricator.services.mozilla.com/D19563
This commit is contained in:
Emilio Cobos Álvarez 2019-02-12 21:45:29 +01:00
parent d4ad12bee4
commit 1e6338e1ee
4 changed files with 26 additions and 58 deletions

View file

@ -82,6 +82,7 @@ include = [
"Size", "Size",
"MaxSize", "MaxSize",
"FlexBasis", "FlexBasis",
"Position",
] ]
item_types = ["enums", "structs", "typedefs"] item_types = ["enums", "structs", "typedefs"]
@ -90,6 +91,7 @@ item_types = ["enums", "structs", "typedefs"]
// Defined in nsStyleCoord.h // Defined in nsStyleCoord.h
static constexpr inline StyleLengthPercentage Zero(); static constexpr inline StyleLengthPercentage Zero();
static inline StyleLengthPercentage FromAppUnits(nscoord); static inline StyleLengthPercentage FromAppUnits(nscoord);
static inline StyleLengthPercentage FromPercentage(float);
inline CSSCoord LengthInCSSPixels() const; inline CSSCoord LengthInCSSPixels() const;
private: private:
inline nscoord LengthComponent() const; inline nscoord LengthComponent() const;
@ -103,8 +105,12 @@ item_types = ["enums", "structs", "typedefs"]
inline float ToPercentage() const; inline float ToPercentage() const;
inline CSSCoord ResolveToCSSPixels(CSSCoord aPercentageBasisInCSSPixels) const; inline CSSCoord ResolveToCSSPixels(CSSCoord aPercentageBasisInCSSPixels) const;
template<typename T> inline CSSCoord ResolveToCSSPixelsWith(T aPercentageGetter) const; template<typename T> inline CSSCoord ResolveToCSSPixelsWith(T aPercentageGetter) const;
template<typename T, typename U>
inline nscoord Resolve(T aPercentageGetter, U aPercentRoundingFunction) const;
template<typename T>
inline nscoord Resolve(nscoord aPercentageBasis, T aPercentRoundingFunction) const;
template<typename T> inline nscoord Resolve(T aPercentageGetter) const;
inline nscoord Resolve(nscoord aPercentageBasis) const; inline nscoord Resolve(nscoord aPercentageBasis) const;
template<typename T> inline nscoord ResolveWith(T aPercentageGetter) const;
""" """
"GenericLengthPercentageOrAuto" = """ "GenericLengthPercentageOrAuto" = """
@ -146,6 +152,11 @@ item_types = ["enums", "structs", "typedefs"]
inline bool BehavesLikeInitialValueOnBlockAxis() const; inline bool BehavesLikeInitialValueOnBlockAxis() const;
""" """
"GenericPosition" = """
inline bool DependsOnPositioningAreaSize() const;
static inline StyleGenericPosition FromPercentage(float);
"""
"Rect" = """ "Rect" = """
// Defined in nsStyleCoord.h // Defined in nsStyleCoord.h
template<typename Predicate> inline bool All(Predicate) const; template<typename Predicate> inline bool All(Predicate) const;

View file

@ -614,7 +614,6 @@ pub mod basic_shape {
//! Conversions from and to CSS shape representations. //! Conversions from and to CSS shape representations.
use crate::gecko::values::GeckoStyleCoordConvertible; use crate::gecko::values::GeckoStyleCoordConvertible;
use crate::gecko_bindings::structs;
use crate::gecko_bindings::structs::{nsStyleCoord, nsStyleCorners}; use crate::gecko_bindings::structs::{nsStyleCoord, nsStyleCorners};
use crate::gecko_bindings::structs::{StyleBasicShape, StyleBasicShapeType}; use crate::gecko_bindings::structs::{StyleBasicShape, StyleBasicShapeType};
use crate::gecko_bindings::structs::{ use crate::gecko_bindings::structs::{
@ -628,7 +627,6 @@ pub mod basic_shape {
use crate::values::computed::border::{BorderCornerRadius, BorderRadius}; use crate::values::computed::border::{BorderCornerRadius, BorderRadius};
use crate::values::computed::length::LengthPercentage; use crate::values::computed::length::LengthPercentage;
use crate::values::computed::motion::OffsetPath; use crate::values::computed::motion::OffsetPath;
use crate::values::computed::position;
use crate::values::computed::url::ComputedUrl; use crate::values::computed::url::ComputedUrl;
use crate::values::generics::basic_shape::{ use crate::values::generics::basic_shape::{
BasicShape as GenericBasicShape, InsetRect, Polygon, BasicShape as GenericBasicShape, InsetRect, Polygon,
@ -759,12 +757,12 @@ pub mod basic_shape {
}, },
StyleBasicShapeType::Circle => GenericBasicShape::Circle(Circle { StyleBasicShapeType::Circle => GenericBasicShape::Circle(Circle {
radius: (&other.mCoordinates[0]).into(), radius: (&other.mCoordinates[0]).into(),
position: (&other.mPosition).into(), position: other.mPosition,
}), }),
StyleBasicShapeType::Ellipse => GenericBasicShape::Ellipse(Ellipse { StyleBasicShapeType::Ellipse => GenericBasicShape::Ellipse(Ellipse {
semiaxis_x: (&other.mCoordinates[0]).into(), semiaxis_x: (&other.mCoordinates[0]).into(),
semiaxis_y: (&other.mCoordinates[1]).into(), semiaxis_y: (&other.mCoordinates[1]).into(),
position: (&other.mPosition).into(), position: other.mPosition,
}), }),
StyleBasicShapeType::Polygon => { StyleBasicShapeType::Polygon => {
let mut coords = Vec::with_capacity(other.mCoordinates.len() / 2); let mut coords = Vec::with_capacity(other.mCoordinates.len() / 2);
@ -852,17 +850,6 @@ pub mod basic_shape {
} }
} }
// Can't be a From impl since we need to set an existing
// Position, not create a new one
impl From<position::Position> for structs::Position {
fn from(other: position::Position) -> Self {
structs::Position {
mXPosition: other.horizontal.into(),
mYPosition: other.vertical.into(),
}
}
}
impl<'a> From<&'a nsStyleCoord> for ShapeRadius { impl<'a> From<&'a nsStyleCoord> for ShapeRadius {
fn from(other: &'a nsStyleCoord) -> Self { fn from(other: &'a nsStyleCoord) -> Self {
let other = other.borrow(); let other = other.borrow();
@ -871,15 +858,6 @@ pub mod basic_shape {
} }
} }
impl<'a> From<&'a structs::Position> for position::Position {
fn from(other: &'a structs::Position) -> Self {
position::Position {
horizontal: other.mXPosition.into(),
vertical: other.mYPosition.into(),
}
}
}
impl From<ShapeBox> for StyleGeometryBox { impl From<ShapeBox> for StyleGeometryBox {
fn from(reference: ShapeBox) -> Self { fn from(reference: ShapeBox) -> Self {
use crate::gecko_bindings::structs::StyleGeometryBox::*; use crate::gecko_bindings::structs::StyleGeometryBox::*;

View file

@ -453,22 +453,6 @@ def set_gecko_property(ffi_name, expr):
} }
</%def> </%def>
<%def name="impl_position(ident, gecko_ffi_name)">
#[allow(non_snake_case)]
pub fn set_${ident}(&mut self, v: longhands::${ident}::computed_value::T) {
${set_gecko_property("%s.mXPosition" % gecko_ffi_name, "v.horizontal.into()")}
${set_gecko_property("%s.mYPosition" % gecko_ffi_name, "v.vertical.into()")}
}
<%call expr="impl_simple_copy(ident, gecko_ffi_name)"></%call>
#[allow(non_snake_case)]
pub fn clone_${ident}(&self) -> longhands::${ident}::computed_value::T {
longhands::${ident}::computed_value::T {
horizontal: self.gecko.${gecko_ffi_name}.mXPosition.into(),
vertical: self.gecko.${gecko_ffi_name}.mYPosition.into(),
}
}
</%def>
<%def name="impl_color(ident, gecko_ffi_name)"> <%def name="impl_color(ident, gecko_ffi_name)">
<%call expr="impl_color_setter(ident, gecko_ffi_name)"></%call> <%call expr="impl_color_setter(ident, gecko_ffi_name)"></%call>
<%call expr="impl_color_copy(ident, gecko_ffi_name)"></%call> <%call expr="impl_color_copy(ident, gecko_ffi_name)"></%call>
@ -1401,7 +1385,7 @@ impl Clone for ${style_struct.gecko_struct_name} {
"OverflowWrap": impl_simple, "OverflowWrap": impl_simple,
"OverflowAnchor": impl_simple, "OverflowAnchor": impl_simple,
"Perspective": impl_style_coord, "Perspective": impl_style_coord,
"Position": impl_position, "Position": impl_simple,
"RGBAColor": impl_rgba_color, "RGBAColor": impl_rgba_color,
"SVGLength": impl_svg_length, "SVGLength": impl_svg_length,
"SVGOpacity": impl_svg_opacity, "SVGOpacity": impl_svg_opacity,
@ -3095,11 +3079,7 @@ fn static_assert() {
where I: IntoIterator<Item = longhands::scroll_snap_coordinate::computed_value::single_value::T>, where I: IntoIterator<Item = longhands::scroll_snap_coordinate::computed_value::single_value::T>,
I::IntoIter: ExactSizeIterator I::IntoIter: ExactSizeIterator
{ {
let iter = v.into_iter().map(|c| structs::mozilla::Position { self.gecko.mScrollSnapCoordinate.assign_from_iter_pod(v.into_iter());
mXPosition: c.horizontal.into(),
mYPosition: c.vertical.into(),
});
self.gecko.mScrollSnapCoordinate.assign_from_iter_pod(iter);
} }
pub fn copy_scroll_snap_coordinate_from(&mut self, other: &Self) { pub fn copy_scroll_snap_coordinate_from(&mut self, other: &Self) {
@ -3112,7 +3092,7 @@ fn static_assert() {
} }
pub fn clone_scroll_snap_coordinate(&self) -> longhands::scroll_snap_coordinate::computed_value::T { pub fn clone_scroll_snap_coordinate(&self) -> longhands::scroll_snap_coordinate::computed_value::T {
let vec = self.gecko.mScrollSnapCoordinate.iter().map(|f| f.into()).collect(); let vec = self.gecko.mScrollSnapCoordinate.iter().cloned().collect();
longhands::scroll_snap_coordinate::computed_value::List(vec) longhands::scroll_snap_coordinate::computed_value::List(vec)
} }
@ -3759,7 +3739,7 @@ fn static_assert() {
<% impl_simple_image_array_property("clip", shorthand, image_layers_field, "mClip", struct_name) %> <% impl_simple_image_array_property("clip", shorthand, image_layers_field, "mClip", struct_name) %>
<% impl_simple_image_array_property("origin", shorthand, image_layers_field, "mOrigin", struct_name) %> <% impl_simple_image_array_property("origin", shorthand, image_layers_field, "mOrigin", struct_name) %>
% for orientation in ["x", "y"]: % for (orientation, keyword) in [("x", "horizontal"), ("y", "vertical")]:
pub fn copy_${shorthand}_position_${orientation}_from(&mut self, other: &Self) { pub fn copy_${shorthand}_position_${orientation}_from(&mut self, other: &Self) {
use crate::gecko_bindings::structs::nsStyleImageLayers_LayerType as LayerType; use crate::gecko_bindings::structs::nsStyleImageLayers_LayerType as LayerType;
@ -3774,8 +3754,7 @@ fn static_assert() {
for (layer, other) in self.gecko.${image_layers_field}.mLayers.iter_mut() for (layer, other) in self.gecko.${image_layers_field}.mLayers.iter_mut()
.zip(other.gecko.${image_layers_field}.mLayers.iter()) .zip(other.gecko.${image_layers_field}.mLayers.iter())
.take(count as usize) { .take(count as usize) {
layer.mPosition.m${orientation.upper()}Position layer.mPosition.${keyword} = other.mPosition.${keyword};
= other.mPosition.m${orientation.upper()}Position;
} }
self.gecko.${image_layers_field}.mPosition${orientation.upper()}Count = count; self.gecko.${image_layers_field}.mPosition${orientation.upper()}Count = count;
} }
@ -3789,7 +3768,7 @@ fn static_assert() {
longhands::${shorthand}_position_${orientation}::computed_value::List( longhands::${shorthand}_position_${orientation}::computed_value::List(
self.gecko.${image_layers_field}.mLayers.iter() self.gecko.${image_layers_field}.mLayers.iter()
.take(self.gecko.${image_layers_field}.mPosition${orientation.upper()}Count as usize) .take(self.gecko.${image_layers_field}.mPosition${orientation.upper()}Count as usize)
.map(|position| position.mPosition.m${orientation.upper()}Position.into()) .map(|position| position.mPosition.${keyword})
.collect() .collect()
) )
} }
@ -3812,7 +3791,7 @@ fn static_assert() {
self.gecko.${image_layers_field}.mPosition${orientation[0].upper()}Count = v.len() as u32; self.gecko.${image_layers_field}.mPosition${orientation[0].upper()}Count = v.len() as u32;
for (servo, geckolayer) in v.zip(self.gecko.${image_layers_field} for (servo, geckolayer) in v.zip(self.gecko.${image_layers_field}
.mLayers.iter_mut()) { .mLayers.iter_mut()) {
geckolayer.mPosition.m${orientation[0].upper()}Position = servo.into(); geckolayer.mPosition.${keyword} = servo;
} }
} }
% endfor % endfor

View file

@ -19,20 +19,20 @@
ToAnimatedZero, ToAnimatedZero,
ToComputedValue, ToComputedValue,
)] )]
pub struct Position<H, V> { #[repr(C)]
pub struct GenericPosition<H, V> {
/// The horizontal component of position. /// The horizontal component of position.
pub horizontal: H, pub horizontal: H,
/// The vertical component of position. /// The vertical component of position.
pub vertical: V, pub vertical: V,
} }
pub use self::GenericPosition as Position;
impl<H, V> Position<H, V> { impl<H, V> Position<H, V> {
/// Returns a new position. /// Returns a new position.
pub fn new(horizontal: H, vertical: V) -> Self { pub fn new(horizontal: H, vertical: V) -> Self {
Self { Self { horizontal, vertical }
horizontal: horizontal,
vertical: vertical,
}
} }
} }