|
1 |
| -use forward_ref::{forward_ref_binop, forward_ref_op_assign}; |
2 |
| -use schemars::JsonSchema; |
3 |
| -use serde::{de, ser, Deserialize, Deserializer, Serialize}; |
4 | 1 | use std::fmt::{self};
|
5 | 2 | use std::ops::{
|
6 | 3 | Add, AddAssign, Div, DivAssign, Mul, MulAssign, Rem, RemAssign, Shr, ShrAssign, Sub, SubAssign,
|
7 | 4 | };
|
8 | 5 | use std::str::FromStr;
|
9 | 6 |
|
| 7 | +use forward_ref::{forward_ref_binop, forward_ref_op_assign}; |
| 8 | +use schemars::JsonSchema; |
| 9 | +use serde::{de, ser, Deserialize, Deserializer, Serialize}; |
| 10 | + |
| 11 | +use crate::errors::CheckedMultiplyFractionalError; |
10 | 12 | use crate::errors::{
|
11 |
| - CheckedMultiplyFractionalError, CheckedMultiplyRatioError, DivideByZeroError, OverflowError, |
12 |
| - OverflowOperation, StdError, |
| 13 | + CheckedMultiplyRatioError, DivideByZeroError, OverflowError, OverflowOperation, StdError, |
13 | 14 | };
|
14 |
| -use crate::{ConversionOverflowError, Fractional, Uint256, Uint64}; |
| 15 | +use crate::math::fraction::FractionMath; |
| 16 | +use crate::{fraction_math, ConversionOverflowError, Fractional, Uint256, Uint512, Uint64}; |
15 | 17 |
|
16 | 18 | /// A thin wrapper around u128 that is using strings for JSON encoding/decoding,
|
17 | 19 | /// such that the full u128 range can be used for clients that convert JSON numbers to floats,
|
@@ -137,37 +139,6 @@ impl Uint128 {
|
137 | 139 | .unwrap()
|
138 | 140 | }
|
139 | 141 |
|
140 |
| - pub fn mul_floored<F: Fractional<T>, T: Into<u128>>(self, rhs: F) -> Self { |
141 |
| - self.checked_mul_floored(rhs).unwrap() |
142 |
| - } |
143 |
| - |
144 |
| - pub fn checked_mul_floored<F: Fractional<T>, T: Into<u128>>( |
145 |
| - self, |
146 |
| - rhs: F, |
147 |
| - ) -> Result<Self, CheckedMultiplyFractionalError> { |
148 |
| - let res = self |
149 |
| - .full_mul(rhs.numerator()) |
150 |
| - .checked_div(Uint256::from(rhs.denominator().into()))?; |
151 |
| - Ok(res.try_into()?) |
152 |
| - } |
153 |
| - |
154 |
| - pub fn mul_ceil<F: Fractional<T> + Clone, T: Into<u128>>(self, rhs: F) -> Self { |
155 |
| - self.checked_mul_ceil(rhs).unwrap() |
156 |
| - } |
157 |
| - |
158 |
| - pub fn checked_mul_ceil<F: Fractional<T> + Clone, T: Into<u128>>( |
159 |
| - self, |
160 |
| - rhs: F, |
161 |
| - ) -> Result<Self, CheckedMultiplyFractionalError> { |
162 |
| - let mut result = self.checked_mul_floored(rhs.clone())?; |
163 |
| - let numerator = Uint256::from(rhs.numerator().into()); |
164 |
| - let denominator = Uint256::from(rhs.denominator().into()); |
165 |
| - if !numerator.checked_rem(denominator)?.is_zero() { |
166 |
| - result += Uint128::one(); |
167 |
| - }; |
168 |
| - Ok(result) |
169 |
| - } |
170 |
| - |
171 | 142 | pub fn checked_add(self, other: Self) -> Result<Self, OverflowError> {
|
172 | 143 | self.0
|
173 | 144 | .checked_add(other.0)
|
@@ -262,6 +233,8 @@ impl Uint128 {
|
262 | 233 | }
|
263 | 234 | }
|
264 | 235 |
|
| 236 | +fraction_math!(Uint128); |
| 237 | + |
265 | 238 | // `From<u{128,64,32,16,8}>` is implemented manually instead of
|
266 | 239 | // using `impl<T: Into<u128>> From<T> for Uint128` because
|
267 | 240 | // of the conflict with `TryFrom<&str>` as described here
|
@@ -569,11 +542,12 @@ impl PartialEq<Uint128> for &Uint128 {
|
569 | 542 |
|
570 | 543 | #[cfg(test)]
|
571 | 544 | mod tests {
|
572 |
| - use super::*; |
573 | 545 | use crate::errors::CheckedMultiplyFractionalError::{ConversionOverflow, DivideByZero};
|
574 |
| - use crate::math::fraction::Fraction; |
| 546 | + use crate::math::fraction::{Fraction, FractionMath}; |
575 | 547 | use crate::{from_slice, to_vec, Decimal};
|
576 | 548 |
|
| 549 | + use super::*; |
| 550 | + |
577 | 551 | #[test]
|
578 | 552 | fn size_of_works() {
|
579 | 553 | assert_eq!(std::mem::size_of::<Uint128>(), 16);
|
@@ -1115,7 +1089,7 @@ mod tests {
|
1115 | 1089 | assert_eq!(
|
1116 | 1090 | Uint128::MAX.checked_mul_floored(fraction),
|
1117 | 1091 | Err(ConversionOverflow(ConversionOverflowError {
|
1118 |
| - source_type: "Uint256", |
| 1092 | + source_type: "Uint512", |
1119 | 1093 | target_type: "Uint128",
|
1120 | 1094 | value: "893241213167463466591358344508391555069".to_string()
|
1121 | 1095 | })),
|
@@ -1181,7 +1155,7 @@ mod tests {
|
1181 | 1155 | assert_eq!(
|
1182 | 1156 | Uint128::MAX.checked_mul_ceil(fraction),
|
1183 | 1157 | Err(ConversionOverflow(ConversionOverflowError {
|
1184 |
| - source_type: "Uint256", |
| 1158 | + source_type: "Uint512", |
1185 | 1159 | target_type: "Uint128",
|
1186 | 1160 | value: "893241213167463466591358344508391555069".to_string()
|
1187 | 1161 | })),
|
|
0 commit comments