Skip to content

Commit

Permalink
Use Self keyword where appropriate
Browse files Browse the repository at this point in the history
The `Self` keyword should (in theory) make it easier to maintain a more generic code, especially if we migrate to 3d+m support.

* Replace return types with `-> Self`
* Replace some tuple constructors with `Self(x)`
  • Loading branch information
nyurik committed Mar 17, 2022
1 parent b6bfff1 commit 75a582e
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 75 deletions.
12 changes: 6 additions & 6 deletions geo-types/src/arbitrary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl<'a, T: arbitrary::Arbitrary<'a> + CoordFloat> arbitrary::Arbitrary<'a> for

impl<'a, T: arbitrary::Arbitrary<'a> + CoordFloat> arbitrary::Arbitrary<'a> for Point<T> {
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
u.arbitrary::<Coordinate<T>>().map(Point)
u.arbitrary::<Coordinate<T>>().map(Self)
}
}

Expand All @@ -26,7 +26,7 @@ impl<'a, T: arbitrary::Arbitrary<'a> + CoordFloat> arbitrary::Arbitrary<'a> for
return Err(arbitrary::Error::IncorrectFormat);
}

Ok(LineString(coords))
Ok(Self(coords))
}

fn size_hint(_depth: usize) -> (usize, Option<usize>) {
Expand All @@ -45,19 +45,19 @@ impl<'a, T: arbitrary::Arbitrary<'a> + CoordFloat> arbitrary::Arbitrary<'a> for

impl<'a, T: arbitrary::Arbitrary<'a> + CoordFloat> arbitrary::Arbitrary<'a> for MultiPoint<T> {
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
u.arbitrary::<Vec<Point<T>>>().map(MultiPoint)
u.arbitrary::<Vec<Point<T>>>().map(Self)
}
}

impl<'a, T: arbitrary::Arbitrary<'a> + CoordFloat> arbitrary::Arbitrary<'a> for MultiLineString<T> {
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
u.arbitrary::<Vec<LineString<T>>>().map(MultiLineString)
u.arbitrary::<Vec<LineString<T>>>().map(Self)
}
}

impl<'a, T: arbitrary::Arbitrary<'a> + CoordFloat> arbitrary::Arbitrary<'a> for MultiPolygon<T> {
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
u.arbitrary::<Vec<Polygon<T>>>().map(MultiPolygon)
u.arbitrary::<Vec<Polygon<T>>>().map(Self)
}
}

Expand All @@ -80,7 +80,7 @@ impl<'a, T: arbitrary::Arbitrary<'a> + CoordFloat> arbitrary::Arbitrary<'a> for

