simple_endian/
math_ops.rs1#[allow(unused_imports)]
3use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
4
5#[allow(unused_imports)]
6use super::*;
7
8#[allow(unused_macros)]
9macro_rules! add_math_ops {
10 ($wrap_ty:ty) => {
11 impl Add for $wrap_ty {
12 type Output = Self;
13
14 fn add(self, other: Self) -> Self {
15 Self::from(self.to_native() + other.to_native())
16 }
17 }
18
19 impl AddAssign for $wrap_ty {
20 fn add_assign(&mut self, other: Self) {
21 *self = *self + other;
22 }
23 }
24
25 impl Mul for $wrap_ty {
26 type Output = Self;
27
28 fn mul(self, other: Self) -> Self {
29 Self::from(self.to_native() * other.to_native())
30 }
31 }
32
33 impl MulAssign for $wrap_ty {
34 fn mul_assign(&mut self, other: Self) {
35 *self = *self * other;
36 }
37 }
38
39 impl Div for $wrap_ty {
40 type Output = Self;
41
42 fn div(self, other: Self) -> Self {
43 Self::from(self.to_native() / other.to_native())
44 }
45 }
46
47 impl DivAssign for $wrap_ty {
48 fn div_assign(&mut self, other: Self) {
49 *self = *self / other;
50 }
51 }
52
53 impl Sub for $wrap_ty {
54 type Output = Self;
55
56 fn sub(self, other: Self) -> Self {
57 Self::from(self.to_native() - other.to_native())
58 }
59 }
60 impl SubAssign for $wrap_ty {
61 fn sub_assign(&mut self, other: Self) {
62 *self = *self - other;
63 }
64 }
65
66 }
67}
68
69#[cfg(feature = "big_endian")]
70mod be {
71 use super::*;
72 #[cfg(feature = "byte_impls")]
73 mod bytes {
74 use super::*;
75 add_math_ops!(BigEndian<u8>);
76 add_math_ops!(BigEndian<i8>);
77 }
78
79 #[cfg(feature = "integer_impls")]
80 mod integers {
81 use super::*;
82 add_math_ops!(BigEndian<u16>);
83 add_math_ops!(BigEndian<i16>);
84 add_math_ops!(BigEndian<u32>);
85 add_math_ops!(BigEndian<i32>);
86 add_math_ops!(BigEndian<u64>);
87 add_math_ops!(BigEndian<i64>);
88 add_math_ops!(BigEndian<u128>);
89 add_math_ops!(BigEndian<i128>);
90 add_math_ops!(BigEndian<usize>);
91 add_math_ops!(BigEndian<isize>);
92 }
93
94 #[cfg(feature = "float_impls")]
95 mod floats {
96 use super::*;
97 add_math_ops!(BigEndian<f32>);
98 add_math_ops!(BigEndian<f64>);
99 }
100}
101
102#[cfg(feature = "little_endian")]
103mod le {
104 use super::*;
105 #[cfg(feature = "byte_impls")]
106 mod bytes {
107 use super::*;
108 add_math_ops!(LittleEndian<u8>);
109 add_math_ops!(LittleEndian<i8>);
110 }
111
112 #[cfg(feature = "integer_impls")]
113 mod integers {
114 use super::*;
115 add_math_ops!(LittleEndian<u16>);
116 add_math_ops!(LittleEndian<i16>);
117 add_math_ops!(LittleEndian<u32>);
118 add_math_ops!(LittleEndian<i32>);
119 add_math_ops!(LittleEndian<u64>);
120 add_math_ops!(LittleEndian<i64>);
121 add_math_ops!(LittleEndian<u128>);
122 add_math_ops!(LittleEndian<i128>);
123 add_math_ops!(LittleEndian<usize>);
124 add_math_ops!(LittleEndian<isize>);
125 }
126
127 #[cfg(feature = "float_impls")]
128 mod floats {
129 use super::*;
130 add_math_ops!(LittleEndian<f32>);
131 add_math_ops!(LittleEndian<f64>);
132 }
133}
134
135#[cfg(test)]
136mod tests {
137 extern crate test;
138 use crate::*;
139
140 #[test]
141 fn add_fp_be() {
142 let mut be1 = f64be::from(1234.5678);
143 be1 = be1 + 1.0.into();
144 be1 += 1.0.into();
145 assert_eq!(be1, 1236.5678.into());
146 }
147
148 #[test]
149 fn subtract_fp_be() {
150 let mut be1 = f64be::from(1234.5678);
151 be1 = be1 - 1.0.into();
152 be1 -= 1.0.into();
153 assert_eq!(be1, 1232.5678.into());
154 }
155
156 #[test]
157 fn mul_fp_be() {
158 let mut be1 = f64be::from(1234.5678);
159 be1 = be1 * 10.0.into();
160 be1 *= 10.0.into();
161 assert_eq!(be1, 123456.78.into());
162 }
163
164 #[test]
165 fn div_fp_be() {
166 let mut ne1: f64 = 1234.5678;
167 let mut be1 = f64be::from(ne1);
168 be1 = be1 / 10.0.into();
169 ne1 = ne1 / 10.0;
170 be1 /= 10.0.into();
171 ne1 /= 10.0;
172 assert_eq!(ne1, be1.into());
173 }
174
175
176}