tstr/to_uint/
impl_no_const_generics.rs

1use super::{Sealed, ToUint};
2
3#[cfg(not(feature = "min_const_generics"))]
4macro_rules! impl_to_digit {
5    ($($ty:ident = $val:tt,)*) => (
6        $(
7            impl Sealed for crate::$ty {}
8
9            impl ToUint for crate::$ty {
10                const U128: u128 = $val;
11                const USIZE: usize = $val;
12                const DIGITS: u32 = 1;
13            }
14        )*
15    )
16}
17
18#[cfg(not(feature = "min_const_generics"))]
19impl_to_digit! {
20    __0 = 0,
21    __1 = 1,
22    __2 = 2,
23    __3 = 3,
24    __4 = 4,
25    __5 = 5,
26    __6 = 6,
27    __7 = 7,
28    __8 = 8,
29    __9 = 9,
30}
31
32#[cfg(feature = "min_const_generics")]
33macro_rules! impl_to_digits_const {
34    (
35        [$($digit:literal => $value:literal,)*]
36        [
37            $( ($chars_structs:ident, [$($chars:ident),*], $len:expr) ,)*
38        ]
39    )=>{
40        use crate::__a;
41
42        $(
43            impl Sealed for __a<$digit> {}
44
45            impl ToUint for __a<$digit> {
46                const USIZE: usize = $value;
47                const U128: u128 = $value;
48                const DIGITS: u32 = 1;
49            }
50        )*
51
52        $(
53            impl<$(const $chars: char,)*> Sealed for crate::$chars_structs<$($chars,)*> {}
54
55            impl<$(const $chars: char,)*> ToUint for crate::$chars_structs<$($chars,)*>
56            where
57                $(__a<$chars>: ToUint,)*
58            {
59                const U128: u128 = {
60                    #[allow(unused_mut)]
61                    let mut sum = 0u128;
62                    $(
63                        sum = __a::<$chars>::U128 + sum * ten_pow(__a::<$chars>::DIGITS);
64                    )*
65                    sum
66                };
67                const DIGITS: u32 = $len;
68            }
69        )*
70    }
71}
72
73#[cfg(feature = "min_const_generics")]
74impl_to_digits_const! {
75    [
76        '0' => 0,
77        '1' => 1,
78        '2' => 2,
79        '3' => 3,
80        '4' => 4,
81        '5' => 5,
82        '6' => 6,
83        '7' => 7,
84        '8' => 8,
85        '9' => 9,
86    ]
87    [
88        (__b, [A,B], 2),
89        (__c, [A,B,C], 3),
90        (__d, [A,B,C,D], 4),
91        (__e, [A,B,C,D,E], 5),
92        (__f, [A,B,C,D,E,F], 6),
93        (__g, [A,B,C,D,E,F,G], 7),
94        (__ , [A,B,C,D,E,F,G,H], 8),
95    ]
96}
97
98/*
99fn main(){
100    let mut accum = 1u128;
101    println!("    {},", accum);
102    while let Some(next) = accum.checked_mul(10) {
103        println!("    {},", next);
104        accum = next;
105    }
106}
107*/
108const POW_TEN: &[u128; 39] = &[
109    1,
110    10,
111    100,
112    1000,
113    10000,
114    100000,
115    1000000,
116    10000000,
117    100000000,
118    1000000000,
119    10000000000,
120    100000000000,
121    1000000000000,
122    10000000000000,
123    100000000000000,
124    1000000000000000,
125    10000000000000000,
126    100000000000000000,
127    1000000000000000000,
128    10000000000000000000,
129    100000000000000000000,
130    1000000000000000000000,
131    10000000000000000000000,
132    100000000000000000000000,
133    1000000000000000000000000,
134    10000000000000000000000000,
135    100000000000000000000000000,
136    1000000000000000000000000000,
137    10000000000000000000000000000,
138    100000000000000000000000000000,
139    1000000000000000000000000000000,
140    10000000000000000000000000000000,
141    100000000000000000000000000000000,
142    1000000000000000000000000000000000,
143    10000000000000000000000000000000000,
144    100000000000000000000000000000000000,
145    1000000000000000000000000000000000000,
146    10000000000000000000000000000000000000,
147    100000000000000000000000000000000000000,
148];
149
150const fn ten_pow(power: u32) -> u128 {
151    POW_TEN[power as usize]
152}
153
154macro_rules! tuple_impl {
155    ($($ty:ident)*) => (
156        impl<$($ty,)*> Sealed for ($($ty,)*)
157        where
158            $($ty: Sealed,)*
159        {}
160
161        #[doc(hidden)]
162        impl<$($ty,)*> ToUint for ($($ty,)*)
163        where
164            $($ty: ToUint,)*
165        {
166            const U128: u128 = {
167                #[allow(unused_mut)]
168                let mut sum = 0u128;
169                $(
170                    sum = $ty::U128 + sum * ten_pow($ty::DIGITS);
171                )*
172                sum
173            };
174            const DIGITS: u32 = 0 $( + $ty::DIGITS )*;
175        }
176    )
177}
178
179tuple_impl! {}
180tuple_impl! {A }
181tuple_impl! {A B}
182tuple_impl! {A B C}
183tuple_impl! {A B C D}
184tuple_impl! {A B C D E}
185tuple_impl! {A B C D E F}
186tuple_impl! {A B C D E F G}
187tuple_impl! {A B C D E F G H}