Remove vec allocation

This commit is contained in:
Cory Forsstrom 2022-01-27 10:01:03 -08:00
parent e835cea03c
commit 730c57ba67

View file

@ -72,13 +72,16 @@ impl Path {
} }
pub(super) fn dashed(path: &Path, line_dash: LineDash) -> Path { pub(super) fn dashed(path: &Path, line_dash: LineDash) -> Path {
Path::new(|builder| {
let segments_odd = line_dash.segments.len() % 2 == 1; let segments_odd = line_dash.segments.len() % 2 == 1;
let segments = segments_odd let segments = segments_odd
.then(|| [&line_dash.segments[..], &line_dash.segments[..]].concat()) .then(|| {
[&line_dash.segments[..], &line_dash.segments[..]].concat()
})
.unwrap_or(line_dash.segments); .unwrap_or(line_dash.segments);
let mut points = vec![]; let mut draw_line = false;
walk_along_path( walk_along_path(
path.raw().iter().flattened(0.01), path.raw().iter().flattened(0.01),
@ -87,26 +90,24 @@ pub(super) fn dashed(path: &Path, line_dash: LineDash) -> Path {
callback: |position: lyon_algorithms::math::Point, callback: |position: lyon_algorithms::math::Point,
_tangent, _tangent,
_distance| { _distance| {
points.push(Point { let point = Point {
x: position.x, x: position.x,
y: position.y, y: position.y,
}); };
if draw_line {
builder.line_to(point);
} else {
builder.move_to(point);
}
draw_line = !draw_line;
true true
}, },
index: line_dash.offset, index: line_dash.offset,
intervals: &segments, intervals: &segments,
}, },
); );
Path::new(|builder| {
for (idx, point) in points.into_iter().enumerate() {
let is_even = idx % 2 == 0;
if is_even {
builder.move_to(point);
} else {
builder.line_to(point);
}
}
}) })
} }