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
#[derive(Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
pub(crate) struct DomainId(Option<usize>);

/// Used for mapping domain identifiers to unique integers.
/// Can be derived with `domain_enum!(TYPE)`;
pub trait DomainEnumeration {
    /// The unique integer for a specific domain
    fn id(&self) -> usize;
}

/// If only one domain is required, this can be used.
/// But if you have your own domain type defined with `domain_enum!` or implementing `DomainEnumeration` manually, do not use this.
/// Different domain types should never be mixed!
pub struct DefaultDomain;

impl DomainEnumeration for DefaultDomain {
    fn id(&self) -> usize {
        0
    }
}

impl DomainId {
    pub(crate) fn new(d: &impl DomainEnumeration) -> DomainId {
        DomainId(Some(d.id()))
    }
    pub(crate) fn index(&self) -> Option<usize> {
        self.0
    }
}

#[macro_export]
/// Implements DomainEnumeration for an enum.
///
/// This macro can only be used on primitive enums that implement Copy.
/// The current implementation of the macro unfortunately also requires
/// `DomainEnumeration` to be imported with this exact name.
///
/// # Example:
// @ START-DOC DOMAIN_MACRO_EXAMPLE
/// ```
/// #[macro_use] extern crate nuts;
/// use nuts::{domain_enum, DomainEnumeration};
/// #[derive(Clone, Copy)]
/// enum MyDomain {
///     DomainA,
///     DomainB,
/// }
/// domain_enum!(MyDomain);
/// ```
// @ END-DOC DOMAIN_MACRO_EXAMPLE
macro_rules! domain_enum {
    ( $e:tt ) => {
        impl DomainEnumeration for $e {
            fn id(&self) -> usize {
                *self as usize
            }
        }
    };
}