From 0c810a0ba80e8414261a90cf568f127fddd9ae5f Mon Sep 17 00:00:00 2001 From: Daniel Alley Date: Thu, 2 Apr 2020 23:59:27 -0400 Subject: [PATCH] Cargo clippy recommendations & spelling fixes --- src/dash.rs | 4 ++-- src/draw_target.rs | 4 ++-- src/geom.rs | 15 +++++---------- src/rasterizer.rs | 20 +++++++------------- src/stroke.rs | 2 +- 5 files changed, 17 insertions(+), 28 deletions(-) diff --git a/src/dash.rs b/src/dash.rs index aa47b06..74810ab 100644 --- a/src/dash.rs +++ b/src/dash.rs @@ -174,8 +174,8 @@ pub fn dash_path(path: &Path, dash_array: &[f32], mut dash_offset: f32) -> Path } } - // We still have an intial segment that we need to emit - if initial_segment.len() > 0 { + // We still have an initial segment that we need to emit + if !initial_segment.is_empty() { dashed.move_to(initial_segment[0].x, initial_segment[0].y); for i in 1..initial_segment.len() { dashed.line_to(initial_segment[i].x, initial_segment[i].y); diff --git a/src/draw_target.rs b/src/draw_target.rs index ecb4469..cfefac3 100644 --- a/src/draw_target.rs +++ b/src/draw_target.rs @@ -446,7 +446,7 @@ impl DrawTarget { } // if we get here, we need to force dst to be monotonic, even though // we couldn't compute a unit_divide value (probably underflow). - let b = if abs(a - b) < abs(b - c) { a } else { c }; + let b = if (a - b).abs() < (b - c).abs() { a } else { c }; curve[1].y = b; } self.rasterizer.add_edge(curve[0], curve[2], true, curve[1]); @@ -1051,7 +1051,7 @@ impl DrawTarget { pub fn write_png>(&self, path: P) -> Result<(), png::EncodingError> { let file = File::create(path)?; - let ref mut w = BufWriter::new(file); + let w = &mut BufWriter::new(file); let mut encoder = png::Encoder::new(w, self.width as u32, self.height as u32); encoder.set_color(png::ColorType::RGBA); diff --git a/src/geom.rs b/src/geom.rs index 86c283f..0aeb725 100644 --- a/src/geom.rs +++ b/src/geom.rs @@ -10,13 +10,6 @@ pub fn intrect(x1: T, y1: T, x2: T, y2: T) -> euclid::default::Box2D euclid::default::Box2D::new(euclid::point2(x1, y1), euclid::point2(x2, y2)) } -pub fn abs(a: f32) -> f32 { - if a < 0. { - return -a; - } - return a; -} - // we can do this pub fn valid_unit_divide(mut numer: f32, mut denom: f32, ratio: &mut f32) -> bool { if numer < 0. { @@ -38,7 +31,8 @@ pub fn valid_unit_divide(mut numer: f32, mut denom: f32, ratio: &mut f32) -> boo return false; } *ratio = r; - return true; + + true } pub fn is_not_monotonic(a: f32, b: f32, c: f32) -> bool { @@ -47,12 +41,13 @@ pub fn is_not_monotonic(a: f32, b: f32, c: f32) -> bool { if ab < 0. { bc = -bc; } - return ab == 0. || bc < 0.; + + ab == 0. || bc < 0. } fn interp(a: f32, b: f32, t: f32) -> f32 { debug_assert!(t >= 0. && t <= 1.); - return a + (b - a) * t; + a + (b - a) * t } // Skia does a weird thing where it treats arrays of points as castable to array of floats. diff --git a/src/rasterizer.rs b/src/rasterizer.rs index 2d642d2..9ddb5e5 100644 --- a/src/rasterizer.rs +++ b/src/rasterizer.rs @@ -199,17 +199,10 @@ impl Rasterizer { } } -fn abs(mut value: i32) -> i32 { - if value < 0 { - value = -value; - } - return value; -} - // A cheap version of the "Alpha max plus beta min" algorithm (⍺=1, β=0.5) fn cheap_distance(mut dx: i32, mut dy: i32) -> i32 { - dx = abs(dx); - dy = abs(dy); + dx = dx.abs(); + dy = dy.abs(); // return max + min/2 if dx > dy { dx + (dy >> 1) @@ -229,7 +222,7 @@ fn diff_to_shift(dx: i32, dy: i32) -> i32 { dist = (dist + (1 << 4)) >> 5; // each subdivision (shift value) cuts this dist (error) by 1/4 - return (32 - ((dist as u32).leading_zeros()) as i32) >> 1; + (32 - ((dist as u32).leading_zeros()) as i32) >> 1 } // this metric is taken from skia @@ -238,7 +231,8 @@ fn compute_curve_steps(e: &Edge) -> i32 { let dy = (e.control_y << 1) - e.y1 - e.y2; let shift = diff_to_shift(dx << 4, dy << 4); assert!(shift >= 0); - return shift; + + shift } const SAMPLE_SIZE: f32 = (1 << SAMPLE_SHIFT) as f32; @@ -381,9 +375,9 @@ impl Rasterizer { } } - // add to the begining of the edge start list + // add to the beginning of the edge start list // if edges are added from left to right - // the'll be in this list from right to left + // they'll be in this list from right to left // this works out later during insertion e.next = self.edge_starts[cury as usize]; self.edge_starts[cury as usize] = Some(NonNull::from(e)); diff --git a/src/stroke.rs b/src/stroke.rs index f448e1d..3c44d74 100644 --- a/src/stroke.rs +++ b/src/stroke.rs @@ -187,7 +187,7 @@ fn bevel( * This function has a period of 4. e.g. swap(swap(swap(swap(x) == x */ fn swap(a: Vector) -> Vector { /* one of these needs to be negative. We choose a.x so that we rotate to the right instead of negating */ - return Vector::new(a.y, -a.x); + Vector::new(a.y, -a.x) } fn unperp(a: Vector) -> Vector {