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
use std::fmt::{Debug, Display};
use std::hash::Hash;
use std::iter::Step;

use num_traits::{AsPrimitive, FromPrimitive, PrimInt, Signed, ToPrimitive, Unsigned};

pub trait Num = PrimInt + Default + Debug + AsPrimitive<usize> + ToPrimitive;

pub trait SignNum = Num + Signed + FromPrimitive;

pub trait ToSigned {
    type Type: Num + Signed;

    fn to_signed(&self) -> Self::Type;
}

macro_rules! signed_type_impl {
    ($U:ty, $S:ty) => {
        impl ToSigned for $U {
            type Type = $S;

            fn to_signed(&self) -> Self::Type {
                *self as Self::Type
            }
        }
    };
}

signed_type_impl!(i8, i8);
signed_type_impl!(u8, i8);
signed_type_impl!(i16, i16);
signed_type_impl!(u16, i16);
signed_type_impl!(i32, i32);
signed_type_impl!(u32, i32);
signed_type_impl!(i64, i64);
signed_type_impl!(u64, i64);
signed_type_impl!(i128, i128);
signed_type_impl!(u128, i128);
signed_type_impl!(isize, isize);
signed_type_impl!(usize, isize);

pub trait MaxValue {
    const MAX: Self;
}

macro_rules! max_value_impl {
    ($T:ty) => {
        impl MaxValue for $T {
            const MAX: Self = <$T>::MAX;
        }
    };
}

// TODO: Create macro foreach
max_value_impl!(i8);
max_value_impl!(u8);
max_value_impl!(i16);
max_value_impl!(u16);
max_value_impl!(i32);
max_value_impl!(u32);
max_value_impl!(i64);
max_value_impl!(u64);
max_value_impl!(i128);
max_value_impl!(u128);
max_value_impl!(isize);
max_value_impl!(usize);

// TODO: Not use alias - IDEs does not support it
#[rustfmt::skip]
pub trait LinkType:
    Num
    + Unsigned
    + Step
    + ToSigned
    + MaxValue
    + FromPrimitive
    + Debug
    + Display
    + Hash
    + Send
    + Sync
    + 'static { }

#[rustfmt::skip]
impl<
    All: Num
        + Unsigned
        + Step
        + ToSigned
        + MaxValue
        + FromPrimitive
        + Debug
        + Display
        + Hash
        + Send
        + Sync
        + 'static,
    > LinkType for All { }