Skip to content

Commit

Permalink
Use LineString::from_raw() instead of LineString()
Browse files Browse the repository at this point in the history
Make migration simpler by using static function that will still be available if `LineString` becomes a type alias.

Similar to georust#777

- [x] I agree to follow the project's [code of conduct](https://github.com/georust/geo/blob/master/CODE_OF_CONDUCT.md).
- ~[ ] I added an entry to `CHANGES.md` if knowledge of this change could be valuable to users.~
---
  • Loading branch information
nyurik committed Mar 18, 2022
1 parent fdbfed6 commit b44da4c
Show file tree
Hide file tree
Showing 14 changed files with 43 additions and 47 deletions.
4 changes: 2 additions & 2 deletions geo-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,13 @@ mod tests {

#[test]
fn polygon_new_test() {
let exterior = LineString::from(vec![
let exterior = LineString::from_raw(vec![
coord! { x: 0., y: 0. },
coord! { x: 1., y: 1. },
coord! { x: 1., y: 0. },
coord! { x: 0., y: 0. },
]);
let interiors = vec![LineString::from(vec![
let interiors = vec![LineString::from_raw(vec![
coord! { x: 0.1, y: 0.1 },
coord! { x: 0.9, y: 0.9 },
coord! { x: 0.9, y: 0.1 },
Expand Down
19 changes: 12 additions & 7 deletions geo-types/src/line_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use std::ops::{Index, IndexMut};
/// ```
/// use geo_types::{coord, LineString};
///
/// let line_string = LineString::from(vec![
/// let line_string = LineString::from_raw(vec![
/// coord! { x: 0., y: 0. },
/// coord! { x: 10., y: 0. },
/// ]);
Expand Down Expand Up @@ -83,7 +83,7 @@ use std::ops::{Index, IndexMut};
/// ```
/// use geo_types::{coord, LineString};
///
/// let line_string = LineString::from(vec![
/// let line_string = LineString::from_raw(vec![
/// coord! { x: 0., y: 0. },
/// coord! { x: 10., y: 0. },
/// ]);
Expand All @@ -100,7 +100,7 @@ use std::ops::{Index, IndexMut};
/// ```
/// use geo_types::{coord, LineString};
///
/// let line_string = LineString::from(vec![
/// let line_string = LineString::from_raw(vec![
/// coord! { x: 0., y: 0. },
/// coord! { x: 10., y: 0. },
/// ]);
Expand All @@ -120,7 +120,7 @@ use std::ops::{Index, IndexMut};
/// ```
/// use geo_types::{coord, LineString, Point};
///
/// let line_string = LineString::from(vec![
/// let line_string = LineString::from_raw(vec![
/// coord! { x: 0., y: 0. },
/// coord! { x: 10., y: 0. },
/// ]);
Expand Down Expand Up @@ -191,6 +191,11 @@ impl<'a, T: CoordNum> DoubleEndedIterator for CoordinatesIter<'a, T> {
}

impl<T: CoordNum> LineString<T> {
/// Instantiate Self from the raw content value
pub fn from_raw(value: Vec<Coordinate<T>>) -> Self {
Self(value)
}

/// Return an iterator yielding the coordinates of a [`LineString`] as [`Point`]s
#[deprecated(note = "Use points() instead")]
pub fn points_iter(&self) -> PointsIter<T> {
Expand Down Expand Up @@ -529,7 +534,7 @@ mod test {
#[test]
fn test_exact_size() {
// see https://github.com/georust/geo/issues/762
let ls = LineString::from(vec![coord! { x: 0., y: 0. }, coord! { x: 10., y: 0. }]);
let ls = LineString::from_raw(vec![coord! { x: 0., y: 0. }, coord! { x: 10., y: 0. }]);

// reference to force the `impl IntoIterator for &LineString` impl, giving a `CoordinatesIter`
for c in (&ls).into_iter().rev().skip(1).rev() {
Expand Down Expand Up @@ -601,14 +606,14 @@ mod test {
let start = coord! { x: 0, y: 0 };
let end = coord! { x: 10, y: 10 };
let line = Line::new(start, end);
let expected = LineString::from(vec![start, end]);
let expected = LineString::from_raw(vec![start, end]);

assert_eq!(expected, LineString::from(line));

let start = coord! { x: 10., y: 0.5 };
let end = coord! { x: 10000., y: 10.4 };
let line = Line::new(start, end);
let expected = LineString::from(vec![start, end]);
let expected = LineString::from_raw(vec![start, end]);

assert_eq!(expected, LineString::from(line));
}
Expand Down
3 changes: 1 addition & 2 deletions geo/src/algorithm/centroid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -725,10 +725,9 @@ mod test {
}
#[test]
fn empty_interior_polygon_test() {
let empty: Vec<Coordinate<f64>> = vec![];
let poly = Polygon::new(
LineString::from(vec![p(0., 0.), p(0., 1.), p(1., 1.), p(1., 0.), p(0., 0.)]),
vec![LineString::from(empty)],
vec![LineString::from_raw(vec![])],
);
assert_eq!(poly.centroid(), Some(p(0.5, 0.5)));
}
Expand Down
3 changes: 1 addition & 2 deletions geo/src/algorithm/closest_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,7 @@ mod tests {

#[test]
fn empty_line_string_is_indeterminate() {
let empty: Vec<Coordinate<f32>> = Vec::new();
let ls = LineString::from(empty);
let ls = LineString::from_raw(Vec::new());
let p = Point::new(0.0, 0.0);

let got = ls.closest_point(&p);
Expand Down
6 changes: 2 additions & 4 deletions geo/src/algorithm/contains/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,7 @@ mod test {
/// Tests: Point in LineString
#[test]
fn empty_linestring_test() {
let empty: Vec<Coordinate<f64>> = Vec::new();
let linestring = LineString::from(empty);
let linestring = LineString::from_raw(Vec::new());
assert!(!linestring.contains(&Point::new(2., 1.)));
}
#[test]
Expand All @@ -155,8 +154,7 @@ mod test {
/// Tests: Point in Polygon
#[test]
fn empty_polygon_test() {
let empty: Vec<Coordinate<f64>> = Vec::new();
let linestring = LineString::from(empty);
let linestring = LineString::from_raw(Vec::new());
let poly = Polygon::new(linestring, Vec::new());
assert!(!poly.contains(&Point::new(2., 1.)));
}
Expand Down
2 changes: 1 addition & 1 deletion geo/src/algorithm/contains/triangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ where
T: GeoNum,
{
fn contains(&self, coord: &Coordinate<T>) -> bool {
let ls = LineString::from(vec![self.0, self.1, self.2, self.0]);
let ls = LineString::from_raw(vec![self.0, self.1, self.2, self.0]);
use crate::utils::{coord_pos_relative_to_ring, CoordPos};
coord_pos_relative_to_ring(*coord, &ls) == CoordPos::Inside
}
Expand Down
2 changes: 1 addition & 1 deletion geo/src/algorithm/convex_hull/graham.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ where
}

// Close and output the line string
let mut output = LineString::from(output);
let mut output = LineString::from_raw(output);
output.close();
output
}
Expand Down
2 changes: 1 addition & 1 deletion geo/src/algorithm/convex_hull/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ where
ls.push(ls[0]);
}

let mut ls = LineString::from(ls);
let mut ls = LineString::from_raw(ls);
ls.close();

// Maintain the CCW invariance
Expand Down
3 changes: 1 addition & 2 deletions geo/src/algorithm/coordinate_position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,7 @@ mod test {

#[test]
fn test_empty_poly() {
let empty: Vec<Coordinate<f64>> = vec![];
let square_poly = Polygon::new(LineString::from(empty), vec![]);
let square_poly: Polygon<f64> = Polygon::new(LineString::from_raw(vec![]), vec![]);
assert_eq!(
square_poly.coordinate_position(&Coordinate::zero()),
CoordPos::Outside
Expand Down
7 changes: 3 additions & 4 deletions geo/src/algorithm/dimensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,16 @@ pub trait HasDimensions {
/// Types like `Point` and `Rect`, which have at least one coordinate by construction, can
/// never be considered empty.
/// ```
/// use geo_types::{Point, coord, Coordinate, LineString};
/// use geo_types::{Point, coord, LineString};
/// use geo::algorithm::dimensions::HasDimensions;
///
/// let line_string = LineString::from(vec![
/// let line_string = LineString::from_raw(vec![
/// coord! { x: 0., y: 0. },
/// coord! { x: 10., y: 0. },
/// ]);
/// assert!(!line_string.is_empty());
///
/// let empty: Vec<Coordinate<f64>> = vec![];
/// let empty_line_string = LineString::from(empty);
/// let empty_line_string: LineString<f64> = LineString::from_raw(vec![]);
/// assert!(empty_line_string.is_empty());
///
/// let point = Point::new(0.0, 0.0);
Expand Down
8 changes: 4 additions & 4 deletions geo/src/algorithm/euclidean_distance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -687,8 +687,8 @@ mod test {
// Point to Polygon, empty Polygon
fn point_polygon_empty_test() {
// an empty Polygon
let points: Vec<Coordinate<f64>> = vec![];
let ls = LineString::from(points);
let points = vec![];
let ls = LineString::from_raw(points);
let poly = Polygon::new(ls, vec![]);
// A point on the octagon
let p = Point::new(2.5, 0.5);
Expand Down Expand Up @@ -824,8 +824,8 @@ mod test {
#[test]
// Point to LineString, empty LineString
fn point_linestring_empty_test() {
let points: Vec<Coordinate<f64>> = vec![];
let ls = LineString::from(points);
let points = vec![];
let ls = LineString::from_raw(points);
let p = Point::new(5.0, 4.0);
let dist = p.euclidean_distance(&ls);
assert_relative_eq!(dist, 0.0);
Expand Down
4 changes: 1 addition & 3 deletions geo/src/algorithm/intersects/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ mod test {
coord, line_string, polygon, Geometry, Line, LineString, MultiLineString, MultiPoint,
MultiPolygon, Point, Polygon, Rect,
};
use geo_types::Coordinate;

/// Tests: intersection LineString and LineString
#[test]
Expand All @@ -125,8 +124,7 @@ mod test {
#[test]
fn empty_linestring2_test() {
let linestring = line_string![(x: 3., y: 2.), (x: 7., y: 6.)];
let empty: Vec<Coordinate<f64>> = Vec::new();
assert!(!linestring.intersects(&LineString::from(empty)));
assert!(!linestring.intersects(&LineString::from_raw(Vec::new())));
}
#[test]
fn empty_all_linestring_test() {
Expand Down
7 changes: 3 additions & 4 deletions geo/src/algorithm/k_nearest_concave_hull.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,8 @@ where
return false;
}

let coords: Vec<Coordinate<T>> = hull.iter().take(hull.len() - 1).cloned().collect();
let linestring = LineString::from(coords);
let coords = hull.iter().take(hull.len() - 1).cloned().collect();
let linestring = LineString::from_raw(coords);
let line = crate::Line::new(*line[0], *line[1]);
linestring.intersects(&line)
}
Expand Down Expand Up @@ -402,8 +402,7 @@ mod tests {
#[test]
fn empty_hull() {
let actual: Polygon<f64> = concave_hull(vec![].iter(), 3);
let empty: Vec<Coordinate<f64>> = vec![];
let expected = Polygon::new(LineString::from(empty), vec![]);
let expected = Polygon::new(LineString::from_raw(vec![]), vec![]);
assert_eq!(actual, expected);
}
}
20 changes: 10 additions & 10 deletions geo/src/algorithm/line_locate_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ mod test {
use super::*;
use crate::geo_types::coord;
use crate::point;
use geo_types::Coordinate;
use num_traits::Float;

#[test]
Expand Down Expand Up @@ -213,24 +212,25 @@ mod test {
assert_eq!(ring.line_locate_point(&pt), None);

// point is equidistant to two line segments - return the fraction from the first closest
let line: Vec<Coordinate<f64>> = vec![
let line: LineString<f64> = LineString::from_raw(vec![
(0.0, 0.0).into(),
(1.0, 0.0).into(),
(1.0, 1.0).into(),
(0.0, 1.0).into(),
];
let line = LineString::from(line);
]);
let pt = point!(x: 0.0, y: 0.5);
assert_eq!(line.line_locate_point(&pt), Some(0.0));

let line: Vec<Coordinate<f64>> =
vec![(1.0, 1.0).into(), (1.0, 1.0).into(), (1.0, 1.0).into()];
let line = LineString::from(line);
let line: LineString<f64> = LineString::from_raw(vec![
(1.0, 1.0).into(),
(1.0, 1.0).into(),
(1.0, 1.0).into(),
]);
let pt = point!(x: 2.0, y: 2.0);
assert_eq!(line.line_locate_point(&pt), Some(0.0));

// line contains inf or nan
let line = LineString::from(vec![
let line: LineString<f64> = LineString::from_raw(vec![
coord! { x: 1.0, y: 1.0 },
coord! {
x: Float::nan(),
Expand All @@ -241,7 +241,7 @@ mod test {
let pt = point!(x: 2.0, y: 2.0);
assert_eq!(line.line_locate_point(&pt), None);

let line = LineString::from(vec![
let line: LineString<f64> = LineString::from_raw(vec![
coord! { x: 1.0, y: 1.0 },
coord! {
x: Float::infinity(),
Expand All @@ -251,7 +251,7 @@ mod test {
]);
let pt = point!(x: 2.0, y: 2.0);
assert_eq!(line.line_locate_point(&pt), None);
let line = LineString::from(vec![
let line: LineString<f64> = LineString::from_raw(vec![
coord! { x: 1.0, y: 1.0 },
coord! {
x: Float::neg_infinity(),
Expand Down

0 comments on commit b44da4c

Please sign in to comment.