[librsvg/rustification] marker.rs: Factor out replacement/creation of segments
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg/rustification] marker.rs: Factor out replacement/creation of segments
- Date: Wed, 26 Oct 2016 18:06:20 +0000 (UTC)
commit e503b9fc375fa14e0a2230d83d07271257fb8670
Author: Federico Mena Quintero <federico gnome org>
Date: Wed Oct 26 12:00:38 2016 -0500
marker.rs: Factor out replacement/creation of segments
Move the "push a new segment or modify the last one" code to just before
the end of the loop. This way we get rid of the has_first_segment state
variable and the segment_num counter, and also we get to remove
duplicated code.
rust/src/marker.rs | 54 +++++++++++----------------------------------------
1 files changed, 12 insertions(+), 42 deletions(-)
---
diff --git a/rust/src/marker.rs b/rust/src/marker.rs
index 5da31d0..3c3beef 100644
--- a/rust/src/marker.rs
+++ b/rust/src/marker.rs
@@ -51,8 +51,6 @@ pub fn path_to_segments (path: cairo::Path) -> Vec<Segment> {
let mut cur_y: f64;
let mut subpath_start_x: f64;
let mut subpath_start_y: f64;
- let mut has_first_segment : bool;
- let mut segment_num : usize;
let mut segments: Vec<Segment>;
let mut state: SegmentState;
@@ -61,8 +59,6 @@ pub fn path_to_segments (path: cairo::Path) -> Vec<Segment> {
subpath_start_x = 0.0;
subpath_start_y = 0.0;
- has_first_segment = false;
- segment_num = 0;
segments = Vec::new ();
state = SegmentState::End;
@@ -70,26 +66,22 @@ pub fn path_to_segments (path: cairo::Path) -> Vec<Segment> {
last_x = cur_x;
last_y = cur_y;
+ let needs_new_segment: bool;
+ let seg: Segment;
+
match cairo_segment {
cairo::PathSegment::MoveTo ((x, y)) => {
- if has_first_segment {
- segment_num += 1;
- } else {
- has_first_segment = true;
- }
-
cur_x = x;
cur_y = y;
subpath_start_x = cur_x;
subpath_start_y = cur_y;
- let seg = Segment::Degenerate {
+ seg = Segment::Degenerate {
x: cur_x,
y: cur_y
};
-
- segments.push (seg);
+ needs_new_segment = true;
state = SegmentState::Start;
},
@@ -98,9 +90,6 @@ pub fn path_to_segments (path: cairo::Path) -> Vec<Segment> {
cur_x = x;
cur_y = y;
- let needs_new_segment : bool;
- let seg : Segment;
-
match state {
SegmentState::Start => {
state = SegmentState::End;
@@ -108,7 +97,6 @@ pub fn path_to_segments (path: cairo::Path) -> Vec<Segment> {
},
SegmentState::End => {
- segment_num += 1;
needs_new_segment = true;
}
}
@@ -126,12 +114,6 @@ pub fn path_to_segments (path: cairo::Path) -> Vec<Segment> {
x4: cur_x,
y4: cur_y,
};
-
- if needs_new_segment {
- segments.push (seg);
- } else {
- segments[segment_num] = seg;
- }
},
cairo::PathSegment::CurveTo ((mut x2, mut y2), (mut x3, mut y3), (x4, y4)) => {
@@ -141,9 +123,6 @@ pub fn path_to_segments (path: cairo::Path) -> Vec<Segment> {
let x1 = last_x;
let y1 = last_y;
- let needs_new_segment : bool;
- let seg : Segment;
-
match state {
SegmentState::Start => {
state = SegmentState::End;
@@ -151,7 +130,6 @@ pub fn path_to_segments (path: cairo::Path) -> Vec<Segment> {
},
SegmentState::End => {
- segment_num += 1;
needs_new_segment = true;
}
}
@@ -181,21 +159,12 @@ pub fn path_to_segments (path: cairo::Path) -> Vec<Segment> {
x4: x4,
y4: y4,
};
-
- if needs_new_segment {
- segments.push (seg);
- } else {
- segments[segment_num] = seg;
- }
}
cairo::PathSegment::ClosePath => {
cur_x = subpath_start_x;
cur_y = subpath_start_y;
- let needs_new_segment : bool;
- let seg: Segment;
-
match state {
SegmentState::Start => {
state = SegmentState::End;
@@ -221,14 +190,15 @@ pub fn path_to_segments (path: cairo::Path) -> Vec<Segment> {
x4: cur_x,
y4: cur_y,
};
-
- if needs_new_segment {
- segments.push (seg);
- } else {
- segments[segment_num] = seg;
- }
}
}
+
+ if needs_new_segment {
+ segments.push (seg);
+ } else {
+ let len = segments.len ();
+ segments[len - 1] = seg;
+ }
}
segments
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]