1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
use std::marker::PhantomData;
use std::hash::{Hash, Hasher};
use std::cmp::Ordering;
use std::fmt;
use std::fmt::Formatter;
#[derive(Debug)]
pub struct Index<T> {
index: usize,
phantom: PhantomData<T>,
}
impl<T> Hash for Index<T> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.index.hash(state)
}
}
impl<T> Copy for Index<T> {}
impl<T> Clone for Index<T> {
fn clone(&self) -> Self {
Self::new(self.index)
}
}
impl<T> Eq for Index<T> {}
impl<T> PartialEq for Index<T> {
fn eq(&self, other: &Self) -> bool {
self.index.eq(&other.index)
}
}
impl<T> Ord for Index<T> {
fn cmp(&self, other: &Self) -> Ordering {
self.index.cmp(&other.index)
}
}
impl<T> PartialOrd for Index<T> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.index.partial_cmp(&other.index)
}
}
impl<T> fmt::Display for Index<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.index)
}
}
impl<T> Index<T> {
pub(crate) fn new(index: usize) -> Self {
Index {
index,
phantom: Default::default(),
}
}
pub fn value(&self) -> usize {
self.index
}
}
#[derive(Debug, Clone)]
pub(crate) struct IndexGenerator<T> {
counter: usize,
phantom: PhantomData<T>,
}
impl<T> Default for IndexGenerator<T> {
fn default() -> Self {
Self::new(0)
}
}
impl<T> IndexGenerator<T> {
pub fn new(start: usize) -> Self {
IndexGenerator {
counter: start,
phantom: Default::default(),
}
}
pub fn next(&mut self) -> Index<T> {
let index = Index::new(self.counter);
self.counter += 1;
index
}
}