geo

Macro geometry_delegate_impl

Source
macro_rules! geometry_delegate_impl {
    ($($a:tt)*) => { ... };
}
Expand description

Implements the common pattern where a Geometry enum simply delegates its trait impl to it’s inner type.


trait Foo<T: GeoNum> {
    fn foo_1(&self, coord: Coord<T>)  -> bool;
    fn foo_2(&self) -> i32;
}

// Assuming we have an impl for all the inner types like this:
impl<T: GeoNum> Foo<T> for Point<T> {
    fn foo_1(&self, coord: Coord<T>)  -> bool { true }
    fn foo_2(&self)  -> i32 { 1 }
}
impl<T: GeoNum> Foo<T> for Line<T> {
    fn foo_1(&self, coord: Coord<T>)  -> bool { false }
    fn foo_2(&self)  -> i32 { 2 }
}
impl<T: GeoNum> Foo<T> for LineString<T> {
    fn foo_1(&self, coord: Coord<T>)  -> bool { true }
    fn foo_2(&self)  -> i32 { 3 }
}
impl<T: GeoNum> Foo<T> for Polygon<T> {
    fn foo_1(&self, coord: Coord<T>)  -> bool { false }
    fn foo_2(&self)  -> i32 { 4 }
}
impl<T: GeoNum> Foo<T> for MultiPoint<T> {
    fn foo_1(&self, coord: Coord<T>)  -> bool { true }
    fn foo_2(&self)  -> i32 { 5 }
}
impl<T: GeoNum> Foo<T> for MultiLineString<T> {
    fn foo_1(&self, coord: Coord<T>)  -> bool { false }
    fn foo_2(&self)  -> i32 { 6 }
}
impl<T: GeoNum> Foo<T> for MultiPolygon<T> {
    fn foo_1(&self, coord: Coord<T>)  -> bool { true }
    fn foo_2(&self)  -> i32 { 7 }
}
impl<T: GeoNum> Foo<T> for GeometryCollection<T> {
    fn foo_1(&self, coord: Coord<T>)  -> bool { false }
    fn foo_2(&self)  -> i32 { 8 }
}
impl<T: GeoNum> Foo<T> for Rect<T> {
    fn foo_1(&self, coord: Coord<T>)  -> bool { true }
    fn foo_2(&self)  -> i32 { 9 }
}
impl<T: GeoNum> Foo<T> for Triangle<T> {
    fn foo_1(&self, coord: Coord<T>)  -> bool { true }
    fn foo_2(&self)  -> i32 { 10 }
}

// If we want the impl for Geometry to simply delegate to it's
// inner case...
impl<T: GeoNum> Foo<T> for Geometry<T> {
    // Instead of writing out this trivial enum delegation...
    // fn foo_1(&self, coord: Coord<T>)  -> bool {
    //     match self {
    //        Geometry::Point(g) => g.foo_1(coord),
    //        Geometry::LineString(g) => g.foo_1(coord),
    //        _ => unimplemented!("...etc for other cases")
    //     }
    // }
    //
    // fn foo_2(&self)  -> i32 {
    //     match self {
    //        Geometry::Point(g) => g.foo_2(),
    //        Geometry::LineString(g) => g.foo_2(),
    //        _ => unimplemented!("...etc for other cases")
    //     }
    // }

    // we can equivalently write:
    geo::geometry_delegate_impl! {
        fn foo_1(&self, coord: Coord<T>) -> bool;
        fn foo_2(&self) -> i32;
    }
}