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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#![no_std]

extern crate alloc;
extern crate num_traits;

use num_traits::{One, Zero};
use core::cmp;
use core::ops::{Add, Sub};

mod id;
mod id_list;
mod id_range;
mod id_vector;

pub use id::Id;
pub use id_list::{IdFreeList, NoneAsNullId, NullId};
pub use id_range::{IdRange, ReverseIdRange};
pub use id_vector::{IdSlice, IdVec, MutIdSlice};

pub trait FromUsize {
    fn from_usize(idx: usize) -> Self;
}

pub trait ToUsize {
    fn to_usize(&self) -> usize;
}

pub trait IntegerHandle:
    Copy
    + Clone
    + Add<Output = Self>
    + Sub<Output = Self>
    + cmp::Ord
    + PartialEq
    + PartialOrd
    + FromUsize
    + ToUsize
    + Zero
    + One
{
}

pub trait Identifier: Copy + FromUsize + ToUsize + PartialEq {
    type Handle: IntegerHandle;
    type Tag;
}

impl Identifier for u8 {
    type Handle = u8;
    type Tag = ();
}
impl Identifier for u16 {
    type Handle = u16;
    type Tag = ();
}
impl Identifier for u32 {
    type Handle = u32;
    type Tag = ();
}
impl Identifier for u64 {
    type Handle = u64;
    type Tag = ();
}
impl Identifier for i8 {
    type Handle = i8;
    type Tag = ();
}
impl Identifier for i16 {
    type Handle = i16;
    type Tag = ();
}
impl Identifier for i32 {
    type Handle = i32;
    type Tag = ();
}
impl Identifier for i64 {
    type Handle = i64;
    type Tag = ();
}

impl ToUsize for u8 {
    #[inline]
    fn to_usize(&self) -> usize {
        *self as usize
    }
}
impl ToUsize for u16 {
    #[inline]
    fn to_usize(&self) -> usize {
        *self as usize
    }
}
impl ToUsize for u32 {
    #[inline]
    fn to_usize(&self) -> usize {
        *self as usize
    }
}
impl ToUsize for u64 {
    #[inline]
    fn to_usize(&self) -> usize {
        *self as usize
    }
}
impl ToUsize for usize {
    #[inline]
    fn to_usize(&self) -> usize {
        *self
    }
}
impl ToUsize for i8 {
    #[inline]
    fn to_usize(&self) -> usize {
        debug_assert!(*self >= 0);
        *self as usize
    }
}
impl ToUsize for i16 {
    #[inline]
    fn to_usize(&self) -> usize {
        debug_assert!(*self >= 0);
        *self as usize
    }
}
impl ToUsize for i32 {
    #[inline]
    fn to_usize(&self) -> usize {
        debug_assert!(*self >= 0);
        *self as usize
    }
}
impl ToUsize for i64 {
    #[inline]
    fn to_usize(&self) -> usize {
        debug_assert!(*self >= 0);
        *self as usize
    }
}
impl ToUsize for isize {
    #[inline]
    fn to_usize(&self) -> usize {
        debug_assert!(*self >= 0);
        *self as usize
    }
}

impl FromUsize for u8 {
    #[inline]
    fn from_usize(idx: usize) -> u8 {
        idx as u8
    }
}
impl FromUsize for u16 {
    #[inline]
    fn from_usize(idx: usize) -> u16 {
        idx as u16
    }
}
impl FromUsize for u32 {
    #[inline]
    fn from_usize(idx: usize) -> u32 {
        idx as u32
    }
}
impl FromUsize for u64 {
    #[inline]
    fn from_usize(idx: usize) -> u64 {
        idx as u64
    }
}
impl FromUsize for usize {
    #[inline]
    fn from_usize(idx: usize) -> usize {
        idx
    }
}
impl FromUsize for i8 {
    #[inline]
    fn from_usize(idx: usize) -> i8 {
        idx as i8
    }
}
impl FromUsize for i16 {
    #[inline]
    fn from_usize(idx: usize) -> i16 {
        idx as i16
    }
}
impl FromUsize for i32 {
    #[inline]
    fn from_usize(idx: usize) -> i32 {
        idx as i32
    }
}
impl FromUsize for i64 {
    #[inline]
    fn from_usize(idx: usize) -> i64 {
        idx as i64
    }
}
impl FromUsize for isize {
    #[inline]
    fn from_usize(idx: usize) -> isize {
        idx as isize
    }
}

impl IntegerHandle for u8 {}
impl IntegerHandle for u16 {}
impl IntegerHandle for u32 {}
impl IntegerHandle for u64 {}
impl IntegerHandle for usize {}
impl IntegerHandle for i8 {}
impl IntegerHandle for i16 {}
impl IntegerHandle for i32 {}
impl IntegerHandle for i64 {}
impl IntegerHandle for isize {}

/*
// TODO: remove it or implement traits manually

pub trait Generation {
    fn get_gen(&self) -> u32;
}

#[derive(Copy, Clone)]
pub struct GenId<T, H: Copy, G> {
    pub id: Id<T, H>,
    pub gen: G,
}

impl<T, H: Copy + core::fmt::Display, G: core::fmt::Display> core::fmt::Debug for GenId<T, H, G> {
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        write!(f, "GenId#{}({})", self.id.handle, self.gen)
    }
}

impl<T, H: IntegerHandle, G: PartialEq> PartialEq for GenId<T, H, G> {
    fn eq(&self, other: &GenId<T, H, G>) -> bool {
        self.id == other.id && self.gen == other.gen
    }
}

impl<T, H: IntegerHandle, G> ToUsize for GenId<T, H, G> {
    fn to_usize(&self) -> usize {
        self.id.to_usize()
    }
}

impl Generation for u8 {
    fn get_gen(&self) -> u32 {
        *self as u32
    }
}
impl Generation for u16 {
    fn get_gen(&self) -> u32 {
        *self as u32
    }
}
impl Generation for u32 {
    fn get_gen(&self) -> u32 {
        *self as u32
    }
}
impl Generation for u64 {
    fn get_gen(&self) -> u32 {
        *self as u32
    }
}

impl<T, H: Copy, G: Generation> Generation for GenId<T, H, G> {
    fn get_gen(&self) -> u32 {
        self.gen.get_gen()
    }
}
*/

#[test]
fn test_copy_id() {
    #[derive(Debug)]
    struct Foo;

    // check that Id is Copy
    let a: Id<Foo, u32> = Id::new(0);
    let b = a;
    let c = a;
    assert_eq!(b, c);

    // check that IdRange is Copy
    let r1: IdRange<Foo, u32> = IdRange::new(0..10);

    let r2 = r1;
    let r3 = r1;
    assert_eq!(r2, r3);
}

#[test]
fn test_reverese_id_range() {
    fn range(first: u16, count: u16) -> IdRange<u16, u16> {
        IdRange::new(first..(first + count))
    }
    let mut iter = ReverseIdRange::new(range(1, 5));
    assert_eq!(iter.next(), Some(Id::new(5)));
    assert_eq!(iter.next(), Some(Id::new(4)));
    assert_eq!(iter.next(), Some(Id::new(3)));
    assert_eq!(iter.next(), Some(Id::new(2)));
    assert_eq!(iter.next(), Some(Id::new(1)));
    assert_eq!(iter.next(), None);
    assert_eq!(iter.next(), None);
}