chalk_macros/
index.rs

1#[macro_export]
2macro_rules! index_struct {
3    ($(#[$m:meta])* $v:vis struct $n:ident {
4        $vf:vis value: usize,
5    }) => {
6        #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
7        $(#[$m])*
8        $v struct $n {
9            $vf value: usize,
10        }
11
12        impl $n {
13            // Not all index structs need this, so allow it to be dead
14            // code.
15            #[allow(dead_code)]
16            $v fn get_and_increment(&mut self) -> Self {
17                let old_value = *self;
18                self.increment();
19                old_value
20            }
21
22            #[allow(dead_code)]
23            $v fn increment(&mut self) {
24                self.value += 1;
25            }
26
27            // TODO: Once the Step trait is stabilized (https://github.com/rust-lang/rust/issues/42168), instead implement it and use the Iterator implementation of Range
28            pub fn iterate_range(range: ::std::ops::Range<Self>) -> impl Iterator<Item = $n> {
29                (range.start.value..range.end.value).into_iter().map(|i| Self { value: i })
30            }
31        }
32
33        impl ::std::fmt::Debug for $n {
34            fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
35                write!(fmt, "{}({})", stringify!($n), self.value)
36            }
37        }
38
39        impl From<usize> for $n {
40            fn from(value: usize) -> Self {
41                Self { value }
42            }
43        }
44    }
45}