geo_traits/
rect.rs

1use std::marker::PhantomData;
2
3#[cfg(feature = "geo-types")]
4use geo_types::{Coord, CoordNum, Rect};
5
6use crate::{CoordTrait, UnimplementedCoord};
7
8/// A trait for accessing data from a generic Rect.
9///
10/// A Rect is an _axis-aligned_ bounded 2D rectangle whose area is
11/// defined by minimum and maximum [`Point`s][CoordTrait].
12pub trait RectTrait: crate::GeometryTrait {
13    /// The type of each underlying coordinate, which implements [CoordTrait]
14    type CoordType<'a>: 'a + CoordTrait<T = <Self as crate::GeometryTrait>::T>
15    where
16        Self: 'a;
17
18    /// The minimum coordinate of this Rect
19    fn min(&self) -> Self::CoordType<'_>;
20
21    /// The maximum coordinate of this Rect
22    fn max(&self) -> Self::CoordType<'_>;
23}
24
25#[cfg(feature = "geo-types")]
26impl<T: CoordNum> RectTrait for Rect<T> {
27    type CoordType<'b>
28        = Coord<T>
29    where
30        Self: 'b;
31
32    fn min(&self) -> Self::CoordType<'_> {
33        Rect::min(*self)
34    }
35
36    fn max(&self) -> Self::CoordType<'_> {
37        Rect::max(*self)
38    }
39}
40
41#[cfg(feature = "geo-types")]
42impl<'a, T: CoordNum + 'a> RectTrait for &'a Rect<T> {
43    type CoordType<'b>
44        = Coord<T>
45    where
46        Self: 'b;
47
48    fn min(&self) -> Self::CoordType<'_> {
49        Rect::min(**self)
50    }
51
52    fn max(&self) -> Self::CoordType<'_> {
53        Rect::max(**self)
54    }
55}
56
57/// An empty struct that implements [RectTrait].
58///
59/// This can be used as the `RectType` of the `GeometryTrait` by implementations that don't
60/// have a Rect concept
61pub struct UnimplementedRect<T>(PhantomData<T>);
62
63impl<T> RectTrait for UnimplementedRect<T> {
64    type CoordType<'a>
65        = UnimplementedCoord<Self::T>
66    where
67        Self: 'a;
68
69    fn min(&self) -> Self::CoordType<'_> {
70        unimplemented!()
71    }
72
73    fn max(&self) -> Self::CoordType<'_> {
74        unimplemented!()
75    }
76}