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
// Copyright © 2018–2022 Trevor Spiteri

// This library is free software: you can redistribute it and/or
// modify it under the terms of either
//
//   * the Apache License, Version 2.0 or
//   * the MIT License
//
// at your option.
//
// You should have recieved copies of the Apache License and the MIT
// License along with the library. If not, see
// <https://www.apache.org/licenses/LICENSE-2.0> and
// <https://opensource.org/licenses/MIT>.

use crate::{
    types::extra::{LeEqU128, LeEqU16, LeEqU32, LeEqU64, LeEqU8},
    F128Bits, FixedI128, FixedI16, FixedI32, FixedI64, FixedI8, FixedU128, FixedU16, FixedU32,
    FixedU64, FixedU8,
};
use az_crate::{Cast, CheckedCast, OverflowingCast, SaturatingCast, UnwrappedCast, WrappingCast};
use half::{bf16, f16};

macro_rules! cast {
    ($Src:ident($LeEqUSrc:ident); $Dst:ident($LeEqUDst:ident)) => {
        impl<FracSrc: $LeEqUSrc, FracDst: $LeEqUDst> Cast<$Dst<FracDst>> for $Src<FracSrc> {
            #[inline]
            fn cast(self) -> $Dst<FracDst> {
                self.to_num()
            }
        }

        impl<FracSrc: $LeEqUSrc, FracDst: $LeEqUDst> CheckedCast<$Dst<FracDst>> for $Src<FracSrc> {
            #[inline]
            fn checked_cast(self) -> Option<$Dst<FracDst>> {
                self.checked_to_num()
            }
        }

        impl<FracSrc: $LeEqUSrc, FracDst: $LeEqUDst> SaturatingCast<$Dst<FracDst>>
            for $Src<FracSrc>
        {
            #[inline]
            fn saturating_cast(self) -> $Dst<FracDst> {
                self.saturating_to_num()
            }
        }

        impl<FracSrc: $LeEqUSrc, FracDst: $LeEqUDst> WrappingCast<$Dst<FracDst>> for $Src<FracSrc> {
            #[inline]
            fn wrapping_cast(self) -> $Dst<FracDst> {
                self.wrapping_to_num()
            }
        }

        impl<FracSrc: $LeEqUSrc, FracDst: $LeEqUDst> OverflowingCast<$Dst<FracDst>>
            for $Src<FracSrc>
        {
            #[inline]
            fn overflowing_cast(self) -> ($Dst<FracDst>, bool) {
                self.overflowing_to_num()
            }
        }

        impl<FracSrc: $LeEqUSrc, FracDst: $LeEqUDst> UnwrappedCast<$Dst<FracDst>>
            for $Src<FracSrc>
        {
            #[inline]
            #[track_caller]
            fn unwrapped_cast(self) -> $Dst<FracDst> {
                self.unwrapped_to_num()
            }
        }
    };

    ($Fixed:ident($LeEqU:ident); $Dst:ident) => {
        impl<Frac: $LeEqU> Cast<$Dst> for $Fixed<Frac> {
            #[inline]
            fn cast(self) -> $Dst {
                self.to_num()
            }
        }

        impl<Frac: $LeEqU> CheckedCast<$Dst> for $Fixed<Frac> {
            #[inline]
            fn checked_cast(self) -> Option<$Dst> {
                self.checked_to_num()
            }
        }

        impl<Frac: $LeEqU> SaturatingCast<$Dst> for $Fixed<Frac> {
            #[inline]
            fn saturating_cast(self) -> $Dst {
                self.saturating_to_num()
            }
        }

        impl<Frac: $LeEqU> WrappingCast<$Dst> for $Fixed<Frac> {
            #[inline]
            fn wrapping_cast(self) -> $Dst {
                self.wrapping_to_num()
            }
        }

        impl<Frac: $LeEqU> OverflowingCast<$Dst> for $Fixed<Frac> {
            #[inline]
            fn overflowing_cast(self) -> ($Dst, bool) {
                self.overflowing_to_num()
            }
        }

        impl<Frac: $LeEqU> UnwrappedCast<$Dst> for $Fixed<Frac> {
            #[inline]
            #[track_caller]
            fn unwrapped_cast(self) -> $Dst {
                self.unwrapped_to_num()
            }
        }
    };

    ($Src:ident; $Fixed:ident($LeEqU:ident)) => {
        impl<Frac: $LeEqU> Cast<$Fixed<Frac>> for $Src {
            #[inline]
            fn cast(self) -> $Fixed<Frac> {
                <$Fixed<Frac>>::from_num(self)
            }
        }

        impl<Frac: $LeEqU> CheckedCast<$Fixed<Frac>> for $Src {
            #[inline]
            fn checked_cast(self) -> Option<$Fixed<Frac>> {
                <$Fixed<Frac>>::checked_from_num(self)
            }
        }

        impl<Frac: $LeEqU> SaturatingCast<$Fixed<Frac>> for $Src {
            #[inline]
            fn saturating_cast(self) -> $Fixed<Frac> {
                <$Fixed<Frac>>::saturating_from_num(self)
            }
        }

        impl<Frac: $LeEqU> WrappingCast<$Fixed<Frac>> for $Src {
            #[inline]
            fn wrapping_cast(self) -> $Fixed<Frac> {
                <$Fixed<Frac>>::wrapping_from_num(self)
            }
        }

        impl<Frac: $LeEqU> OverflowingCast<$Fixed<Frac>> for $Src {
            #[inline]
            fn overflowing_cast(self) -> ($Fixed<Frac>, bool) {
                <$Fixed<Frac>>::overflowing_from_num(self)
            }
        }

        impl<Frac: $LeEqU> UnwrappedCast<$Fixed<Frac>> for $Src {
            #[inline]
            #[track_caller]
            fn unwrapped_cast(self) -> $Fixed<Frac> {
                <$Fixed<Frac>>::unwrapped_from_num(self)
            }
        }
    };
}

macro_rules! cast_num {
    ($Src:ident($LeEqUSrc:ident); $($Dst:ident($LeEqUDst:ident),)*) => { $(
        cast! { $Src($LeEqUSrc); $Dst($LeEqUDst) }
    )* };
    ($Fixed:ident($LeEqU:ident); $($Num:ident,)*) => { $(
        cast! { $Fixed($LeEqU); $Num }
        cast! { $Num; $Fixed($LeEqU) }
    )* };
    ($($Fixed:ident($LeEqU:ident),)*) => { $(
        cast_num! {
            $Fixed($LeEqU);
            FixedI8(LeEqU8), FixedI16(LeEqU16), FixedI32(LeEqU32), FixedI64(LeEqU64),
            FixedI128(LeEqU128),
            FixedU8(LeEqU8), FixedU16(LeEqU16), FixedU32(LeEqU32), FixedU64(LeEqU64),
            FixedU128(LeEqU128),
        }
        cast! { bool; $Fixed($LeEqU) }
        cast_num! {
            $Fixed($LeEqU);
            i8, i16, i32, i64, i128, isize,
            u8, u16, u32, u64, u128, usize,
            f16, bf16, f32, f64, F128Bits,
        }
    )* };
}

cast_num! {
    FixedI8(LeEqU8), FixedI16(LeEqU16), FixedI32(LeEqU32), FixedI64(LeEqU64), FixedI128(LeEqU128),
    FixedU8(LeEqU8), FixedU16(LeEqU16), FixedU32(LeEqU32), FixedU64(LeEqU64), FixedU128(LeEqU128),
}