impl<'a, T: arbitrary::Arbitrary<'a> + CoordFloat> arbitrary::Arbitrary<'a> for Triangle<T> {
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
Ok(Triangle(
Ok(Self(
u.arbitrary::<Coordinate<T>>()?,
u.arbitrary::<Coordinate<T>>()?,
u.arbitrary::<Coordinate<T>>()?,
Expand Down
20 changes: 10 additions & 10 deletions geo-types/src/coordinate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ impl<T> Neg for Coordinate<T>
where
T: CoordNum + Neg<Output = T>,
{
type Output = Coordinate<T>;
type Output = Self;

fn neg(self) -> Coordinate<T> {
fn neg(self) -> Self {
(-self.x, -self.y).into()
}
}
Expand All @@ -132,9 +132,9 @@ where
/// assert_eq!(sum.y, 5.0);
/// ```
impl<T: CoordNum> Add for Coordinate<T> {
type Output = Coordinate<T>;
type Output = Self;

fn add(self, rhs: Coordinate<T>) -> Coordinate<T> {
fn add(self, rhs: Self) -> Self {
(self.x + rhs.x, self.y + rhs.y).into()
}
}
Expand All @@ -154,9 +154,9 @@ impl<T: CoordNum> Add for Coordinate<T> {
/// assert_eq!(diff.y, 0.);
/// ```
impl<T: CoordNum> Sub for Coordinate<T> {
type Output = Coordinate<T>;
type Output = Self;

fn sub(self, rhs: Coordinate<T>) -> Coordinate<T> {
fn sub(self, rhs: Self) -> Self {
(self.x - rhs.x, self.y - rhs.y).into()
}
}
Expand All @@ -175,9 +175,9 @@ impl<T: CoordNum> Sub for Coordinate<T> {
/// assert_eq!(q.y, 10.0);
/// ```
impl<T: CoordNum> Mul<T> for Coordinate<T> {
type Output = Coordinate<T>;
type Output = Self;

fn mul(self, rhs: T) -> Coordinate<T> {
fn mul(self, rhs: T) -> Self {
(self.x * rhs, self.y * rhs).into()
}
}
Expand All @@ -196,9 +196,9 @@ impl<T: CoordNum> Mul<T> for Coordinate<T> {
/// assert_eq!(q.y, 2.5);
/// ```
impl<T: CoordNum> Div<T> for Coordinate<T> {
type Output = Coordinate<T>;
type Output = Self;

fn div(self, rhs: T) -> Coordinate<T> {
fn div(self, rhs: T) -> Self {
(self.x / rhs, self.y / rhs).into()
}
}
Expand Down
18 changes: 9 additions & 9 deletions geo-types/src/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,49 +40,49 @@ pub enum Geometry<T: CoordNum> {
}

impl<T: CoordNum> From<Point<T>> for Geometry<T> {
fn from(x: Point<T>) -> Geometry<T> {
fn from(x: Point<T>) -> Self {
Geometry::Point(x)
}
}
impl<T: CoordNum> From<Line<T>> for Geometry<T> {
fn from(x: Line<T>) -> Geometry<T> {
fn from(x: Line<T>) -> Self {
Geometry::Line(x)
}
}
impl<T: CoordNum> From<LineString<T>> for Geometry<T> {
fn from(x: LineString<T>) -> Geometry<T> {
fn from(x: LineString<T>) -> Self {
Geometry::LineString(x)
}
}
impl<T: CoordNum> From<Polygon<T>> for Geometry<T> {
fn from(x: Polygon<T>) -> Geometry<T> {
fn from(x: Polygon<T>) -> Self {
Geometry::Polygon(x)
}
}
impl<T: CoordNum> From<MultiPoint<T>> for Geometry<T> {
fn from(x: MultiPoint<T>) -> Geometry<T> {
fn from(x: MultiPoint<T>) -> Self {
Geometry::MultiPoint(x)
}
}
impl<T: CoordNum> From<MultiLineString<T>> for Geometry<T> {
fn from(x: MultiLineString<T>) -> Geometry<T> {
fn from(x: MultiLineString<T>) -> Self {
Geometry::MultiLineString(x)
}
}
impl<T: CoordNum> From<MultiPolygon<T>> for Geometry<T> {
fn from(x: MultiPolygon<T>) -> Geometry<T> {
fn from(x: MultiPolygon<T>) -> Self {
Geometry::MultiPolygon(x)
}
}

impl<T: CoordNum> From<Rect<T>> for Geometry<T> {
fn from(x: Rect<T>) -> Geometry<T> {
fn from(x: Rect<T>) -> Self {
Geometry::Rect(x)
}
}

impl<T: CoordNum> From<Triangle<T>> for Geometry<T> {
fn from(x: Triangle<T>) -> Geometry<T> {
fn from(x: Triangle<T>) -> Self {
Geometry::Triangle(x)
}
}
Expand Down
8 changes: 4 additions & 4 deletions geo-types/src/geometry_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ pub struct GeometryCollection<T: CoordNum>(pub Vec<Geometry<T>>);
// todo: consider adding Default as a CoordNum requirement
impl<T: CoordNum> Default for GeometryCollection<T> {
fn default() -> Self {
GeometryCollection(Vec::new())
Self(Vec::new())
}
}

impl<T: CoordNum> GeometryCollection<T> {
/// Return an empty GeometryCollection
pub fn new() -> GeometryCollection<T> {
pub fn new() -> Self {
GeometryCollection::default()
}

Expand All @@ -102,14 +102,14 @@ impl<T: CoordNum> GeometryCollection<T> {
/// GeometryCollection
impl<T: CoordNum, IG: Into<Geometry<T>>> From<IG> for GeometryCollection<T> {
fn from(x: IG) -> Self {
GeometryCollection(vec![x.into()])
Self(vec![x.into()])
}
}

/// Collect Geometries (or what can be converted to a Geometry) into a GeometryCollection
impl<T: CoordNum, IG: Into<Geometry<T>>> FromIterator<IG> for GeometryCollection<T> {
fn from_iter<I: IntoIterator<Item = IG>>(iter: I) -> Self {
GeometryCollection(iter.into_iter().map(|g| g.into()).collect())
Self(iter.into_iter().map(|g| g.into()).collect())
}
}

Expand Down
6 changes: 3 additions & 3 deletions geo-types/src/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ impl<T: CoordNum> Line<T> {
/// assert_eq!(line.start, coord! { x: 0., y: 0. });
/// assert_eq!(line.end, coord! { x: 1., y: 2. });
/// ```
pub fn new<C>(start: C, end: C) -> Line<T>
pub fn new<C>(start: C, end: C) -> Self
where
C: Into<Coordinate<T>>,
{
Line {
Self {
start: start.into(),
end: end.into(),
}
Expand Down Expand Up @@ -156,7 +156,7 @@ impl<T: CoordNum> Line<T> {
}

impl<T: CoordNum> From<[(T, T); 2]> for Line<T> {
fn from(coord: [(T, T); 2]) -> Line<T> {
fn from(coord: [(T, T); 2]) -> Self {
Line::new(coord[0], coord[1])
}
}
Expand Down
6 changes: 3 additions & 3 deletions geo-types/src/line_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,20 +333,20 @@ impl<T: CoordNum> LineString<T> {
/// Turn a [`Vec`] of [`Point`]-like objects into a [`LineString`].
impl<T: CoordNum, IC: Into<Coordinate<T>>> From<Vec<IC>> for LineString<T> {
fn from(v: Vec<IC>) -> Self {
LineString(v.into_iter().map(|c| c.into()).collect())
Self(v.into_iter().map(|c| c.into()).collect())
}
}

impl<T: CoordNum> From<Line<T>> for LineString<T> {
fn from(line: Line<T>) -> Self {
LineString(vec![line.start, line.end])
Self(vec![line.start, line.end])
}
}

/// Turn an iterator of [`Point`]-like objects into a [`LineString`].
impl<T: CoordNum, IC: Into<Coordinate<T>>> FromIterator<IC> for LineString<T> {
fn from_iter<I: IntoIterator<Item = IC>>(iter: I) -> Self {
LineString(iter.into_iter().map(|c| c.into()).collect())
Self(iter.into_iter().map(|c| c.into()).collect())
}
}

Expand Down
4 changes: 2 additions & 2 deletions geo-types/src/multi_line_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ impl<T: CoordNum> MultiLineString<T> {

impl<T: CoordNum, ILS: Into<LineString<T>>> From<ILS> for MultiLineString<T> {
fn from(ls: ILS) -> Self {
MultiLineString(vec![ls.into()])
Self(vec![ls.into()])
}
}

impl<T: CoordNum, ILS: Into<LineString<T>>> FromIterator<ILS> for MultiLineString<T> {
fn from_iter<I: IntoIterator<Item = ILS>>(iter: I) -> Self {
MultiLineString(iter.into_iter().map(|ls| ls.into()).collect())
Self(iter.into_iter().map(|ls| ls.into()).collect())
}
}

Expand Down
10 changes: 5 additions & 5 deletions geo-types/src/multi_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,23 @@ pub struct MultiPoint<T: CoordNum>(pub Vec<Point<T>>);
impl<T: CoordNum, IP: Into<Point<T>>> From<IP> for MultiPoint<T> {
/// Convert a single `Point` (or something which can be converted to a `Point`) into a
/// one-member `MultiPoint`
fn from(x: IP) -> MultiPoint<T> {
MultiPoint(vec![x.into()])
fn from(x: IP) -> Self {
Self(vec![x.into()])
}
}

impl<T: CoordNum, IP: Into<Point<T>>> From<Vec<IP>> for MultiPoint<T> {
/// Convert a `Vec` of `Points` (or `Vec` of things which can be converted to a `Point`) into a
/// `MultiPoint`.
fn from(v: Vec<IP>) -> MultiPoint<T> {
MultiPoint(v.into_iter().map(|p| p.into()).collect())
fn from(v: Vec<IP>) -> Self {
Self(v.into_iter().map(|p| p.into()).collect())
}
}

impl<T: CoordNum, IP: Into<Point<T>>> FromIterator<IP> for MultiPoint<T> {
/// Collect the results of a `Point` iterator into a `MultiPoint`
fn from_iter<I: IntoIterator<Item = IP>>(iter: I) -> Self {
MultiPoint(iter.into_iter().map(|p| p.into()).collect())
Self(iter.into_iter().map(|p| p.into()).collect())
}
}

Expand Down
6 changes: 3 additions & 3 deletions geo-types/src/multi_polygon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@ pub struct MultiPolygon<T: CoordNum>(pub Vec<Polygon<T>>);

impl<T: CoordNum, IP: Into<Polygon<T>>> From<IP> for MultiPolygon<T> {
fn from(x: IP) -> Self {
MultiPolygon(vec![x.into()])
Self(vec![x.into()])
}
}

impl<T: CoordNum, IP: Into<Polygon<T>>> From<Vec<IP>> for MultiPolygon<T> {
fn from(x: Vec<IP>) -> Self {
MultiPolygon(x.into_iter().map(|p| p.into()).collect())
Self(x.into_iter().map(|p| p.into()).collect())
}
}

impl<T: CoordNum, IP: Into<Polygon<T>>> FromIterator<IP> for MultiPolygon<T> {
fn from_iter<I: IntoIterator<Item = IP>>(iter: I) -> Self {
MultiPolygon(iter.into_iter().map(|p| p.into()).collect())
Self(iter.into_iter().map(|p| p.into()).collect())
}
}

Expand Down
Loading

0 comments on commit 75a582e

Please sign in to comment.