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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use core::ops::Rem;
pub trait TryFromDigits<T> {
fn from_digits(n: T) -> Result<Self, <Self as TryFrom<T>>::Error>
where
Self: TryFrom<T>;
}
macro_rules! try_from_digits_impls {
( $($type:ty, $trait_name:ident);* ) => {
$(
impl<T> TryFromDigits<T> for $type
where
T: Rem<T, Output = T> + $trait_name,
Self: TryFrom<T>,
{
#[inline]
fn from_digits(n: T) -> Result<Self, <Self as TryFrom<T>>::Error>
{
<Self as TryFrom<T>>::try_from(n % T::num())
}
}
)*
}
}
try_from_digits_impls! { i8,Thousands; u8, Thousands; i16, HundredThousands; u16, HundredThousands;
i32, TenBillions; u32, TenBillions; i64, BigTen; u64, BigHundred; isize, BigTen; usize, BigHundred }
trait Thousands {
fn num() -> Self;
}
macro_rules! thousands_impls {
( $($type:ty),* ; $value:expr ) => {
$(
impl Thousands for $type {
#[inline]
fn num() -> Self {
$value
}
}
)*
}
}
thousands_impls! { i16, u16, i32, u32, i64, u64, isize, usize, i128, u128; 1000 }
trait HundredThousands {
fn num() -> Self;
}
macro_rules! hundred_thousands_impls {
( $($type:ty),* ; $value:expr ) => {
$(
impl HundredThousands for $type {
#[inline]
fn num() -> Self {
$value
}
}
)*
}
}
hundred_thousands_impls! { i32, u32, i64, u64, isize, usize, i128, u128; 100_000 }
trait TenBillions {
fn num() -> Self;
}
macro_rules! ten_billions_impls {
( $($type:ty),* ; $value:expr ) => {
$(
impl TenBillions for $type {
#[inline]
fn num() -> Self {
$value
}
}
)*
}
}
ten_billions_impls! { i64, u64, isize, usize, i128, u128; 10_000_000_000 }
trait BigTen {
fn num() -> Self;
}
trait BigHundred {
fn num() -> Self;
}
macro_rules! big_impls {
( $( $trait_name:ident, $type:ty, $value:expr );* ) => {
$(
impl $trait_name for $type {
#[inline]
fn num() -> Self {
$value
}
}
)*
}
}
big_impls! { BigTen, i128, 10_000_000_000_000_000_000; BigHundred, u128, 100_000_000_000_000_000_000 }
big_impls! { BigTen, u128, 10_000_000_000_000_000_000; BigHundred, i128, 100_000_000_000_000_000_000 }
#[cfg(test)]
mod tests {
use super::*;
use paste::paste;
macro_rules! try_from_digits {
( $trait_name:ident; $($type:ty),* ; $value:expr ) => {
$( paste! {
#[test]
fn [<digit_$type _value_$value _test>]() {
assert_eq!(<$type as $trait_name>::num(), $value);
}
})*
}
}
try_from_digits! { Thousands; i16, u16, i32, u32, i64, u64, isize, usize, i128, u128; 1000 }
try_from_digits! { HundredThousands; i32, u32, i64, u64, isize, usize, i128, u128; 100_000 }
try_from_digits! { TenBillions; i64, u64, isize, usize, i128, u128; 10_000_000_000 }
try_from_digits! { BigTen; i128; 10_000_000_000_000_000_000 }
try_from_digits! { BigTen; u128; 10_000_000_000_000_000_000 }
try_from_digits! { BigHundred; u128; 100_000_000_000_000_000_000 }
try_from_digits! { BigHundred; i128; 100_000_000_000_000_000_000 }
}