platform_num/
imp.rs

1use std::fmt::{Debug, Display};
2use std::hash::Hash;
3use std::iter::Step;
4
5use num_traits::{AsPrimitive, FromPrimitive, PrimInt, Signed, ToPrimitive, Unsigned};
6
7pub trait Num: PrimInt + Default + Debug + AsPrimitive<usize> + ToPrimitive {}
8
9impl<
10    All: PrimInt + Default + Debug + AsPrimitive<usize> + ToPrimitive,
11> Num for All {}
12
13pub trait SignNum: Num + Signed + FromPrimitive {}
14
15impl<
16    All: Num + Signed + FromPrimitive,
17> SignNum for All {}
18
19
20pub trait ToSigned {
21    type Type: Num + Signed;
22
23    fn to_signed(&self) -> Self::Type;
24}
25
26macro_rules! signed_type_impl {
27    ($U:ty, $S:ty) => {
28        impl ToSigned for $U {
29            type Type = $S;
30
31            fn to_signed(&self) -> Self::Type {
32                *self as Self::Type
33            }
34        }
35    };
36}
37
38signed_type_impl!(i8, i8);
39signed_type_impl!(u8, i8);
40signed_type_impl!(i16, i16);
41signed_type_impl!(u16, i16);
42signed_type_impl!(i32, i32);
43signed_type_impl!(u32, i32);
44signed_type_impl!(i64, i64);
45signed_type_impl!(u64, i64);
46signed_type_impl!(i128, i128);
47signed_type_impl!(u128, i128);
48signed_type_impl!(isize, isize);
49signed_type_impl!(usize, isize);
50
51pub trait MaxValue {
52    const MAX: Self;
53}
54
55macro_rules! max_value_impl {
56    ($T:ty) => {
57        impl MaxValue for $T {
58            const MAX: Self = <$T>::MAX;
59        }
60    };
61}
62
63// TODO: Create macro foreach
64max_value_impl!(i8);
65max_value_impl!(u8);
66max_value_impl!(i16);
67max_value_impl!(u16);
68max_value_impl!(i32);
69max_value_impl!(u32);
70max_value_impl!(i64);
71max_value_impl!(u64);
72max_value_impl!(i128);
73max_value_impl!(u128);
74max_value_impl!(isize);
75max_value_impl!(usize);
76
77// TODO: Not use alias - IDEs does not support it
78#[rustfmt::skip]
79pub trait LinkType:
80Num
81+ Unsigned
82+ Step
83+ ToSigned
84+ MaxValue
85+ FromPrimitive
86+ Debug
87+ Display
88+ Hash
89+ Send
90+ Sync
91+ 'static {}
92
93#[rustfmt::skip]
94impl<
95    All: Num
96    + Unsigned
97    + Step
98    + ToSigned
99    + MaxValue
100    + FromPrimitive
101    + Debug
102    + Display
103    + Hash
104    + Send
105    + Sync
106    + 'static,
107> LinkType for All {}