Skip to content

Commit 77661a2

Browse files
committed
Deprecate Uint256::from_u128 and Int256::from_i128
1 parent 497d75b commit 77661a2

File tree

5 files changed

+43
-43
lines changed

5 files changed

+43
-43
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ and this project adheres to
4949
- cosmwasm-std: Deprecate `{Decimal,Decimal256}::raw` and
5050
`{SignedDecimal,SignedDecimal256}::raw` in favour of e.g.
5151
`Decimal::new(Uint128::new(value))`. ([#2399])
52+
- cosmwasm-std: Deprecate `Uint256::from_u128` and `Int256::from_i128` in favour
53+
of `::new`. ([#2399])
5254

5355
## Fixed
5456

packages/std/src/math/decimal256.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ pub struct Decimal256RangeExceeded;
3434

3535
impl Decimal256 {
3636
const DECIMAL_FRACTIONAL: Uint256 = // 1*10**18
37-
Uint256::from_u128(1_000_000_000_000_000_000);
37+
Uint256::new(1_000_000_000_000_000_000);
3838
const DECIMAL_FRACTIONAL_SQUARED: Uint256 = // 1*10**36
39-
Uint256::from_u128(1_000_000_000_000_000_000_000_000_000_000_000_000);
39+
Uint256::new(1_000_000_000_000_000_000_000_000_000_000_000_000);
4040

4141
/// The number of decimal places. Since decimal types are fixed-point rather than
4242
/// floating-point, this is a constant.
@@ -70,7 +70,7 @@ impl Decimal256 {
7070
note = "Use Decimal256::new(Uint256::new(value)) instead"
7171
)]
7272
pub const fn raw(value: u128) -> Self {
73-
Self(Uint256::from_u128(value))
73+
Self(Uint256::new(value))
7474
}
7575

7676
/// Create a 1.0 Decimal256
@@ -99,7 +99,7 @@ impl Decimal256 {
9999
pub const fn percent(x: u64) -> Self {
100100
// multiplication does not overflow since `u64::MAX` * 10**16 is well in u128 range
101101
let atomics = (x as u128) * 10_000_000_000_000_000;
102-
Self(Uint256::from_u128(atomics))
102+
Self(Uint256::new(atomics))
103103
}
104104

105105
/// Convert permille (x/1000) into Decimal256
@@ -116,7 +116,7 @@ impl Decimal256 {
116116
pub const fn permille(x: u64) -> Self {
117117
// multiplication does not overflow since `u64::MAX` * 10**15 is well in u128 range
118118
let atomics = (x as u128) * 1_000_000_000_000_000;
119-
Self(Uint256::from_u128(atomics))
119+
Self(Uint256::new(atomics))
120120
}
121121

122122
/// Convert basis points (x/10000) into Decimal256
@@ -135,7 +135,7 @@ impl Decimal256 {
135135
pub const fn bps(x: u64) -> Self {
136136
// multiplication does not overflow since `u64::MAX` * 10**14 is well in u128 range
137137
let atomics = (x as u128) * 100_000_000_000_000;
138-
Self(Uint256::from_u128(atomics))
138+
Self(Uint256::new(atomics))
139139
}
140140

141141
/// Creates a decimal from a number of atomic units and the number
@@ -2234,18 +2234,18 @@ mod tests {
22342234
#[test]
22352235
fn decimal256_to_uint_floor_works() {
22362236
let d = Decimal256::from_str("12.000000000000000001").unwrap();
2237-
assert_eq!(d.to_uint_floor(), Uint256::from_u128(12));
2237+
assert_eq!(d.to_uint_floor(), Uint256::new(12));
22382238
let d = Decimal256::from_str("12.345").unwrap();
2239-
assert_eq!(d.to_uint_floor(), Uint256::from_u128(12));
2239+
assert_eq!(d.to_uint_floor(), Uint256::new(12));
22402240
let d = Decimal256::from_str("12.999").unwrap();
2241-
assert_eq!(d.to_uint_floor(), Uint256::from_u128(12));
2241+
assert_eq!(d.to_uint_floor(), Uint256::new(12));
22422242
let d = Decimal256::from_str("0.98451384").unwrap();
2243-
assert_eq!(d.to_uint_floor(), Uint256::from_u128(0));
2243+
assert_eq!(d.to_uint_floor(), Uint256::new(0));
22442244

22452245
let d = Decimal256::from_str("75.0").unwrap();
2246-
assert_eq!(d.to_uint_floor(), Uint256::from_u128(75));
2246+
assert_eq!(d.to_uint_floor(), Uint256::new(75));
22472247
let d = Decimal256::from_str("0.0").unwrap();
2248-
assert_eq!(d.to_uint_floor(), Uint256::from_u128(0));
2248+
assert_eq!(d.to_uint_floor(), Uint256::new(0));
22492249

22502250
let d = Decimal256::MAX;
22512251
assert_eq!(
@@ -2283,16 +2283,16 @@ mod tests {
22832283
#[test]
22842284
fn decimal256_to_uint_ceil_works() {
22852285
let d = Decimal256::from_str("12.000000000000000001").unwrap();
2286-
assert_eq!(d.to_uint_ceil(), Uint256::from_u128(13));
2286+
assert_eq!(d.to_uint_ceil(), Uint256::new(13));
22872287
let d = Decimal256::from_str("12.345").unwrap();
2288-
assert_eq!(d.to_uint_ceil(), Uint256::from_u128(13));
2288+
assert_eq!(d.to_uint_ceil(), Uint256::new(13));
22892289
let d = Decimal256::from_str("12.999").unwrap();
2290-
assert_eq!(d.to_uint_ceil(), Uint256::from_u128(13));
2290+
assert_eq!(d.to_uint_ceil(), Uint256::new(13));
22912291

22922292
let d = Decimal256::from_str("75.0").unwrap();
2293-
assert_eq!(d.to_uint_ceil(), Uint256::from_u128(75));
2293+
assert_eq!(d.to_uint_ceil(), Uint256::new(75));
22942294
let d = Decimal256::from_str("0.0").unwrap();
2295-
assert_eq!(d.to_uint_ceil(), Uint256::from_u128(0));
2295+
assert_eq!(d.to_uint_ceil(), Uint256::new(0));
22962296

22972297
let d = Decimal256::MAX;
22982298
assert_eq!(

packages/std/src/math/int256.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,9 @@ impl Int256 {
8383

8484
/// A conversion from `i128` that, unlike the one provided by the `From` trait,
8585
/// can be used in a `const` context.
86-
pub const fn from_i128(v: i128) -> Self {
87-
Self::from_be_bytes(grow_be_int(v.to_be_bytes()))
86+
#[deprecated(since = "3.0.0", note = "Use Int256::new(value) instead")]
87+
pub const fn from_i128(value: i128) -> Self {
88+
Self::new(value)
8889
}
8990

9091
#[must_use]
@@ -731,6 +732,7 @@ mod tests {
731732
}
732733

733734
#[test]
735+
#[allow(deprecated)]
734736
fn int256_from_i128() {
735737
assert_eq!(Int256::from_i128(123i128), Int256::from_str("123").unwrap());
736738

@@ -1034,7 +1036,7 @@ mod tests {
10341036

10351037
#[test]
10361038
fn int256_checked_multiply_ratio_works() {
1037-
let base = Int256::from_i128(500);
1039+
let base = Int256::new(500);
10381040

10391041
// factor 1/1
10401042
assert_eq!(base.checked_multiply_ratio(1i128, 1i128).unwrap(), base);
@@ -1051,38 +1053,38 @@ mod tests {
10511053
// factor 3/2
10521054
assert_eq!(
10531055
base.checked_multiply_ratio(3i128, 2i128).unwrap(),
1054-
Int256::from_i128(750)
1056+
Int256::new(750)
10551057
);
10561058
assert_eq!(
10571059
base.checked_multiply_ratio(333333i128, 222222i128).unwrap(),
1058-
Int256::from_i128(750)
1060+
Int256::new(750)
10591061
);
10601062

10611063
// factor 2/3 (integer division always floors the result)
10621064
assert_eq!(
10631065
base.checked_multiply_ratio(2i128, 3i128).unwrap(),
1064-
Int256::from_i128(333)
1066+
Int256::new(333)
10651067
);
10661068
assert_eq!(
10671069
base.checked_multiply_ratio(222222i128, 333333i128).unwrap(),
1068-
Int256::from_i128(333)
1070+
Int256::new(333)
10691071
);
10701072

10711073
// factor 5/6 (integer division always floors the result)
10721074
assert_eq!(
10731075
base.checked_multiply_ratio(5i128, 6i128).unwrap(),
1074-
Int256::from_i128(416)
1076+
Int256::new(416)
10751077
);
10761078
assert_eq!(
10771079
base.checked_multiply_ratio(100i128, 120i128).unwrap(),
1078-
Int256::from_i128(416)
1080+
Int256::new(416)
10791081
);
10801082
}
10811083

10821084
#[test]
10831085
fn int256_checked_multiply_ratio_does_not_panic() {
10841086
assert_eq!(
1085-
Int256::from_i128(500i128).checked_multiply_ratio(1i128, 0i128),
1087+
Int256::new(500i128).checked_multiply_ratio(1i128, 0i128),
10861088
Err(CheckedMultiplyRatioError::DivideByZero),
10871089
);
10881090
assert_eq!(

packages/std/src/math/signed_decimal_256.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ pub struct SignedDecimal256RangeExceeded;
3737

3838
impl SignedDecimal256 {
3939
const DECIMAL_FRACTIONAL: Int256 = // 1*10**18
40-
Int256::from_i128(1_000_000_000_000_000_000);
40+
Int256::new(1_000_000_000_000_000_000);
4141
const DECIMAL_FRACTIONAL_SQUARED: Int256 = // 1*10**36
42-
Int256::from_i128(1_000_000_000_000_000_000_000_000_000_000_000_000);
42+
Int256::new(1_000_000_000_000_000_000_000_000_000_000_000_000);
4343

4444
/// The number of decimal places. Since decimal types are fixed-point rather than
4545
/// floating-point, this is a constant.
@@ -104,7 +104,7 @@ impl SignedDecimal256 {
104104
note = "Use SignedDecimal256::new(Int256::new(value)) instead"
105105
)]
106106
pub const fn raw(value: i128) -> Self {
107-
Self(Int256::from_i128(value))
107+
Self(Int256::new(value))
108108
}
109109

110110
/// Create a 1.0 SignedDecimal256
@@ -117,7 +117,7 @@ impl SignedDecimal256 {
117117
#[inline]
118118
pub const fn negative_one() -> Self {
119119
// -DECIMAL_FRACTIONAL
120-
Self(Int256::from_i128(-1_000_000_000_000_000_000))
120+
Self(Int256::new(-1_000_000_000_000_000_000))
121121
}
122122

123123
/// Create a 0.0 SignedDecimal256
@@ -926,7 +926,7 @@ mod tests {
926926

927927
#[test]
928928
fn try_from_integer() {
929-
let int = Int256::from_i128(0xDEADBEEF);
929+
let int = Int256::new(0xDEADBEEF);
930930
let decimal = SignedDecimal256::try_from(int).unwrap();
931931
assert_eq!(int.to_string(), decimal.to_string());
932932
}
@@ -1481,15 +1481,15 @@ mod tests {
14811481
);
14821482
assert_eq!(
14831483
SignedDecimal256::from(SignedDecimal::MAX),
1484-
SignedDecimal256::new(Int256::from_i128(i128::MAX))
1484+
SignedDecimal256::new(Int256::new(i128::MAX))
14851485
);
14861486
assert_eq!(
14871487
SignedDecimal256::from(SignedDecimal::percent(-50)),
14881488
SignedDecimal256::percent(-50)
14891489
);
14901490
assert_eq!(
14911491
SignedDecimal256::from(SignedDecimal::MIN),
1492-
SignedDecimal256::new(Int256::from_i128(i128::MIN))
1492+
SignedDecimal256::new(Int256::new(i128::MIN))
14931493
);
14941494
}
14951495

packages/std/src/math/uint256.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,21 +126,16 @@ impl Uint256 {
126126
/// A conversion from `u128` that, unlike the one provided by the `From` trait,
127127
/// can be used in a `const` context.
128128
#[must_use]
129-
pub const fn from_u128(num: u128) -> Self {
130-
let bytes = num.to_le_bytes();
131-
132-
Self::from_le_bytes([
133-
bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7],
134-
bytes[8], bytes[9], bytes[10], bytes[11], bytes[12], bytes[13], bytes[14], bytes[15],
135-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
136-
])
129+
#[deprecated(since = "3.0.0", note = "Use Uint256::new(value) instead")]
130+
pub const fn from_u128(value: u128) -> Self {
131+
Self::new(value)
137132
}
138133

139134
/// A conversion from `Uint128` that, unlike the one provided by the `From` trait,
140135
/// can be used in a `const` context.
141136
#[must_use]
142137
pub const fn from_uint128(num: Uint128) -> Self {
143-
Self::from_u128(num.u128())
138+
Self::new(num.u128())
144139
}
145140

146141
/// Returns a copy of the number as big endian bytes.
@@ -1103,6 +1098,7 @@ mod tests {
11031098
}
11041099

11051100
#[test]
1101+
#[allow(deprecated)]
11061102
fn uint256_from_u128() {
11071103
assert_eq!(
11081104
Uint256::from_u128(123u128),

0 commit comments

Comments
 (0)