Skip to content

Commit

Permalink
Merge #778
Browse files Browse the repository at this point in the history
778: Use `MultiPoint::new()` instead of `MultiPoint()` r=frewsxcv a=nyurik

Make migration simpler by using static function that will still be available if `MultiPoint` becomes a type alias.

Similar to #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.~
---



Co-authored-by: Yuri Astrakhan <[email protected]>
  • Loading branch information
bors[bot] and nyurik authored Mar 28, 2022
2 parents 17ba0fe + 1657dc0 commit 8f4a8a8
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 32 deletions.
2 changes: 1 addition & 1 deletion geo-postgis/src/from_postgis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ where
{
fn from_postgis(mp: &'a T) -> Self {
let ret = mp.points().map(Point::from_postgis).collect();
MultiPoint(ret)
MultiPoint::new(ret)
}
}
impl<'a, T> FromPostgis<&'a T> for MultiLineString<f64>
Expand Down
36 changes: 20 additions & 16 deletions geo-types/src/multi_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ impl<'a, T: CoordNum> IntoIterator for &'a mut MultiPoint<T> {
}

impl<T: CoordNum> MultiPoint<T> {
pub fn new(value: Vec<Point<T>>) -> Self {
Self(value)
}

pub fn iter(&self) -> impl Iterator<Item = &Point<T>> {
self.0.iter()
}
Expand Down Expand Up @@ -111,8 +115,8 @@ where
/// use geo_types::MultiPoint;
/// use geo_types::point;
///
/// let a = MultiPoint(vec![point![x: 0., y: 0.], point![x: 10., y: 10.]]);
/// let b = MultiPoint(vec![point![x: 0., y: 0.], point![x: 10.001, y: 10.]]);
/// let a = MultiPoint::new(vec![point![x: 0., y: 0.], point![x: 10., y: 10.]]);
/// let b = MultiPoint::new(vec![point![x: 0., y: 0.], point![x: 10.001, y: 10.]]);
///
/// approx::assert_relative_eq!(a, b, max_relative=0.1)
/// ```
Expand Down Expand Up @@ -153,8 +157,8 @@ where
/// use geo_types::MultiPoint;
/// use geo_types::point;
///
/// let a = MultiPoint(vec![point![x: 0., y: 0.], point![x: 10., y: 10.]]);
/// let b = MultiPoint(vec![point![x: 0., y: 0.], point![x: 10.001, y: 10.]]);
/// let a = MultiPoint::new(vec![point![x: 0., y: 0.], point![x: 10., y: 10.]]);
/// let b = MultiPoint::new(vec![point![x: 0., y: 0.], point![x: 10.001, y: 10.]]);
///
/// approx::abs_diff_eq!(a, b, epsilon=0.1);
/// ```
Expand All @@ -176,7 +180,7 @@ mod test {

#[test]
fn test_iter() {
let multi = MultiPoint(vec![point![x: 0, y: 0], point![x: 10, y: 10]]);
let multi = MultiPoint::new(vec![point![x: 0, y: 0], point![x: 10, y: 10]]);

let mut first = true;
for p in &multi {
Expand All @@ -202,7 +206,7 @@ mod test {

#[test]
fn test_iter_mut() {
let mut multi = MultiPoint(vec![point![x: 0, y: 0], point![x: 10, y: 10]]);
let mut multi = MultiPoint::new(vec![point![x: 0, y: 0], point![x: 10, y: 10]]);

for point in &mut multi {
point.0.x += 1;
Expand All @@ -229,22 +233,22 @@ mod test {
fn test_relative_eq() {
let delta = 1e-6;

let multi = MultiPoint(vec![point![x: 0., y: 0.], point![x: 10., y: 10.]]);
let multi = MultiPoint::new(vec![point![x: 0., y: 0.], point![x: 10., y: 10.]]);

let multi_x = MultiPoint(vec![point![x: 0., y: 0.], point![x: 10.+delta, y: 10.]]);
let multi_x = MultiPoint::new(vec![point![x: 0., y: 0.], point![x: 10.+delta, y: 10.]]);
assert!(multi.relative_eq(&multi_x, 1e-2, 1e-2));
assert!(multi.relative_ne(&multi_x, 1e-12, 1e-12));

let multi_y = MultiPoint(vec![point![x: 0., y: 0.], point![x: 10., y: 10.+delta]]);
let multi_y = MultiPoint::new(vec![point![x: 0., y: 0.], point![x: 10., y: 10.+delta]]);
assert!(multi.relative_eq(&multi_y, 1e-2, 1e-2));
assert!(multi.relative_ne(&multi_y, 1e-12, 1e-12));

// Under-sized but otherwise equal.
let multi_undersized = MultiPoint(vec![point![x: 0., y: 0.]]);
let multi_undersized = MultiPoint::new(vec![point![x: 0., y: 0.]]);
assert!(multi.relative_ne(&multi_undersized, 1., 1.));

// Over-sized but otherwise equal.
let multi_oversized = MultiPoint(vec![
let multi_oversized = MultiPoint::new(vec![
point![x: 0., y: 0.],
point![x: 10., y: 10.],
point![x: 10., y:100.],
Expand All @@ -256,22 +260,22 @@ mod test {
fn test_abs_diff_eq() {
let delta = 1e-6;

let multi = MultiPoint(vec![point![x: 0., y: 0.], point![x: 10., y: 10.]]);
let multi = MultiPoint::new(vec![point![x: 0., y: 0.], point![x: 10., y: 10.]]);

let multi_x = MultiPoint(vec![point![x: 0., y: 0.], point![x: 10.+delta, y: 10.]]);
let multi_x = MultiPoint::new(vec![point![x: 0., y: 0.], point![x: 10.+delta, y: 10.]]);
assert!(multi.abs_diff_eq(&multi_x, 1e-2));
assert!(multi.abs_diff_ne(&multi_x, 1e-12));

let multi_y = MultiPoint(vec![point![x: 0., y: 0.], point![x: 10., y: 10.+delta]]);
let multi_y = MultiPoint::new(vec![point![x: 0., y: 0.], point![x: 10., y: 10.+delta]]);
assert!(multi.abs_diff_eq(&multi_y, 1e-2));
assert!(multi.abs_diff_ne(&multi_y, 1e-12));

// Under-sized but otherwise equal.
let multi_undersized = MultiPoint(vec![point![x: 0., y: 0.]]);
let multi_undersized = MultiPoint::new(vec![point![x: 0., y: 0.]]);
assert!(multi.abs_diff_ne(&multi_undersized, 1.));

// Over-sized but otherwise equal.
let multi_oversized = MultiPoint(vec![
let multi_oversized = MultiPoint::new(vec![
point![x: 0., y: 0.],
point![x: 10., y: 10.],
point![x: 10., y:100.],
Expand Down
5 changes: 3 additions & 2 deletions geo/src/algorithm/centroid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -802,10 +802,11 @@ mod test {
let p2 = point!(x: 2.0, y: 2.0);
let p3 = point!(x: 0.0, y: 2.0);

let multi_point = MultiPoint(vec![p0, p1, p2, p3]);
let multi_point = MultiPoint::new(vec![p0, p1, p2, p3]);
assert_eq!(multi_point.centroid().unwrap(), point!(x: 1.0, y: 1.0));

let collection = GeometryCollection(vec![MultiPoint(vec![p1, p2, p3]).into(), p0.into()]);
let collection =
GeometryCollection(vec![MultiPoint::new(vec![p1, p2, p3]).into(), p0.into()]);

assert_eq!(collection.centroid().unwrap(), point!(x: 1.0, y: 1.0));
}
Expand Down
2 changes: 1 addition & 1 deletion geo/src/algorithm/convex_hull/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn convex_hull_multipoint_test() {
Point::new(-1, 1),
Point::new(0, 10),
];
let mp = MultiPoint(v);
let mp = MultiPoint::new(v);
let correct = vec![
Coordinate::from((0, -10)),
Coordinate::from((10, 0)),
Expand Down
4 changes: 2 additions & 2 deletions geo/src/algorithm/coords_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub trait CoordsIter<'a> {
/// ```
/// use geo::coords_iter::CoordsIter;
///
/// let multi_point = geo::MultiPoint(vec![
/// let multi_point = geo::MultiPoint::new(vec![
/// geo::point!(x: -10., y: 0.),
/// geo::point!(x: 20., y: 20.),
/// geo::point!(x: 30., y: 40.),
Expand Down Expand Up @@ -684,7 +684,7 @@ mod test {
expected_coords.append(&mut coords.clone());
expected_coords.append(&mut coords);

let actual_coords = MultiPoint(vec![point, point])
let actual_coords = MultiPoint::new(vec![point, point])
.coords_iter()
.collect::<Vec<_>>();

Expand Down
2 changes: 1 addition & 1 deletion geo/src/algorithm/euclidean_distance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,7 @@ mod test {
Point::new(-1.0, 1.0),
Point::new(0.0, 10.0),
];
let mp = MultiPoint(v);
let mp = MultiPoint::new(v);
let p = Point::new(50.0, 50.0);
assert_relative_eq!(p.euclidean_distance(&mp), 64.03124237432849)
}
Expand Down
2 changes: 1 addition & 1 deletion geo/src/algorithm/extremes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ mod test {

#[test]
fn empty() {
let multi_point: MultiPoint<f32> = MultiPoint(vec![]);
let multi_point: MultiPoint<f32> = MultiPoint::new(vec![]);

let actual = multi_point.extremes();

Expand Down
2 changes: 1 addition & 1 deletion geo/src/algorithm/intersects/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ mod test {
);
let geom = Geometry::Point(pt);
let gc = GeometryCollection(vec![geom.clone()]);
let multi_point = MultiPoint(vec![pt]);
let multi_point = MultiPoint::new(vec![pt]);
let multi_ls = MultiLineString(vec![ls.clone()]);
let multi_poly = MultiPolygon::new(vec![poly.clone()]);

Expand Down
8 changes: 4 additions & 4 deletions geo/src/algorithm/map_coords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ impl<T: CoordNum, NT: CoordNum> MapCoords<T, NT> for MultiPoint<T> {
type Output = MultiPoint<NT>;

fn map_coords(&self, func: impl Fn(&(T, T)) -> (NT, NT) + Copy) -> Self::Output {
MultiPoint(self.iter().map(|p| p.map_coords(func)).collect())
MultiPoint::new(self.iter().map(|p| p.map_coords(func)).collect())
}
}

Expand All @@ -328,7 +328,7 @@ impl<T: CoordNum, NT: CoordNum, E> TryMapCoords<T, NT, E> for MultiPoint<T> {
&self,
func: impl Fn(&(T, T)) -> Result<(NT, NT), E> + Copy,
) -> Result<Self::Output, E> {
Ok(MultiPoint(
Ok(MultiPoint::new(
self.0
.iter()
.map(|p| p.try_map_coords(func))
Expand Down Expand Up @@ -718,11 +718,11 @@ mod test {
fn multipoint() {
let p1 = Point::new(10., 10.);
let p2 = Point::new(0., -100.);
let mp = MultiPoint(vec![p1, p2]);
let mp = MultiPoint::new(vec![p1, p2]);

assert_eq!(
mp.map_coords(|&(x, y)| (x + 10., y + 100.)),
MultiPoint(vec![Point::new(20., 110.), Point::new(10., 0.)])
MultiPoint::new(vec![Point::new(20., 110.), Point::new(10., 0.)])
);
}

Expand Down
6 changes: 3 additions & 3 deletions geo/src/algorithm/rotate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,14 +358,14 @@ mod test {

#[test]
fn test_rotate_multipoints() {
let multi_points = MultiPoint(vec![
let multi_points = MultiPoint::new(vec![
point!(x: 0., y: 0.),
point!(x: 1., y: 1.),
point!(x: 2., y: 1.),
]);

// Results match shapely for `centroid`
let expected_for_centroid = MultiPoint(vec![
let expected_for_centroid = MultiPoint::new(vec![
point!(x: 0.7642977396044841, y: -0.5118446353109125),
point!(x: 0.7642977396044842, y: 0.9023689270621824),
point!(x: 1.471404520791032, y: 1.60947570824873),
Expand All @@ -376,7 +376,7 @@ mod test {
);

// Results match shapely for `center`
let expected_for_center = MultiPoint(vec![
let expected_for_center = MultiPoint::new(vec![
point!(x: 0.6464466094067262, y: -0.5606601717798212),
point!(x: 0.6464466094067263, y: 0.8535533905932737),
point!(x: 1.353553390593274, y: 1.560660171779821),
Expand Down

0 comments on commit 8f4a8a8

Please sign in to comment.