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
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, DeriveInput};

#[proc_macro_derive(UIntWrapper)]
pub fn uint_wrapper_derive(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    let name = &input.ident;

    let gen = quote! {
        impl std::fmt::Display for #name {
            fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
                write!(f, "{}", self.to_uint())
            }
        }

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

            fn add(self, rhs: Self) -> Self::Output {
                Self::new(self.to_uint() + rhs.to_uint())
            }
        }

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

            fn mul(self, rhs: Self) -> Self::Output {
                Self::new(self.to_uint() * rhs.to_uint())
            }
        }

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

            fn sub(self, rhs: Self) -> Self::Output {
                Self::new(self.to_uint() - rhs.to_uint())
            }
        }

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

            fn div(self, rhs: Self) -> Self::Output {
                Self::new(self.to_uint() / rhs.to_uint())
            }
        }

        impl std::ops::Rem for #name {
            type Output = Self;

            fn rem(self, rhs: Self) -> Self::Output {
                Self::new(self.to_uint() % rhs.to_uint())
            }
        }

        impl From<<#name as UIntType>::Uint> for #name {
            fn from(value: <#name as UIntType>::Uint) -> Self {
                Self {
                    inner: BigUint::from(value),
                }
            }
        }

        impl From<#name> for <#name as UIntType>::Uint {
            fn from(value: #name) -> Self {
                value.to_uint()
            }
        }

        impl #name {
            pub fn to_uint(&self) -> <#name as UIntType>::Uint {
                let bigint_bytes_be = self.inner.to_bytes_be();
                let mut bytes = [0u8; std::mem::size_of::<<#name as UIntType>::Uint>()];
                for (i, byte) in bigint_bytes_be.iter().enumerate() {
                    bytes[std::mem::size_of::<<#name as UIntType>::Uint>() - bigint_bytes_be.len() + i] = *byte;
                }
                <#name as UIntType>::Uint::from_be_bytes(bytes)
            }

            pub fn new(num: <#name as UIntType>::Uint) -> Self {
                Self {
                    inner: BigUint::from(num),
                }
            }

            pub fn as_biguint(&self) -> &BigUint {
                &self.inner
            }
        }

        impl TryFrom<BigUint> for #name {
            type Error = crate::Error;

            fn try_from(value: BigUint) -> Result<Self, Self::Error> {
                if value.bits() > std::mem::size_of::<<#name as UIntType>::Uint>() as u64 {
                    return Err(crate::Error::Overflow);
                }
                Ok(Self {
                    inner: value,
                })
            }
        }


        impl From<#name> for BigUint {
            fn from(value: #name) -> Self {
                value.inner
            }
        }
    };

    gen.into()
}