[librsvg: 20/30] Add an enum_default macro and use it




commit 5340acfe36c17e8daabba5a8ea5336c13595d015
Author: Paolo Borelli <pborelli gnome org>
Date:   Sat Dec 26 14:53:18 2020 +0100

    Add an enum_default macro and use it

 src/aspect_ratio.rs         |  6 +-----
 src/dasharray.rs            |  6 +-----
 src/filters/blend.rs        |  4 +++-
 src/filters/color_matrix.rs |  6 +-----
 src/gradient.rs             |  8 ++------
 src/lib.rs                  |  4 +++-
 src/marker.rs               | 12 ++----------
 src/path_builder.rs         |  9 ++++-----
 src/util.rs                 | 12 ++++++++++++
 9 files changed, 29 insertions(+), 38 deletions(-)
---
diff --git a/src/aspect_ratio.rs b/src/aspect_ratio.rs
index fd0a8ecb..21c39ba1 100644
--- a/src/aspect_ratio.rs
+++ b/src/aspect_ratio.rs
@@ -45,11 +45,7 @@ enum FitMode {
     Slice,
 }
 
-impl Default for FitMode {
-    fn default() -> FitMode {
-        FitMode::Meet
-    }
-}
+enum_default!(FitMode, FitMode::Meet);
 
 #[derive(Debug, Copy, Clone, PartialEq, Eq)]
 struct Align {
diff --git a/src/dasharray.rs b/src/dasharray.rs
index 9af37877..31a35f79 100644
--- a/src/dasharray.rs
+++ b/src/dasharray.rs
@@ -12,11 +12,7 @@ pub enum Dasharray {
     Array(Vec<ULength<Both>>),
 }
 
-impl Default for Dasharray {
-    fn default() -> Dasharray {
-        Dasharray::None
-    }
-}
+enum_default!(Dasharray, Dasharray::None);
 
 impl Parse for Dasharray {
     fn parse<'i>(parser: &mut Parser<'i, '_>) -> Result<Dasharray, ParseError<'i>> {
diff --git a/src/filters/blend.rs b/src/filters/blend.rs
index eda17b2b..fbe33ef4 100755
--- a/src/filters/blend.rs
+++ b/src/filters/blend.rs
@@ -33,6 +33,8 @@ enum Mode {
     HslLuminosity,
 }
 
+enum_default!(Mode, Mode::Normal);
+
 /// The `feBlend` filter primitive.
 pub struct FeBlend {
     base: PrimitiveWithInput,
@@ -47,7 +49,7 @@ impl Default for FeBlend {
         FeBlend {
             base: PrimitiveWithInput::new::<Self>(),
             in2: None,
-            mode: Mode::Normal,
+            mode: Mode::default(),
         }
     }
 }
diff --git a/src/filters/color_matrix.rs b/src/filters/color_matrix.rs
index 97b09cf2..9e279626 100644
--- a/src/filters/color_matrix.rs
+++ b/src/filters/color_matrix.rs
@@ -26,11 +26,7 @@ enum OperationType {
     LuminanceToAlpha,
 }
 
-impl Default for OperationType {
-    fn default() -> Self {
-        OperationType::Matrix
-    }
-}
+enum_default!(OperationType, OperationType::Matrix);
 
 /// The `feColorMatrix` filter primitive.
 pub struct FeColorMatrix {
diff --git a/src/gradient.rs b/src/gradient.rs
index c2f517af..e608295b 100644
--- a/src/gradient.rs
+++ b/src/gradient.rs
@@ -46,6 +46,8 @@ pub enum SpreadMethod {
     Repeat,
 }
 
+enum_default!(SpreadMethod, SpreadMethod::Pad);
+
 impl Parse for SpreadMethod {
     fn parse<'i>(parser: &mut Parser<'i, '_>) -> Result<SpreadMethod, ParseError<'i>> {
         Ok(parse_identifiers!(
@@ -57,12 +59,6 @@ impl Parse for SpreadMethod {
     }
 }
 
-impl Default for SpreadMethod {
-    fn default() -> SpreadMethod {
-        SpreadMethod::Pad
-    }
-}
-
 #[derive(Debug, Copy, Clone, PartialEq)]
 struct StopOffset(UnitInterval);
 
diff --git a/src/lib.rs b/src/lib.rs
index 1cff576b..87457bb0 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -89,6 +89,9 @@ mod node;
 #[macro_use]
 mod property_macros;
 
+#[macro_use]
+mod util;
+
 mod angle;
 mod api;
 mod aspect_ratio;
@@ -133,7 +136,6 @@ mod text;
 mod transform;
 mod unit_interval;
 mod url_resolver;
-mod util;
 mod viewbox;
 mod xml;
 
diff --git a/src/marker.rs b/src/marker.rs
index 208d7f90..a29164fc 100644
--- a/src/marker.rs
+++ b/src/marker.rs
@@ -32,11 +32,7 @@ enum MarkerUnits {
     StrokeWidth,
 }
 
-impl Default for MarkerUnits {
-    fn default() -> MarkerUnits {
-        MarkerUnits::StrokeWidth
-    }
-}
+enum_default!(MarkerUnits, MarkerUnits::StrokeWidth);
 
 impl Parse for MarkerUnits {
     fn parse<'i>(parser: &mut Parser<'i, '_>) -> Result<MarkerUnits, ParseError<'i>> {
@@ -55,11 +51,7 @@ enum MarkerOrient {
     Angle(Angle),
 }
 
-impl Default for MarkerOrient {
-    fn default() -> MarkerOrient {
-        MarkerOrient::Angle(Angle::new(0.0))
-    }
-}
+enum_default!(MarkerOrient, MarkerOrient::Angle(Angle::new(0.0)));
 
 impl Parse for MarkerOrient {
     fn parse<'i>(parser: &mut Parser<'i, '_>) -> Result<MarkerOrient, ParseError<'i>> {
diff --git a/src/path_builder.rs b/src/path_builder.rs
index 5e9b6ace..56eb8688 100644
--- a/src/path_builder.rs
+++ b/src/path_builder.rs
@@ -336,11 +336,10 @@ pub enum PathCommand {
     ClosePath,
 }
 
-impl Default for PathCommand {
-    fn default() -> Self {
-        PathCommand::CurveTo(CubicBezierCurve::default())
-    }
-}
+enum_default!(
+    PathCommand,
+    PathCommand::CurveTo(CubicBezierCurve::default())
+);
 
 impl PathCommand {
     fn to_cairo(&self, cr: &cairo::Context) {
diff --git a/src/util.rs b/src/util.rs
index 2b79c0d2..e308359a 100644
--- a/src/util.rs
+++ b/src/util.rs
@@ -41,3 +41,15 @@ pub fn clamp<T: PartialOrd>(val: T, low: T, high: T) -> T {
         val
     }
 }
+
+#[macro_export]
+macro_rules! enum_default {
+    ($name:ident, $default:expr) => {
+        impl Default for $name {
+            #[inline]
+            fn default() -> $name {
+                $default
+            }
+        }
+    };
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]