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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
use proc_macro::TokenStream;
use quote::{format_ident, quote};
use syn::*;

#[proc_macro_derive(BigIntTraits)]
pub fn auto_big_int_derive(input: TokenStream) -> TokenStream {
    let ast = parse_macro_input!(input as DeriveInput);
    let name = &ast.ident;
    let (impl_generics, ty_generics, where_clause) = &ast.generics.split_for_impl();

    let int_conversions = [
        (
            "from_i128_inner",
            "into_i128_inner",
            "i128",
            ["i128", "i64", "i32", "i16", "i8", "isize"],
        ),
        (
            "from_u128_inner",
            "into_u128_inner",
            "u128",
            ["u128", "u64", "u32", "u16", "u8", "usize"],
        ),
    ]
    .into_iter()
    .map(|(from, into, from_target_type, int_types)| {
        let from = format_ident!("{from}");
        let into = format_ident!("{into}");
        let from_target_type = format_ident!("{from_target_type}");
        int_types.map(|int| {
            let int = format_ident!("{int}");
            quote! {
                impl #impl_generics ::std::convert::From<#int> for #name #ty_generics #where_clause {
                    fn from(value: #int) -> Self {
                        <<#name #ty_generics as ::big_int::BigInt<BASE>>::Denormal as ::big_int::Unwrap<#name #ty_generics>>::unwrap(
                            <#name #ty_generics as ::big_int::BigInt<BASE>>::#from(value as #from_target_type)
                        )
                    }
                }

                impl #impl_generics ::std::convert::From<#name #ty_generics> for #int {
                    fn from(value: #name #ty_generics) -> Self {
                        <#name #ty_generics as ::big_int::BigInt<BASE>>::#into(value) as #int
                    }
                }
            }
        })
    })
    .flatten();

    let gen = quote! {
        impl #impl_generics ::big_int::get_back::GetBack for #name #ty_generics #where_clause {
            type Item = ::big_int::Digit;

            fn get_back(&self, index: usize) -> Option<::big_int::Digit> {
                <#name #ty_generics as ::big_int::BigInt<BASE>>::get_back_inner(self, index)
            }
        }

        impl #impl_generics ::std::default::Default for #name #ty_generics #where_clause {
            fn default() -> Self {
                <#name #ty_generics as ::big_int::BigInt<BASE>>::default_inner()
            }
        }

        impl #impl_generics ::std::fmt::Display for #name #ty_generics #where_clause {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                <#name #ty_generics as ::big_int::BigInt<BASE>>::fmt_inner(self, f)
            }
        }

        impl #impl_generics ::std::cmp::PartialOrd for #name #ty_generics #where_clause {
            fn partial_cmp(&self, other: &Self) -> ::std::option::Option<::std::cmp::Ordering> {
                <#name #ty_generics as ::big_int::BigInt<BASE>>::partial_cmp_inner(self, other)
            }
        }

        impl #impl_generics ::std::cmp::Ord for #name #ty_generics #where_clause {
            fn cmp(&self, other: &Self) -> ::std::cmp::Ordering {
                <#name #ty_generics as ::big_int::BigInt<BASE>>::cmp_inner(self, other)
            }
        }

        impl #impl_generics ::std::cmp::PartialEq for #name #ty_generics #where_clause {
            fn eq(&self, other: &Self) -> bool {
                <#name #ty_generics as ::big_int::BigInt<BASE>>::eq_inner(self, other)
            }
        }

        impl #impl_generics ::std::cmp::Eq for #name #ty_generics #where_clause {}

        impl #impl_generics ::std::ops::Neg for #name #ty_generics #where_clause {
            type Output = Self;

            fn neg(self) -> Self::Output {
                <<#name #ty_generics as ::big_int::BigInt<BASE>>::Denormal as ::big_int::Unwrap<#name #ty_generics>>::unwrap(
                    <#name #ty_generics as ::big_int::BigInt<BASE>>::neg_inner(self)
                )
            }
        }

        impl #impl_generics ::std::ops::Add for #name #ty_generics #where_clause {
            type Output = Self;

            fn add(self, rhs: Self) -> Self::Output {
                <<#name #ty_generics as ::big_int::BigInt<BASE>>::Denormal as ::big_int::Unwrap<#name #ty_generics>>::unwrap(
                    <#name #ty_generics as ::big_int::BigInt<BASE>>::add_inner::<Self, Self>(self, rhs)
                )
            }
        }

        impl #impl_generics ::std::ops::AddAssign for #name #ty_generics #where_clause {
            fn add_assign(&mut self, rhs: Self) {
                unsafe {
                    <#name #ty_generics as ::big_int::BigInt<BASE>>::add_assign_inner(self, rhs);
                }
                <#name #ty_generics as ::big_int::BigInt<BASE>>::normalize(self);
            }
        }

        impl #impl_generics ::std::ops::Sub for #name #ty_generics #where_clause {
            type Output = Self;

            fn sub(mut self, rhs: Self) -> Self::Output {
                <<#name #ty_generics as ::big_int::BigInt<BASE>>::Denormal as ::big_int::Unwrap<#name #ty_generics>>::unwrap(
                    <#name #ty_generics as ::big_int::BigInt<BASE>>::sub_inner::<Self, Self>(self, rhs)
                )
            }
        }

        impl #impl_generics ::std::ops::SubAssign for #name #ty_generics #where_clause {
            fn sub_assign(&mut self, rhs: Self) {
                unsafe {
                    <#name #ty_generics as ::big_int::BigInt<BASE>>::sub_assign_inner(self, rhs)
                }
                <#name #ty_generics as ::big_int::BigInt<BASE>>::normalize(self);
            }
        }

        impl #impl_generics ::std::ops::Mul for #name #ty_generics #where_clause {
            type Output = Self;

            fn mul(mut self, rhs: Self) -> Self::Output {
                <<#name #ty_generics as ::big_int::BigInt<BASE>>::Denormal as ::big_int::Unwrap<#name #ty_generics>>::unwrap(
                    <#name #ty_generics as ::big_int::BigInt<BASE>>::mul_inner::<Self, Self>(self, rhs)
                )
            }
        }

        impl #impl_generics ::std::ops::MulAssign for #name #ty_generics #where_clause {
            fn mul_assign(&mut self, rhs: Self) {
                unsafe {
                    <#name #ty_generics as ::big_int::BigInt<BASE>>::mul_assign_inner(self, rhs)
                }
                <#name #ty_generics as ::big_int::BigInt<BASE>>::normalize(self);
            }
        }

        impl #impl_generics ::std::ops::Div for #name #ty_generics #where_clause {
            type Output = Self;

            fn div(self, rhs: Self) -> Self::Output {
                <<#name #ty_generics as ::big_int::BigInt<BASE>>::Denormal as ::big_int::Unwrap<#name #ty_generics>>::unwrap(
                    <#name #ty_generics as ::big_int::BigInt<BASE>>::div_inner::<Self, Self>(self, rhs)
                )
            }
        }

        impl #impl_generics ::std::ops::DivAssign for #name #ty_generics #where_clause {
            fn div_assign(&mut self, rhs: Self) {
                unsafe {
                    <#name #ty_generics as ::big_int::BigInt<BASE>>::div_assign_inner(self, rhs)
                }
                <#name #ty_generics as ::big_int::BigInt<BASE>>::normalize(self);
            }
        }

        impl #impl_generics ::std::ops::Shl for #name #ty_generics #where_clause {
            type Output = Self;

            fn shl(self, rhs: Self) -> Self::Output {
                <#name #ty_generics as ::big_int::BigInt<BASE>>::shl_inner(self, rhs.into())
            }
        }

        impl #impl_generics ::std::ops::ShlAssign for #name #ty_generics #where_clause {
            fn shl_assign(&mut self, rhs: Self) {
                <#name #ty_generics as ::big_int::BigInt<BASE>>::shl_assign_inner(self, rhs.into())
            }
        }

        impl #impl_generics ::std::ops::Shr for #name #ty_generics #where_clause {
            type Output = Self;

            fn shr(self, rhs: Self) -> Self::Output {
                <<#name #ty_generics as ::big_int::BigInt<BASE>>::Denormal as ::big_int::Unwrap<#name #ty_generics>>::unwrap(
                    <#name #ty_generics as ::big_int::BigInt<BASE>>::shr_inner(self, rhs.into())
                )
            }
        }

        impl #impl_generics ::std::ops::ShrAssign for #name #ty_generics #where_clause {
            fn shr_assign(&mut self, rhs: Self) {
                unsafe {
                    <#name #ty_generics as ::big_int::BigInt<BASE>>::shr_assign_inner(self, rhs.into())
                }
                <#name #ty_generics as ::big_int::BigInt<BASE>>::normalize(self);
            }
        }

        impl #impl_generics ::std::str::FromStr for #name #ty_generics #where_clause {
            type Err = ::big_int::error::BigIntError;

            fn from_str(s: &str) -> ::std::result::Result<Self, Self::Err> {
                <#name #ty_generics as ::big_int::BigInt<BASE>>::from_str_inner(s).map(|i|
                    <<#name #ty_generics as ::big_int::BigInt<BASE>>::Denormal as ::big_int::Unwrap<#name #ty_generics>>::unwrap(i)
                )
            }
        }

        impl #impl_generics ::std::iter::FromIterator<::big_int::Digit> for #name #ty_generics #where_clause {
            fn from_iter<T: IntoIterator<Item = ::big_int::Digit>>(iter: T) -> Self {
                <<#name #ty_generics as ::big_int::BigInt<BASE>>::Denormal as ::big_int::Unwrap<#name #ty_generics>>::unwrap(
                    <#name #ty_generics as ::big_int::BigInt<BASE>>::from_iter_inner(iter)
                )
            }
        }

        impl #impl_generics ::std::iter::Iterator for #name #ty_generics #where_clause {
            type Item = ::big_int::Digit;

            fn next(&mut self) -> Option<Self::Item> {
                unsafe {
                    <#name #ty_generics as ::big_int::BigInt<BASE>>::next_inner(self)
                }
            }
        }

        impl #impl_generics ::std::iter::DoubleEndedIterator for #name #ty_generics #where_clause {
            fn next_back(&mut self) -> Option<Self::Item> {
                unsafe {
                    <#name #ty_generics as ::big_int::BigInt<BASE>>::next_back_inner(self)
                }
            }
        }

        impl #impl_generics ::std::convert::From<::std::vec::Vec<::big_int::Digit>> for #name #ty_generics #where_clause {
            fn from(value: ::std::vec::Vec<::big_int::Digit>) -> Self {
                value.into_iter().collect()
            }
        }

        #(#int_conversions)*
    };
    gen.into()
}