From 19035590ceb565ac068529360e70b8eb4724acc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Sun, 16 Dec 2018 09:13:49 +0000 Subject: [PATCH] style: Cleanup some conversion code dealing with NonNegative. I'm about to introduce another use of it and I don't want to repeat the same copy-pasta again. Differential Revision: https://phabricator.services.mozilla.com/D14672 --- components/style/gecko/values.rs | 48 +++++++++++--------------------- 1 file changed, 16 insertions(+), 32 deletions(-) diff --git a/components/style/gecko/values.rs b/components/style/gecko/values.rs index 98dafcd070f..59ccd0afeca 100644 --- a/components/style/gecko/values.rs +++ b/components/style/gecko/values.rs @@ -13,12 +13,10 @@ use crate::gecko_bindings::sugar::ns_style_coord::{CoordData, CoordDataMut, Coor use crate::media_queries::Device; use crate::values::computed::basic_shape::ShapeRadius as ComputedShapeRadius; use crate::values::computed::FlexBasis as ComputedFlexBasis; -use crate::values::computed::NonNegativeNumber; use crate::values::computed::{Angle, ExtremumLength, Length, LengthOrPercentage}; use crate::values::computed::{LengthOrPercentageOrAuto, Percentage}; use crate::values::computed::{LengthOrPercentageOrNone, Number, NumberOrPercentage}; use crate::values::computed::{MaxLength as ComputedMaxLength, MozLength as ComputedMozLength}; -use crate::values::computed::{NonNegativeLength, NonNegativeLengthOrPercentage}; use crate::values::generics::basic_shape::ShapeRadius; use crate::values::generics::box_::Perspective; use crate::values::generics::flex::FlexBasis; @@ -34,6 +32,9 @@ use nsstring::{nsACString, nsCStr}; use std::cmp::max; /// A trait that defines an interface to convert from and to `nsStyleCoord`s. +/// +/// TODO(emilio): Almost everything that is in this file should be somehow +/// switched to cbindgen. pub trait GeckoStyleCoordConvertible: Sized { /// Convert this to a `nsStyleCoord`. fn to_gecko_style_coord(&self, coord: &mut T); @@ -66,6 +67,19 @@ impl GeckoStyleCoo } } +impl GeckoStyleCoordConvertible for NonNegative +where + Inner: GeckoStyleCoordConvertible, +{ + fn to_gecko_style_coord(&self, coord: &mut T) { + self.0.to_gecko_style_coord(coord) + } + + fn from_gecko_style_coord(coord: &T) -> Option { + Some(NonNegative(Inner::from_gecko_style_coord(coord)?)) + } +} + impl GeckoStyleCoordConvertible for ComputedFlexBasis { fn to_gecko_style_coord(&self, coord: &mut T) { match *self { @@ -152,16 +166,6 @@ impl GeckoStyleCoordConvertible for LengthOrPercentage { } } -impl GeckoStyleCoordConvertible for NonNegativeLengthOrPercentage { - fn to_gecko_style_coord(&self, coord: &mut T) { - self.0.to_gecko_style_coord(coord); - } - - fn from_gecko_style_coord(coord: &T) -> Option { - LengthOrPercentage::from_gecko_style_coord(coord).map(NonNegative::) - } -} - impl GeckoStyleCoordConvertible for Length { fn to_gecko_style_coord(&self, coord: &mut T) { coord.set_value(CoordDataValue::Coord(self.to_i32_au())); @@ -175,26 +179,6 @@ impl GeckoStyleCoordConvertible for Length { } } -impl GeckoStyleCoordConvertible for NonNegativeLength { - fn to_gecko_style_coord(&self, coord: &mut T) { - self.0.to_gecko_style_coord(coord); - } - - fn from_gecko_style_coord(coord: &T) -> Option { - Length::from_gecko_style_coord(coord).map(NonNegative::) - } -} - -impl GeckoStyleCoordConvertible for NonNegativeNumber { - fn to_gecko_style_coord(&self, coord: &mut T) { - self.0.to_gecko_style_coord(coord); - } - - fn from_gecko_style_coord(coord: &T) -> Option { - Number::from_gecko_style_coord(coord).map(NonNegative::) - } -} - impl GeckoStyleCoordConvertible for LengthOrPercentageOrAuto { fn to_gecko_style_coord(&self, coord: &mut T) { let value = match *self {