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
use crate::TypeName;

macro_rules! impl_primitive {
    ($type:ty) => {
        impl TypeName for $type {
            fn build_type_name<F: FnMut(&'static str)>(mut f: F) {
                f(stringify!($type));
            }
        }
    };
}

impl_primitive!(());
impl_primitive!(bool);
impl_primitive!(i8);
impl_primitive!(i16);
impl_primitive!(i32);
impl_primitive!(i64);
impl_primitive!(i128);
impl_primitive!(u8);
impl_primitive!(u16);
impl_primitive!(u32);
impl_primitive!(u64);
impl_primitive!(u128);
impl_primitive!(f32);
impl_primitive!(f64);
impl_primitive!(char);

macro_rules! impl_tuple {
    ($type:ident,) => {
        impl<$type: TypeName> TypeName for ($type,) {
            fn build_type_name<F: FnMut(&str)>(mut f: F) {
                f("(");
                $type::build_type_name(&mut f);
                f(",)");
            }
        }
    };
    ($first:ident, $($rest:ident,)+) => {
        impl<$first: TypeName, $($rest: TypeName),+> TypeName for ($first, $($rest,)+) {
            fn build_type_name<F: FnMut(&str)>(mut f: F) {
                f("(");
                $first::build_type_name(&mut f);
                $(f(", "); $rest::build_type_name(&mut f);)+
                f(")");
            }
        }

        impl_tuple! { $($rest,)+ }
    };
}

impl_tuple! { T11, T10, T9, T8, T7, T6, T5, T4, T3, T2, T1, T0, }

#[cfg(not(feature = "std"))]
macro_rules! impl_array {
    () => ();
    ($len:literal, $($rest:literal,)*) => {
        impl<T: TypeName> TypeName for [T; $len] {
            fn build_type_name<F: FnMut(&str)>(mut f: F) {
                f("[");
                T::build_type_name(&mut f);
                f("; ");
                f(stringify!($len));
                f("]");
            }
        }

        impl_array! { $($rest,)* }
    };
}

#[cfg(not(feature = "std"))]
impl_array! { 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, }

#[cfg(feature = "std")]
impl<T: TypeName, const N: usize> TypeName for [T; N] {
    fn build_type_name<F: FnMut(&str)>(mut f: F) {
        f("[");
        T::build_type_name(&mut f);
        f("; ");
        f(N.to_string().as_str());
        f("]");
    }
}

impl TypeName for str {
    fn build_type_name<F: FnMut(&str)>(mut f: F) {
        f("str");
    }
}

impl<T: TypeName> TypeName for [T] {
    fn build_type_name<F: FnMut(&str)>(mut f: F) {
        f("[");
        T::build_type_name(&mut f);
        f("]");
    }
}

impl<T: TypeName> TypeName for Option<T> {
    fn build_type_name<F: FnMut(&str)>(mut f: F) {
        f("core::option::Option<");
        T::build_type_name(&mut f);
        f(">");
    }
}