Skip to main content

gammalooprs/utils/
index_vec.rs

1#[macro_export]
2macro_rules! define_index {
3    (
4$(#[$idx_meta:meta])*
5$idx_vis:vis struct $Idx:ident ;) => {
6    $(#[$idx_meta])*
7    #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash,serde::Serialize, serde::Deserialize,bincode::Encode, bincode::Decode)]
8    $idx_vis struct $Idx(pub usize);
9
10    impl ::std::convert::From<usize> for $Idx {
11        fn from(value: usize) -> Self {
12            $Idx(value)
13        }
14    }
15
16    impl ::std::ops::Add<$Idx> for $Idx {
17        type Output = $Idx;
18
19        fn add(self, other: $Idx) -> Self::Output {
20            $Idx(self.0 + other.0)
21        }
22    }
23
24    impl ::std::ops::Sub<$Idx> for $Idx {
25        type Output = $Idx;
26
27        fn sub(self, other: $Idx) -> Self::Output {
28            $Idx(self.0 - other.0)
29        }
30    }
31
32    impl ::std::ops::AddAssign<$Idx> for $Idx {
33        fn add_assign(&mut self, other: $Idx) {
34            self.0 += other.0;
35        }
36    }
37
38    impl ::std::ops::SubAssign<$Idx> for $Idx {
39        fn sub_assign(&mut self, other: $Idx) {
40            self.0 -= other.0;
41        }
42    }
43
44    impl ::std::convert::From<$Idx> for usize {
45        fn from(value: $Idx) -> Self {
46            value.0
47        }
48    }
49};
50}