Auto merge of #17798 - servo:derive-all-the-things, r=emilio

Derive some SVGPaint impls

<!-- Reviewable:start -->
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/17798)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2017-07-20 04:46:23 -07:00 committed by GitHub
commit fa34af609f
2 changed files with 2 additions and 77 deletions

View file

@ -255,7 +255,7 @@ impl ToCss for FontSettingTagFloat {
///
/// https://www.w3.org/TR/SVG2/painting.html#SpecifyingPaint
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Debug, PartialEq, ToAnimatedValue)]
#[derive(Clone, Debug, PartialEq, ToAnimatedValue, ToComputedValue, ToCss)]
pub struct SVGPaint<ColorType> {
/// The paint source
pub kind: SVGPaintKind<ColorType>,
@ -269,7 +269,7 @@ pub struct SVGPaint<ColorType> {
/// to have a fallback, Gecko lets the context
/// properties have a fallback as well.
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Debug, PartialEq, ToAnimatedValue, ToCss)]
#[derive(Clone, Debug, PartialEq, ToAnimatedValue, ToComputedValue, ToCss)]
pub enum SVGPaintKind<ColorType> {
/// `none`
None,
@ -283,35 +283,6 @@ pub enum SVGPaintKind<ColorType> {
ContextStroke,
}
impl<ColorType> SVGPaintKind<ColorType> {
/// Convert to a value with a different kind of color
pub fn convert<F, OtherColor>(&self, f: F) -> SVGPaintKind<OtherColor>
where F: Fn(&ColorType) -> OtherColor {
match *self {
SVGPaintKind::None => SVGPaintKind::None,
SVGPaintKind::ContextStroke => SVGPaintKind::ContextStroke,
SVGPaintKind::ContextFill => SVGPaintKind::ContextFill,
SVGPaintKind::Color(ref color) => {
SVGPaintKind::Color(f(color))
}
SVGPaintKind::PaintServer(ref server) => {
SVGPaintKind::PaintServer(server.clone())
}
}
}
}
impl<ColorType> SVGPaint<ColorType> {
/// Convert to a value with a different kind of color
pub fn convert<F, OtherColor>(&self, f: F) -> SVGPaint<OtherColor>
where F: Fn(&ColorType) -> OtherColor {
SVGPaint {
kind: self.kind.convert(&f),
fallback: self.fallback.as_ref().map(|color| f(color))
}
}
}
impl<ColorType> SVGPaintKind<ColorType> {
/// Parse a keyword value only
fn parse_ident<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> {
@ -365,15 +336,3 @@ impl<ColorType: Parse> Parse for SVGPaint<ColorType> {
}
}
}
impl<ColorType: ToCss> ToCss for SVGPaint<ColorType> {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
self.kind.to_css(dest)?;
if let Some(ref fallback) = self.fallback {
fallback.to_css(dest)?;
}
Ok(())
}
}

View file

@ -701,40 +701,6 @@ pub type SVGPaint = ::values::generics::SVGPaint<RGBAColor>;
/// Specified SVG Paint Kind value
pub type SVGPaintKind = ::values::generics::SVGPaintKind<RGBAColor>;
impl ToComputedValue for SVGPaint {
type ComputedValue = super::computed::SVGPaint;
#[inline]
fn to_computed_value(&self, context: &Context) -> Self::ComputedValue {
super::computed::SVGPaint {
kind: self.kind.to_computed_value(context),
fallback: self.fallback.as_ref().map(|f| f.to_computed_value(context))
}
}
#[inline]
fn from_computed_value(computed: &Self::ComputedValue) -> Self {
SVGPaint {
kind: ToComputedValue::from_computed_value(&computed.kind),
fallback: computed.fallback.as_ref().map(ToComputedValue::from_computed_value)
}
}
}
impl ToComputedValue for SVGPaintKind {
type ComputedValue = super::computed::SVGPaintKind;
#[inline]
fn to_computed_value(&self, context: &Context) -> Self::ComputedValue {
self.convert(|color| color.to_computed_value(context))
}
#[inline]
fn from_computed_value(computed: &Self::ComputedValue) -> Self {
computed.convert(ToComputedValue::from_computed_value)
}
}
/// <length> | <percentage> | <number>
pub type LengthOrPercentageOrNumber = Either<Number, LengthOrPercentage>;