fastnum2/decimal/udec/impls/ops/
add.rs1use core::ops::{Add, AddAssign};
2
3use crate::decimal::UnsignedDecimal;
4
5impl<const N: usize> Add for UnsignedDecimal<N> {
6 type Output = UnsignedDecimal<N>;
7
8 #[inline]
9 fn add(self, rhs: Self) -> UnsignedDecimal<N> {
10 self.add(rhs)
11 }
12}
13
14impl<const N: usize> AddAssign for UnsignedDecimal<N> {
15 #[inline]
16 fn add_assign(&mut self, rhs: Self) {
17 let res = Add::<UnsignedDecimal<N>>::add(*self, rhs);
18 *self = res;
19 }
20}
21
22macro_rules! macro_impl {
23 (FROM $($ty: tt),*) => {
24 $(
25 impl<const N: usize> Add<$ty> for UnsignedDecimal<N> {
26 type Output = UnsignedDecimal<N>;
27
28 #[inline]
29 fn add(self, rhs: $ty) -> UnsignedDecimal<N> {
30 let rhs = UnsignedDecimal::from(rhs);
31 Add::<UnsignedDecimal<N>>::add(self, rhs)
32 }
33 }
34
35 impl<const N: usize> AddAssign<$ty> for UnsignedDecimal<N> {
36 #[inline]
37 fn add_assign(&mut self, rhs: $ty) {
38 let rhs = UnsignedDecimal::from(rhs);
39 self.add_assign(rhs);
40 }
41 }
42 )*
43 };
44 (TRY_FROM $($ty: tt),*) => {
45 $(
46 impl<const N: usize> Add<$ty> for UnsignedDecimal<N> {
47 type Output = UnsignedDecimal<N>;
48
49 #[inline]
50 fn add(self, rhs: $ty) -> UnsignedDecimal<N> {
51 let Ok(rhs) = UnsignedDecimal::try_from(rhs) else {
52 #[cfg(debug_assertions)]
53 panic!(crate::utils::err_msg!(concat!("attempt to add with invalid ", stringify!($ty))));
54
55 #[cfg(not(debug_assertions))]
56 return self;
57 };
58
59 Add::<UnsignedDecimal<N>>::add(self, rhs)
60 }
61 }
62
63 impl<const N: usize> AddAssign<$ty> for UnsignedDecimal<N> {
64 #[inline]
65 fn add_assign(&mut self, rhs: $ty) {
66 let Ok(rhs) = UnsignedDecimal::try_from(rhs) else {
67 #[cfg(debug_assertions)]
68 panic!(crate::utils::err_msg!(concat!("attempt to add with invalid ", stringify!($ty))));
69
70 #[cfg(not(debug_assertions))]
71 return;
72 };
73
74 self.add_assign(rhs);
75 }
76 }
77 )*
78 };
79}
80
81macro_impl!(FROM u8, u16, u32, u64, u128, usize);
82macro_impl!(TRY_FROM i8, i16, i32, i64, i128, isize, f32, f64);