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
use std::fmt::Debug;
use std::marker::PhantomData;
use std::hash::Hash;

pub trait Id: Copy + Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord {
    fn new(index: usize) -> Self;
}

/// Create a new simple id type, wrapper around a usize that can be created via `IdGen`
#[macro_export]
macro_rules! named_id {
    ($name:ident) => {
        #[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
        pub struct $name(pub usize);
        impl Id for $name {
            fn new(index: usize) -> Self {
                $name(index)
            }
        }
    }
}

/// Generates named Ids, wrappers around increasing usize values.
/// For Ids to be unique, just needs to be one `IdGen` per Id type.
pub struct IdGen<I> {
    id: usize,
    phantom: PhantomData<I>,
}

impl<I: Id> Default for IdGen<I> {
    fn default() -> Self {
        IdGen {
            id: 0,
            phantom: PhantomData,
        }
    }
}

impl<I: Id> IdGen<I> {

    pub fn new() -> Self {
        Self::default()
    }

    /// Not to be confused with `std::iterator::Iterator::next()`!
    /// This function simply increases the Id by 1 while keeping the type
    pub fn next_id(&mut self) -> I {
        let id = self.id;
        self.id = id.wrapping_add(1);
        Id::new(id)
    }
}

named_id!(WidgetId);