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
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 signed_int_conversions = [
        "i128", "i64", "i32", "i16", "i8", "isize"
    ].into_iter().map(|signed| {
            let signed = format_ident!("{signed}");
            quote! {
                impl<const BASE: usize> ::std::convert::From<#signed> for #name<BASE> {
                    fn from(value: #signed) -> Self {
                        <#name<BASE> as ::big_int::BigInt<BASE>>::from_i128_inner(value as i128)
                    }
                }

                impl<const BASE: usize> ::std::convert::From<#name<BASE>> for #signed {
                    fn from(value: #name<BASE>) -> Self {
                        <#name<BASE> as ::big_int::BigInt<BASE>>::into_i128_inner(value) as #signed
                    }
                }
            }
        });

    let unsigned_int_conversions = [
        "u128", "u64", "u32", "u16", "u8", "usize",
    ].into_iter().map(|unsigned| {
        let unsigned = format_ident!("{unsigned}");
        quote! {
            impl<const BASE: usize> ::std::convert::From<#unsigned> for #name<BASE> {
                fn from(value: #unsigned) -> Self {
                    <#name<BASE> as ::big_int::BigInt<BASE>>::from_u128_inner(value as u128)
                }
            }

            impl<const BASE: usize> ::std::convert::From<#name<BASE>> for #unsigned {
                fn from(value: #name<BASE>) -> Self {
                    <#name<BASE> as ::big_int::BigInt<BASE>>::into_u128_inner(value) as #unsigned
                }
            }
        }
    });

    let gen = quote! {
        impl<const BASE: usize> ::big_int::get_back::GetBack for #name<BASE> {
            type Item = ::big_int::Digit;

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

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

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

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

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

        impl<const BASE: usize> ::std::ops::Neg for #name<BASE> {
            type Output = Self;

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

        impl<const BASE: usize> ::std::ops::Add for #name<BASE> {
            type Output = Self;

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

        impl<const BASE: usize> ::std::ops::AddAssign for #name<BASE> {
            fn add_assign(&mut self, rhs: Self) {
                <#name<BASE> as ::big_int::BigInt<BASE>>::add_assign_inner(self, rhs)
            }
        }

        impl<const BASE: usize> ::std::ops::Sub for #name<BASE> {
            type Output = Self;

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

        impl<const BASE: usize> ::std::ops::SubAssign for #name<BASE> {
            fn sub_assign(&mut self, rhs: Self) {
                <#name<BASE> as ::big_int::BigInt<BASE>>::sub_assign_inner(self, rhs)
            }
        }

        impl<const BASE: usize> ::std::ops::Mul for #name<BASE> {
            type Output = Self;

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

        impl<const BASE: usize> ::std::ops::MulAssign for #name<BASE> {
            fn mul_assign(&mut self, rhs: Self) {
                <#name<BASE> as ::big_int::BigInt<BASE>>::mul_assign_inner(self, rhs)
            }
        }

        impl<const BASE: usize> ::std::ops::Div for #name<BASE> {
            type Output = Self;

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

        impl<const BASE: usize> ::std::ops::DivAssign for #name<BASE> {
            fn div_assign(&mut self, rhs: Self) {
                <#name<BASE> as ::big_int::BigInt<BASE>>::div_assign_inner(self, rhs)
            }
        }

        impl<const BASE: usize> ::std::ops::Shl for #name<BASE> {
            type Output = Self;

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

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

        impl<const BASE: usize> ::std::ops::Shr for #name<BASE> {
            type Output = Self;

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

        impl<const BASE: usize> ::std::ops::ShrAssign for #name<BASE> {
            fn shr_assign(&mut self, rhs: Self) {
                <#name<BASE> as ::big_int::BigInt<BASE>>::shr_assign_inner(self, rhs.into())
            }
        }

        impl<const BASE: usize> ::std::str::FromStr for #name<BASE> {
            type Err = ::big_int::error::BigIntError;

            fn from_str(s: &str) -> ::std::result::Result<Self, Self::Err> {
                <#name<BASE> as ::big_int::BigInt<BASE>>::from_str_inner(s)
            }
        }

        impl<const BASE: usize> ::std::iter::FromIterator<::big_int::Digit> for #name<BASE> {
            fn from_iter<T: IntoIterator<Item = ::big_int::Digit>>(iter: T) -> Self {
                <#name<BASE> as ::big_int::BigInt<BASE>>::from_iter_inner(iter)
            }
        }

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

        #(#signed_int_conversions)*
        #(#unsigned_int_conversions)*
    };
    gen.into()
}