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
//! # Types
//!
//! Implementations of the types for ID-ish.

use super::*;

macro_rules! id {
    (@seal $id:ty) => { impl sealed::Sealed for $id {} };
    (@maybe $id:ty ) => {
        id!(@seal $id);
        impl MaybeID for $id {
            type Out = $id;
            fn as_u128(&self) -> u128 {
                *self as _
            }
            fn into_inner(self) -> Self {
                self
            }
        }
    };
    (@transmute $id:ty => $out:ty) => {
        id!(@seal $id);
        impl MaybeID for $id {
            type Out = $out;
            fn as_u128(&self) -> u128 {
                MaybeID::into_inner(*self) as _
            }
            fn into_inner(self) -> Self::Out {
                unsafe { core::mem::transmute(self) }
            }
        }
    };
    (@nz $($sign:ty => $id:ty => $by:ty),+) => {
        $(
            id!(@seal $sign);
            id!(@seal $id);
            id!($id, $sign);
            impl MaybeID for $id {
                type Out = $by;
                fn as_u128(&self)-> u128 {
                    MaybeID::into_inner(*self) as u128
                }
                fn into_inner(self) -> Self::Out {
                    <$id>::get(self)
                }
            }
            impl MaybeID for $sign {
                type Out = $by;
                fn as_u128(&self)-> u128 {
                    MaybeID::into_inner(*self) as u128
                }
                fn into_inner(self) -> Self::Out {
                    unsafe { core::mem::transmute(<$sign>::get(self)) }
                }
            }
        )+
    };
    ($id:ty) => {
        impl ID for $id {}
    };
    ($($ids:ty),+) => { $(id!($ids);)+ };
    ($($sign:ty => $ids:ty),+) => {
        $(
            id!($ids);
            id!(@maybe $ids);
            id!(@transmute $sign => $ids);
        )+
    };
    (@peel $n:tt -> $A:ident, $($v:tt -> $B:ident),+) => {
        id!($($v -> $B),+);
    };
    ($($v:tt -> $B:ident)+) => {
        impl<$($B),+> sealed::Sealed for ($($B),+) where $($B: sealed::Sealed),+ {}
        impl<$($B),+> MaybeID for ($($B),+) where $($B: MaybeID),+ {
            type Out = ($(<$B as MaybeID>::Out),+);
            fn as_u128(&self) -> u128 {
                panic!("Tuples can't be converted to u128");
            }
            fn into_inner(self) -> Self::Out
            {
                ($(<$B as MaybeID>::into_inner(self.$v)),+)
            }
        }

        impl<$($B),+> ID for ($($B),+) where $($B: ID),+ {}
    };
}

id!(@seal ());
impl MaybeID for () {
    type Out = ();
    fn as_u128(&self) -> u128 {
        0
    }
    fn into_inner(self) -> Self {
        self
    }
}
id!(i8 => u8, i16 => u16, i32 => u32, i64 => u64, isize => usize);
id!(@nz NonZeroI8 => NonZeroU8 => u8, NonZeroI16 => NonZeroU16 => u16, NonZeroI32 => NonZeroU32 => u32, NonZeroI64 => NonZeroU64 => u64, NonZeroIsize => NonZeroUsize => usize);
#[cfg(all(feature = "u128", not(feature = "i128")))]
id!(u128);
#[cfg(feature = "i128")]
id!(i128 => u128);

id! { 0 -> A 1 -> B }
id! { 0 -> A 1 -> B 2 -> C }
id! { 0 -> A 1 -> B 2 -> C 3 -> D }
id! { 0 -> A 1 -> B 2 -> C 3 -> D 4 -> E }
id! { 0 -> A 1 -> B 2 -> C 3 -> D 4 -> E 5 -> F }
id! { 0 -> A 1 -> B 2 -> C 3 -> D 4 -> E 5 -> F 6 -> G }
id! { 0 -> A 1 -> B 2 -> C 3 -> D 4 -> E 5 -> F 6 -> G 7 -> H }
id! { 0 -> A 1 -> B 2 -> C 3 -> D 4 -> E 5 -> F 6 -> G 7 -> H 8 -> I }
id! { 0 -> A 1 -> B 2 -> C 3 -> D 4 -> E 5 -> F 6 -> G 7 -> H 8 -> I 9 -> J }
id! { 0 -> A 1 -> B 2 -> C 3 -> D 4 -> E 5 -> F 6 -> G 7 -> H 8 -> I 9 -> J 10 -> K }
id! { 0 -> A 1 -> B 2 -> C 3 -> D 4 -> E 5 -> F 6 -> G 7 -> H 8 -> I 9 -> J 10 -> K 11 -> L }