[librsvg: 11/15] shapes: implement all the Draw with a macro




commit 0fb8c0f5bf5a95de13548429ccccc3940e5f8ce5
Author: Federico Mena Quintero <federico gnome org>
Date:   Tue Dec 1 15:24:45 2020 -0600

    shapes: implement all the Draw with a macro
    
    I would have liked to do this with an "impl <T: BasicShape> Draw for T",
    but the compiler complains of a conflict:
    
    error[E0119]: conflicting implementations of trait `element::Draw`:
      --> src/shapes.rs:61:1
       |
    61 | impl<T: BasicShape> Draw for T {
       | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation
       |
      ::: src/filters/mod.rs:55:1
       |
    55 | impl<T: FilterEffect> Draw for T {}
       | -------------------------------- first implementation here

 src/shapes.rs | 143 ++++++++++++++++------------------------------------------
 1 file changed, 38 insertions(+), 105 deletions(-)
---
diff --git a/src/shapes.rs b/src/shapes.rs
index de80937c..f86ce635 100644
--- a/src/shapes.rs
+++ b/src/shapes.rs
@@ -58,6 +58,30 @@ trait BasicShape {
     fn make_shape(&self, values: &ComputedValues, draw_ctx: &mut DrawingCtx) -> Shape;
 }
 
+macro_rules! impl_draw {
+    ($name:ident) => {
+        impl Draw for $name {
+            fn draw(
+                &self,
+                node: &Node,
+                acquired_nodes: &mut AcquiredNodes<'_>,
+                cascaded: &CascadedValues<'_>,
+                draw_ctx: &mut DrawingCtx,
+                clipping: bool,
+            ) -> Result<BoundingBox, RenderingError> {
+                let values = cascaded.get();
+                self.make_shape(values, draw_ctx).draw(
+                    node,
+                    acquired_nodes,
+                    values,
+                    draw_ctx,
+                    clipping,
+                )
+            }
+        }
+    };
+}
+
 fn make_ellipse(cx: f64, cy: f64, rx: f64, ry: f64) -> SvgPath {
     let mut builder = PathBuilder::default();
 
@@ -119,6 +143,8 @@ pub struct Path {
     path: Rc<SvgPath>,
 }
 
+impl_draw!(Path);
+
 impl SetAttributes for Path {
     fn set_attributes(&mut self, attrs: &Attributes) -> ElementResult {
         for (attr, value) in attrs.iter() {
@@ -144,21 +170,6 @@ impl BasicShape for Path {
     }
 }
 
-impl Draw for Path {
-    fn draw(
-        &self,
-        node: &Node,
-        acquired_nodes: &mut AcquiredNodes<'_>,
-        cascaded: &CascadedValues<'_>,
-        draw_ctx: &mut DrawingCtx,
-        clipping: bool,
-    ) -> Result<BoundingBox, RenderingError> {
-        let values = cascaded.get();
-        self.make_shape(values, draw_ctx)
-            .draw(node, acquired_nodes, values, draw_ctx, clipping)
-    }
-}
-
 #[derive(Debug, Default, PartialEq)]
 struct Points(Vec<(f64, f64)>);
 
@@ -220,6 +231,8 @@ pub struct Polygon {
     points: Points,
 }
 
+impl_draw!(Polygon);
+
 impl SetAttributes for Polygon {
     fn set_attributes(&mut self, attrs: &Attributes) -> ElementResult {
         for (attr, value) in attrs.iter() {
@@ -232,21 +245,6 @@ impl SetAttributes for Polygon {
     }
 }
 
-impl Draw for Polygon {
-    fn draw(
-        &self,
-        node: &Node,
-        acquired_nodes: &mut AcquiredNodes<'_>,
-        cascaded: &CascadedValues<'_>,
-        draw_ctx: &mut DrawingCtx,
-        clipping: bool,
-    ) -> Result<BoundingBox, RenderingError> {
-        let values = cascaded.get();
-        self.make_shape(values, draw_ctx)
-            .draw(node, acquired_nodes, values, draw_ctx, clipping)
-    }
-}
-
 impl BasicShape for Polygon {
     fn make_shape(&self, _values: &ComputedValues, _draw_ctx: &mut DrawingCtx) -> Shape {
         Shape::new(Rc::new(make_poly(&self.points, true)), Markers::Yes)
@@ -258,6 +256,8 @@ pub struct Polyline {
     points: Points,
 }
 
+impl_draw!(Polyline);
+
 impl SetAttributes for Polyline {
     fn set_attributes(&mut self, attrs: &Attributes) -> ElementResult {
         for (attr, value) in attrs.iter() {
@@ -270,21 +270,6 @@ impl SetAttributes for Polyline {
     }
 }
 
-impl Draw for Polyline {
-    fn draw(
-        &self,
-        node: &Node,
-        acquired_nodes: &mut AcquiredNodes<'_>,
-        cascaded: &CascadedValues<'_>,
-        draw_ctx: &mut DrawingCtx,
-        clipping: bool,
-    ) -> Result<BoundingBox, RenderingError> {
-        let values = cascaded.get();
-        self.make_shape(values, draw_ctx)
-            .draw(node, acquired_nodes, values, draw_ctx, clipping)
-    }
-}
-
 impl BasicShape for Polyline {
     fn make_shape(&self, _values: &ComputedValues, _draw_ctx: &mut DrawingCtx) -> Shape {
         Shape::new(Rc::new(make_poly(&self.points, false)), Markers::Yes)
@@ -299,6 +284,8 @@ pub struct Line {
     y2: Length<Vertical>,
 }
 
+impl_draw!(Line);
+
 impl SetAttributes for Line {
     fn set_attributes(&mut self, attrs: &Attributes) -> ElementResult {
         for (attr, value) in attrs.iter() {
@@ -315,21 +302,6 @@ impl SetAttributes for Line {
     }
 }
 
-impl Draw for Line {
-    fn draw(
-        &self,
-        node: &Node,
-        acquired_nodes: &mut AcquiredNodes<'_>,
-        cascaded: &CascadedValues<'_>,
-        draw_ctx: &mut DrawingCtx,
-        clipping: bool,
-    ) -> Result<BoundingBox, RenderingError> {
-        let values = cascaded.get();
-        self.make_shape(values, draw_ctx)
-            .draw(node, acquired_nodes, values, draw_ctx, clipping)
-    }
-}
-
 impl BasicShape for Line {
     fn make_shape(&self, values: &ComputedValues, draw_ctx: &mut DrawingCtx) -> Shape {
         let mut builder = PathBuilder::default();
@@ -360,6 +332,8 @@ pub struct Rect {
     ry: Option<Length<Vertical>>,
 }
 
+impl_draw!(Rect);
+
 impl SetAttributes for Rect {
     fn set_attributes(&mut self, attrs: &Attributes) -> ElementResult {
         for (attr, value) in attrs.iter() {
@@ -392,21 +366,6 @@ impl SetAttributes for Rect {
     }
 }
 
-impl Draw for Rect {
-    fn draw(
-        &self,
-        node: &Node,
-        acquired_nodes: &mut AcquiredNodes<'_>,
-        cascaded: &CascadedValues<'_>,
-        draw_ctx: &mut DrawingCtx,
-        clipping: bool,
-    ) -> Result<BoundingBox, RenderingError> {
-        let values = cascaded.get();
-        self.make_shape(values, draw_ctx)
-            .draw(node, acquired_nodes, values, draw_ctx, clipping)
-    }
-}
-
 impl BasicShape for Rect {
     fn make_shape(&self, values: &ComputedValues, draw_ctx: &mut DrawingCtx) -> Shape {
         let params = draw_ctx.get_view_params();
@@ -587,6 +546,8 @@ pub struct Circle {
     r: Length<Both>,
 }
 
+impl_draw!(Circle);
+
 impl SetAttributes for Circle {
     fn set_attributes(&mut self, attrs: &Attributes) -> ElementResult {
         for (attr, value) in attrs.iter() {
@@ -604,21 +565,6 @@ impl SetAttributes for Circle {
     }
 }
 
-impl Draw for Circle {
-    fn draw(
-        &self,
-        node: &Node,
-        acquired_nodes: &mut AcquiredNodes<'_>,
-        cascaded: &CascadedValues<'_>,
-        draw_ctx: &mut DrawingCtx,
-        clipping: bool,
-    ) -> Result<BoundingBox, RenderingError> {
-        let values = cascaded.get();
-        self.make_shape(values, draw_ctx)
-            .draw(node, acquired_nodes, values, draw_ctx, clipping)
-    }
-}
-
 impl BasicShape for Circle {
     fn make_shape(&self, values: &ComputedValues, draw_ctx: &mut DrawingCtx) -> Shape {
         let params = draw_ctx.get_view_params();
@@ -639,6 +585,8 @@ pub struct Ellipse {
     ry: Length<Vertical>,
 }
 
+impl_draw!(Ellipse);
+
 impl SetAttributes for Ellipse {
     fn set_attributes(&mut self, attrs: &Attributes) -> ElementResult {
         for (attr, value) in attrs.iter() {
@@ -661,21 +609,6 @@ impl SetAttributes for Ellipse {
     }
 }
 
-impl Draw for Ellipse {
-    fn draw(
-        &self,
-        node: &Node,
-        acquired_nodes: &mut AcquiredNodes<'_>,
-        cascaded: &CascadedValues<'_>,
-        draw_ctx: &mut DrawingCtx,
-        clipping: bool,
-    ) -> Result<BoundingBox, RenderingError> {
-        let values = cascaded.get();
-        self.make_shape(values, draw_ctx)
-            .draw(node, acquired_nodes, values, draw_ctx, clipping)
-    }
-}
-
 impl BasicShape for Ellipse {
     fn make_shape(&self, values: &ComputedValues, draw_ctx: &mut DrawingCtx) -> Shape {
         let params = draw_ctx.get_view_params();


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