[librsvg/marker: 8/9] marker: make code slightly more idiomatic



commit 941e80c751bae2de672057ec8c13b5f1939cebd6
Author: Paolo Borelli <pborelli gnome org>
Date:   Fri Jan 3 18:12:28 2020 +0100

    marker: make code slightly more idiomatic
    
    Use Option instead of a bool return value.

 rsvg_internals/src/marker.rs | 72 +++++++++++++++++++-------------------------
 1 file changed, 31 insertions(+), 41 deletions(-)
---
diff --git a/rsvg_internals/src/marker.rs b/rsvg_internals/src/marker.rs
index 05e7c17d..a306f176 100644
--- a/rsvg_internals/src/marker.rs
+++ b/rsvg_internals/src/marker.rs
@@ -508,19 +508,18 @@ impl<'a> From<&'a PathBuilder> for Segments {
 // segment's start and end points to align with the positive x-axis
 // in user space.
 impl Segments {
-    fn find_incoming_directionality_backwards(&self, start_index: usize) -> (bool, f64, f64) {
+    fn find_incoming_angle_backwards(&self, start_index: usize) -> Option<Angle> {
         // "go backwards ... within the current subpath until ... segment which has directionality
         // at its end point"
         for segment in self[..=start_index].iter().rev() {
             match *segment {
                 Segment::Degenerate { .. } => {
-                    return (false, 0.0, 0.0); // reached the beginning of the subpath as we ran into
-                                              // a standalone point
+                    return None; // reached the beginning of the subpath as we ran into a standalone point
                 }
 
                 Segment::LineOrCurve { .. } => match segment.get_directionalities() {
                     Some((_, _, v2x, v2y)) => {
-                        return (true, v2x, v2y);
+                        return Some(Angle::from_vector(v2x, v2y));
                     }
                     None => {
                         continue;
@@ -529,22 +528,21 @@ impl Segments {
             }
         }
 
-        (false, 0.0, 0.0)
+        None
     }
 
-    fn find_outgoing_directionality_forwards(&self, start_index: usize) -> (bool, f64, f64) {
+    fn find_outgoing_angle_forwards(&self, start_index: usize) -> Option<Angle> {
         // "go forwards ... within the current subpath until ... segment which has directionality at
         // its start point"
         for segment in &self[start_index..] {
             match *segment {
                 Segment::Degenerate { .. } => {
-                    return (false, 0.0, 0.0); // reached the end of a subpath as we ran into a
-                                              // standalone point
+                    return None; // reached the end of a subpath as we ran into a standalone point
                 }
 
                 Segment::LineOrCurve { .. } => match segment.get_directionalities() {
                     Some((v1x, v1y, _, _)) => {
-                        return (true, v1x, v1y);
+                        return Some(Angle::from_vector(v1x, v1y));
                     }
                     None => {
                         continue;
@@ -553,7 +551,7 @@ impl Segments {
             }
         }
 
-        (false, 0.0, 0.0)
+        None
     }
 }
 
@@ -687,13 +685,14 @@ where
                     assert!(i > 0);
 
                     // Got a lone point after a subpath; render the subpath's end marker first
-                    let (_, incoming_vx, incoming_vy) =
-                        segments.find_incoming_directionality_backwards(i - 1);
+                    let angle = segments
+                        .find_incoming_angle_backwards(i - 1)
+                        .unwrap_or_else(|| Angle::new(0.0));
                     let marker_bbox = emit_marker(
                         &segments[i - 1],
                         MarkerEndpoint::End,
                         MarkerType::End,
-                        Angle::from_vector(incoming_vx, incoming_vy),
+                        angle,
                         emit_fn,
                     )?;
                     bbox.insert(&marker_bbox);
@@ -716,13 +715,14 @@ where
                 // Not a degenerate segment
                 match subpath_state {
                     SubpathState::NoSubpath => {
-                        let (_, outgoing_vx, outgoing_vy) =
-                            segments.find_outgoing_directionality_forwards(i);
+                        let angle = segments
+                            .find_outgoing_angle_forwards(i)
+                            .unwrap_or_else(|| Angle::new(0.0));
                         let marker_bbox = emit_marker(
                             segment,
                             MarkerEndpoint::Start,
                             MarkerType::Start,
-                            Angle::from_vector(outgoing_vx, outgoing_vy),
+                            angle,
                             emit_fn,
                         )?;
                         bbox.insert(&marker_bbox);
@@ -733,25 +733,15 @@ where
                     SubpathState::InSubpath => {
                         assert!(i > 0);
 
-                        let (has_incoming, incoming_vx, incoming_vy) =
-                            segments.find_incoming_directionality_backwards(i - 1);
-                        let (has_outgoing, outgoing_vx, outgoing_vy) =
-                            segments.find_outgoing_directionality_forwards(i);
-
-                        let incoming = Angle::from_vector(incoming_vx, incoming_vy);
-                        let outgoing = Angle::from_vector(outgoing_vx, outgoing_vy);
+                        let incoming = segments.find_incoming_angle_backwards(i - 1);
+                        let outgoing = segments.find_outgoing_angle_forwards(i);
 
-                        let angle: Angle;
-
-                        if has_incoming && has_outgoing {
-                            angle = incoming.bisect(outgoing);
-                        } else if has_incoming {
-                            angle = incoming;
-                        } else if has_outgoing {
-                            angle = outgoing;
-                        } else {
-                            angle = Angle::new(0.0);
-                        }
+                        let angle = match (incoming, outgoing) {
+                            (Some(incoming), Some(outgoing)) => incoming.bisect(outgoing),
+                            (Some(incoming), _) => incoming,
+                            (_, Some(outgoing)) => outgoing,
+                            _ => Angle::new(0.0),
+                        };
 
                         let marker_bbox = emit_marker(
                             segment,
@@ -771,18 +761,18 @@ where
     if !segments.is_empty() {
         let segment = &segments[segments.len() - 1];
         if let Segment::LineOrCurve { .. } = *segment {
-            let (_, incoming_vx, incoming_vy) =
-                segments.find_incoming_directionality_backwards(segments.len() - 1);
+            let incoming = segments
+                .find_incoming_angle_backwards(segments.len() - 1)
+                .unwrap_or_else(|| Angle::new(0.0));
 
             let angle = {
                 if let PathCommand::ClosePath = builder.get_path_commands()[segments.len()] {
-                    let (_, outgoing_vx, outgoing_vy) =
-                        segments.find_outgoing_directionality_forwards(0);
-                    let incoming = Angle::from_vector(incoming_vx, incoming_vy);
-                    let outgoing = Angle::from_vector(outgoing_vx, outgoing_vy);
+                    let outgoing = segments
+                        .find_outgoing_angle_forwards(0)
+                        .unwrap_or_else(|| Angle::new(0.0));
                     incoming.bisect(outgoing)
                 } else {
-                    Angle::from_vector(incoming_vx, incoming_vy)
+                    incoming
                 }
             };
 


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