Skip to content

Commit c4ba6be

Browse files
cargo fmt + clippy
1 parent fe2a811 commit c4ba6be

File tree

4 files changed

+92
-87
lines changed

4 files changed

+92
-87
lines changed

geo/src/algorithm/line_split/line_split_trait.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,22 @@ use geo_types::CoordFloat;
22

33
use super::{LineSplitResult, LineSplitTwiceResult};
44

5-
6-
pub trait LineSplit<Scalar> where Self:Sized, Scalar: CoordFloat {
7-
5+
pub trait LineSplit<Scalar>
6+
where
7+
Self: Sized,
8+
Scalar: CoordFloat,
9+
{
810
/// Note on choice of return type:
9-
///
11+
///
1012
/// You may wonder why this does not return `Option<(Option<Line>, Option<Line>)>`?
1113
/// It is because then the return type causes uncertainty; The user may expect to possibly
1214
/// receive `Some((None, None))` which is never possible, this would lead to clutter in match
1315
/// statements.
14-
///
16+
///
1517
/// To make it easier to 'just get the first' or 'just get the second' you can use
1618
/// `LineSplitResult::first()` and `LineSplitResult::second()` which return `Option<T>`
17-
///
18-
///
19+
///
20+
///
1921
fn line_split(&self, fraction: Scalar) -> Option<LineSplitResult<Self>>;
2022

2123
/// Note on choice of return type:
@@ -57,7 +59,7 @@ pub trait LineSplit<Scalar> where Self:Sized, Scalar: CoordFloat {
5759
},
5860
Some(LineSplitResult::First (line1)) => Some(First(line1)),
5961
Some(LineSplitResult::Second(line2)) => match line2.line_split(second_fraction) {
60-
Some(LineSplitResult::FirstSecond(line2, line3)) => Some(SecondThird ( line2, line3)),
62+
Some(LineSplitResult::FirstSecond(line2, line3)) => Some(SecondThird ( line2, line3)),
6163
Some(LineSplitResult::First (line2 )) => Some(Second ( line2 )),
6264
Some(LineSplitResult::Second ( line3)) => Some(Third ( line3)),
6365
None => None,
Lines changed: 66 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
use geo_types::{Line, CoordFloat};
1+
use geo_types::{CoordFloat, Line};
22

3-
use super::{
4-
LineSplitResult,
5-
LineSplit,
6-
};
3+
use super::{LineSplit, LineSplitResult};
74

8-
impl<Scalar> LineSplit<Scalar> for Line<Scalar> where Scalar: CoordFloat {
5+
impl<Scalar> LineSplit<Scalar> for Line<Scalar>
6+
where
7+
Scalar: CoordFloat,
8+
{
99
fn line_split(&self, fraction: Scalar) -> Option<LineSplitResult<Self>> {
1010
if fraction.is_nan() {
11-
return None
11+
return None;
1212
}
1313
if fraction <= Scalar::zero() {
14-
Some(LineSplitResult::Second(self.clone()))
14+
Some(LineSplitResult::Second(*self))
1515
} else if fraction >= Scalar::one() {
16-
Some(LineSplitResult::First(self.clone()))
16+
Some(LineSplitResult::First(*self))
1717
} else {
1818
let new_midpoint = self.start + self.delta() * fraction;
1919
Some(LineSplitResult::FirstSecond(
@@ -25,10 +25,10 @@ impl<Scalar> LineSplit<Scalar> for Line<Scalar> where Scalar: CoordFloat {
2525
}
2626

2727
#[cfg(test)]
28-
mod test{
29-
use geo_types::coord;
28+
mod test {
3029
use super::super::LineSplitTwiceResult;
3130
use super::*;
31+
use geo_types::coord;
3232

3333
// =============================================================================================
3434
// Line::line_split()
@@ -38,71 +38,71 @@ mod test{
3838
fn test_line_split_first_second() {
3939
// simple x-axis aligned check
4040
let line = Line::new(
41-
coord!{x: 0.0_f32, y:0.0_f32},
42-
coord!{x:10.0_f32, y:0.0_f32},
41+
coord! {x: 0.0_f32, y:0.0_f32},
42+
coord! {x:10.0_f32, y:0.0_f32},
4343
);
4444
let result = line.line_split(0.6);
45-
assert_eq!(result, Some(LineSplitResult::FirstSecond(
46-
Line::new(
47-
coord!{x: 0.0_f32, y:0.0_f32},
48-
coord!{x: 6.0_f32, y:0.0_f32},
49-
),
50-
Line::new(
51-
coord!{x: 6.0_f32, y:0.0_f32},
52-
coord!{x:10.0_f32, y:0.0_f32},
53-
)
54-
)));
45+
assert_eq!(
46+
result,
47+
Some(LineSplitResult::FirstSecond(
48+
Line::new(
49+
coord! {x: 0.0_f32, y:0.0_f32},
50+
coord! {x: 6.0_f32, y:0.0_f32},
51+
),
52+
Line::new(
53+
coord! {x: 6.0_f32, y:0.0_f32},
54+
coord! {x:10.0_f32, y:0.0_f32},
55+
)
56+
))
57+
);
5558

5659
// simple y-axis aligned check
5760
let line = Line::new(
58-
coord!{x:0.0_f32, y: 0.0_f32},
59-
coord!{x:0.0_f32, y:10.0_f32},
61+
coord! {x:0.0_f32, y: 0.0_f32},
62+
coord! {x:0.0_f32, y:10.0_f32},
6063
);
6164
let result = line.line_split(0.3);
62-
assert_eq!(result, Some(LineSplitResult::FirstSecond(
63-
Line::new(
64-
coord!{x:0.0_f32, y:0.0_f32},
65-
coord!{x:0.0_f32, y:3.0_f32},
66-
),
67-
Line::new(
68-
coord!{x:0.0_f32, y:3.0_f32},
69-
coord!{x:0.0_f32, y:10.0_f32},
70-
)
71-
)));
65+
assert_eq!(
66+
result,
67+
Some(LineSplitResult::FirstSecond(
68+
Line::new(coord! {x:0.0_f32, y:0.0_f32}, coord! {x:0.0_f32, y:3.0_f32},),
69+
Line::new(
70+
coord! {x:0.0_f32, y:3.0_f32},
71+
coord! {x:0.0_f32, y:10.0_f32},
72+
)
73+
))
74+
);
7275

7376
// non_trivial check
7477
let line = Line::new(
75-
coord!{x: 1.0_f32, y: 1.0_f32},
76-
coord!{x:10.0_f32, y:-10.0_f32},
78+
coord! {x: 1.0_f32, y: 1.0_f32},
79+
coord! {x:10.0_f32, y:-10.0_f32},
7780
);
7881
let split_point = line.start + line.delta() * 0.7;
7982
let result = line.line_split(0.7);
80-
assert_eq!(result, Some(LineSplitResult::FirstSecond(
81-
Line::new(
82-
line.start,
83-
split_point,
84-
),
85-
Line::new(
86-
split_point,
87-
line.end,
88-
)
89-
)));
83+
assert_eq!(
84+
result,
85+
Some(LineSplitResult::FirstSecond(
86+
Line::new(line.start, split_point,),
87+
Line::new(split_point, line.end,)
88+
))
89+
);
9090
}
9191

9292
#[test]
9393
fn test_line_split_first() {
9494
// test one
9595
let line = Line::new(
96-
coord!{x: 0.0_f32, y:0.0_f32},
97-
coord!{x:10.0_f32, y:0.0_f32},
96+
coord! {x: 0.0_f32, y:0.0_f32},
97+
coord! {x:10.0_f32, y:0.0_f32},
9898
);
9999
let result = line.line_split(1.0);
100100
assert_eq!(result, Some(LineSplitResult::First(line)));
101101

102102
// Test numbers larger than one
103103
let line = Line::new(
104-
coord!{x: 0.0_f32, y:0.0_f32},
105-
coord!{x:10.0_f32, y:0.0_f32},
104+
coord! {x: 0.0_f32, y:0.0_f32},
105+
coord! {x:10.0_f32, y:0.0_f32},
106106
);
107107
let result = line.line_split(2.0);
108108
assert_eq!(result, Some(LineSplitResult::First(line)));
@@ -111,26 +111,25 @@ mod test{
111111
fn test_line_split_second() {
112112
// test zero
113113
let line = Line::new(
114-
coord!{x: 0.0_f32, y:0.0_f32},
115-
coord!{x:10.0_f32, y:0.0_f32},
114+
coord! {x: 0.0_f32, y:0.0_f32},
115+
coord! {x:10.0_f32, y:0.0_f32},
116116
);
117117
let result = line.line_split(0.0);
118118
assert_eq!(result, Some(LineSplitResult::Second(line)));
119119

120120
// Test negative numbers
121121
let line = Line::new(
122-
coord!{x: 0.0_f32, y:0.0_f32},
123-
coord!{x:10.0_f32, y:0.0_f32},
122+
coord! {x: 0.0_f32, y:0.0_f32},
123+
coord! {x:10.0_f32, y:0.0_f32},
124124
);
125125
let result = line.line_split(-2.0);
126126
assert_eq!(result, Some(LineSplitResult::Second(line)));
127127
}
128128

129-
130129
// =============================================================================================
131130
// Line::line_split_twice()
132131
// =============================================================================================
133-
132+
134133
macro_rules! test_line_split_twice_helper{
135134
($a:expr, $b:expr, $enum_variant:ident, $(($x1:expr, $x2:expr)),*)=>{{
136135
let line = Line::new(
@@ -154,14 +153,20 @@ mod test{
154153
}
155154

156155
#[test]
157-
fn test_line_split_twice(){
158-
test_line_split_twice_helper!(0.6, 0.8, FirstSecondThird, (0.0, 6.0), (6.0, 8.0), (8.0, 10.0));
156+
fn test_line_split_twice() {
157+
test_line_split_twice_helper!(
158+
0.6,
159+
0.8,
160+
FirstSecondThird,
161+
(0.0, 6.0),
162+
(6.0, 8.0),
163+
(8.0, 10.0)
164+
);
159165
test_line_split_twice_helper!(0.6, 1.0, FirstSecond, (0.0, 6.0), (6.0, 10.0));
160166
test_line_split_twice_helper!(0.6, 0.6, FirstThird, (0.0, 6.0), (6.0, 10.0));
161167
test_line_split_twice_helper!(0.0, 0.6, SecondThird, (0.0, 6.0), (6.0, 10.0));
162168
test_line_split_twice_helper!(1.0, 1.0, First, (0.0, 10.0));
163169
test_line_split_twice_helper!(0.0, 1.0, Second, (0.0, 10.0));
164170
test_line_split_twice_helper!(0.0, 0.0, Third, (0.0, 10.0));
165171
}
166-
167-
}
172+
}

geo/src/algorithm/line_split/line_split_trait_impl_for_linestring.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ where
2424
let LineStringMeasurements {
2525
length_total,
2626
length_segments,
27-
} = match measure_line_string(&self) {
27+
} = match measure_line_string(self) {
2828
Some(x) => x,
2929
None => return None,
3030
};
@@ -88,7 +88,7 @@ where
8888

8989
#[cfg(test)]
9090
mod test {
91-
use geo_types::{line_string, coord};
91+
use geo_types::{coord, line_string};
9292

9393
use super::super::LineSplitTwiceResult;
9494

@@ -99,7 +99,7 @@ mod test {
9999

100100
#[test]
101101
fn split() {
102-
let line_string:LineString<f32> = line_string![
102+
let line_string: LineString<f32> = line_string![
103103
(x:0.0, y:0.0),
104104
(x:1.0, y:0.0),
105105
(x:1.0, y:1.0),
@@ -110,15 +110,15 @@ mod test {
110110
assert_eq!(
111111
line_string.line_split(0.5),
112112
Some(LineSplitResult::FirstSecond(
113-
LineString::new(vec![line_string.0[0],line_string.0[1], slice_point]),
114-
LineString::new(vec![slice_point, line_string.0[2],line_string.0[3]])
113+
LineString::new(vec![line_string.0[0], line_string.0[1], slice_point]),
114+
LineString::new(vec![slice_point, line_string.0[2], line_string.0[3]])
115115
))
116116
);
117117
}
118-
118+
119119
#[test]
120120
fn split_on_point() {
121-
let line_string:LineString<f32> = line_string![
121+
let line_string: LineString<f32> = line_string![
122122
(x:0.0, y:0.0),
123123
(x:1.0, y:0.0),
124124
(x:1.0, y:1.0),
@@ -129,15 +129,15 @@ mod test {
129129
assert_eq!(
130130
line_string.line_split(0.5),
131131
Some(LineSplitResult::FirstSecond(
132-
LineString::new(vec![line_string.0[0],line_string.0[1], slice_point]),
132+
LineString::new(vec![line_string.0[0], line_string.0[1], slice_point]),
133133
LineString::new(vec![slice_point, line_string.0[3], line_string.0[4]])
134134
))
135135
);
136136
}
137137

138138
#[test]
139139
fn split_half_way_through_last_segment() {
140-
let line_string:LineString<f32> = line_string![
140+
let line_string: LineString<f32> = line_string![
141141
(x:0.0, y:0.0),
142142
(x:1.0, y:0.0),
143143
(x:1.0, y:1.0),
@@ -154,7 +154,7 @@ mod test {
154154

155155
#[test]
156156
fn split_half_way_through_first_segment() {
157-
let line_string:LineString<f32> = line_string![
157+
let line_string: LineString<f32> = line_string![
158158
(x:0.0, y:0.0),
159159
(x:1.0, y:0.0),
160160
(x:1.0, y:1.0),
@@ -171,7 +171,7 @@ mod test {
171171

172172
#[test]
173173
fn split_first() {
174-
let line_string:LineString<f32> = line_string![
174+
let line_string: LineString<f32> = line_string![
175175
(x:0.0, y:0.0),
176176
(x:1.0, y:0.0),
177177
];
@@ -183,7 +183,7 @@ mod test {
183183

184184
#[test]
185185
fn split_second() {
186-
let line_string:LineString<f32> = line_string![
186+
let line_string: LineString<f32> = line_string![
187187
(x:0.0, y:0.0),
188188
(x:1.0, y:0.0),
189189
];
@@ -193,21 +193,19 @@ mod test {
193193
);
194194
}
195195

196-
197-
198196
// =============================================================================================
199197
// LineString::line_split_twice()
200198
// =============================================================================================
201199
#[test]
202-
fn split_twice_typical(){
200+
fn split_twice_typical() {
203201
// I think if we exhaustively check
204202
// - `Line::line_split_twice()` and
205203
// - `LineString::line_split()`
206204
// then because the implementation for `line_split_twice` is shared
207205
// we don't need an exhaustive check for `LineString::line_split_twice()`
208206
// So I will just do a spot check for a typical case
209207

210-
let line_string:LineString<f32> = line_string![
208+
let line_string: LineString<f32> = line_string![
211209
(x:0.0, y:0.0),
212210
(x:1.0, y:0.0),
213211
(x:1.0, y:1.0),
@@ -234,4 +232,4 @@ mod test {
234232
)
235233
);
236234
}
237-
}
235+
}

geo/src/algorithm/line_split/line_split_twice_result.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,4 @@ impl<T> LineSplitTwiceResult<T> {
100100
Self::FirstSecondThird(a, b, c) => (Some(a), Some(b), Some(c)),
101101
}
102102
}
103-
}
103+
}

0 commit comments

Comments
 (0)