Skip to main content

blockworx_router/
coord.rs

1//! The routing lattice's per-axis coordinates. The grid is double-ended, so a
2//! coordinate is signed; the two axes are separate types so a `CoordX` cannot
3//! be handed to something expecting a row.
4
5macro_rules! define_coord {
6    ($name:ident) => {
7        #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
8        pub struct $name(i32);
9
10        impl $name {
11            /// The grid origin on this axis.
12            pub const ZERO: Self = $name(0);
13
14            #[must_use]
15            pub fn min(self, other: Self) -> Self {
16                $name(self.0.min(other.0))
17            }
18
19            #[must_use]
20            pub fn max(self, other: Self) -> Self {
21                $name(self.0.max(other.0))
22            }
23
24            pub fn abs(self) -> i32 {
25                self.0.abs()
26            }
27
28            pub fn raw(self) -> i32 {
29                self.0
30            }
31        }
32
33        impl From<i32> for $name {
34            fn from(value: i32) -> Self {
35                $name(value)
36            }
37        }
38
39        impl std::fmt::Display for $name {
40            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41                write!(f, "{}", self.0)
42            }
43        }
44
45        impl std::ops::Add for $name {
46            type Output = Self;
47            fn add(self, rhs: Self) -> Self {
48                $name(self.0 + rhs.0)
49            }
50        }
51
52        impl std::ops::Add<i32> for $name {
53            type Output = Self;
54            fn add(self, rhs: i32) -> Self {
55                $name(self.0 + rhs)
56            }
57        }
58
59        impl std::ops::Sub for $name {
60            type Output = Self;
61            fn sub(self, rhs: Self) -> Self {
62                $name(self.0 - rhs.0)
63            }
64        }
65
66        impl std::ops::Sub<i32> for $name {
67            type Output = Self;
68            fn sub(self, rhs: i32) -> Self {
69                $name(self.0 - rhs)
70            }
71        }
72    };
73}
74
75define_coord!(CoordX);
76define_coord!(CoordY);
77
78pub const INFINITY_X: CoordX = CoordX(i32::MAX >> 4);
79pub const INFINITY_Y: CoordY = CoordY(i32::MAX >> 4);
80pub const NEG_INFINITY_X: CoordX = CoordX(i32::MIN >> 4);
81pub const NEG_INFINITY_Y: CoordY = CoordY(i32::MIN >> 4);