vildrose_core/word/
cross_width.rs1use super::{CheckedDiv, Tryte, Word27, Word54};
2use crate::ops::widen;
3use core::ops;
4
5macro_rules! impl_widening_arithmetic {
6 ($narrow:ty => $wide:ty, $wide_width:literal) => {
7 impl From<$narrow> for $wide {
8 fn from(value: $narrow) -> Self {
9 <$wide>::new(widen::<{ <$narrow>::TRIT_COUNT }, $wide_width>(
10 value.into_trits(),
11 ))
12 }
13 }
14
15 impl ops::Add<$wide> for $narrow {
16 type Output = $wide;
17
18 fn add(self, rhs: $wide) -> Self::Output {
19 let lhs: $wide = self.into();
20 lhs + rhs
21 }
22 }
23
24 impl ops::Add<$narrow> for $wide {
25 type Output = $wide;
26
27 fn add(self, rhs: $narrow) -> Self::Output {
28 let rhs: $wide = rhs.into();
29 self + rhs
30 }
31 }
32
33 impl ops::Sub<$wide> for $narrow {
34 type Output = $wide;
35
36 fn sub(self, rhs: $wide) -> Self::Output {
37 let lhs: $wide = self.into();
38 lhs - rhs
39 }
40 }
41
42 impl ops::Sub<$narrow> for $wide {
43 type Output = $wide;
44
45 fn sub(self, rhs: $narrow) -> Self::Output {
46 let rhs: $wide = rhs.into();
47 self - rhs
48 }
49 }
50
51 impl ops::Mul<$wide> for $narrow {
52 type Output = $wide;
53
54 fn mul(self, rhs: $wide) -> Self::Output {
55 let lhs: $wide = self.into();
56 lhs * rhs
57 }
58 }
59
60 impl ops::Mul<$narrow> for $wide {
61 type Output = $wide;
62
63 fn mul(self, rhs: $narrow) -> Self::Output {
64 let rhs: $wide = rhs.into();
65 self * rhs
66 }
67 }
68
69 impl CheckedDiv<$wide> for $narrow {
70 type Output = $wide;
71
72 fn checked_div(self, rhs: $wide) -> Option<Self::Output> {
73 let lhs: $wide = self.into();
74 <$wide as CheckedDiv<$wide>>::checked_div(lhs, rhs)
75 }
76 }
77
78 impl CheckedDiv<$narrow> for $wide {
79 type Output = $wide;
80
81 fn checked_div(self, rhs: $narrow) -> Option<Self::Output> {
82 let rhs: $wide = rhs.into();
83 <$wide as CheckedDiv<$wide>>::checked_div(self, rhs)
84 }
85 }
86
87 impl ops::Div<$wide> for $narrow {
88 type Output = $wide;
89
90 fn div(self, rhs: $wide) -> Self::Output {
91 let lhs: $wide = self.into();
92 lhs / rhs
93 }
94 }
95
96 impl ops::Div<$narrow> for $wide {
97 type Output = $wide;
98
99 fn div(self, rhs: $narrow) -> Self::Output {
100 let rhs: $wide = rhs.into();
101 self / rhs
102 }
103 }
104 };
105}
106
107impl_widening_arithmetic!(Tryte => Word27, 27);
108impl_widening_arithmetic!(Tryte => Word54, 54);
109impl_widening_arithmetic!(Word27 => Word54, 54);