Skip to content

Commit 589d242

Browse files
authored
Merge pull request #2348 from CosmWasm/uint-convert
Add missing conversions between primitive and wrapped integer types
2 parents a73affb + e2b5cc2 commit 589d242

File tree

10 files changed

+141
-338
lines changed

10 files changed

+141
-338
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ and this project adheres to
88

99
## [Unreleased]
1010

11+
## Added
12+
13+
- cosmwasm-std: Implement `From<Uint64> for u{64,128}`,
14+
`From<Uint128> for u128`, `From<Int64> for i{64,128}`, and
15+
`From<Int128> for i128` ([#2268])
16+
17+
[#2268]: https://github.com/CosmWasm/cosmwasm/issues/2268
18+
1119
## Fixed
1220

1321
- cosmwasm-schema: The schema export now doesn't overwrite existing

packages/std/src/math/conversion.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,34 @@ macro_rules! forward_try_from {
8282
}
8383
pub(crate) use forward_try_from;
8484

85+
/// Helper macro to implement `From` for a type that is just a wrapper around another type.
86+
/// This can be used for all our integer conversions where `bnum` implements `From`.
87+
macro_rules! wrapped_int_to_primitive {
88+
($input: ty, $output: ty) => {
89+
impl From<$input> for $output {
90+
fn from(value: $input) -> Self {
91+
// By convention all our Uint*/Int* types store the value in .0
92+
value.0.into()
93+
}
94+
}
95+
};
96+
}
97+
pub(crate) use wrapped_int_to_primitive;
98+
99+
/// Helper macro to implement `From` for a type that is just a wrapper around another type.
100+
/// This can be used for all our integer conversions where `bnum` implements `From`.
101+
macro_rules! primitive_to_wrapped_int {
102+
($input: ty, $output: ty) => {
103+
impl From<$input> for $output {
104+
fn from(value: $input) -> Self {
105+
// By convention all our Uint*/Int* types store the value in .0
106+
Self(value.into())
107+
}
108+
}
109+
};
110+
}
111+
pub(crate) use primitive_to_wrapped_int;
112+
85113
/// Helper macro to implement `TryFrom` for a conversion from a bigger signed int to a smaller one.
86114
/// This is needed because `bnum` does not implement `TryFrom` for those conversions
87115
/// because of limitations of const generics.

packages/std/src/math/int128.rs

Lines changed: 20 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ use crate::{
1313
__internal::forward_ref_partial_eq,
1414
};
1515

16-
use super::conversion::{forward_try_from, try_from_int_to_int};
16+
use super::conversion::{
17+
forward_try_from, primitive_to_wrapped_int, try_from_int_to_int, wrapped_int_to_primitive,
18+
};
1719
use super::impl_int_serde;
1820
use super::num_consts::NumConsts;
1921

@@ -306,29 +308,10 @@ forward_try_from!(Uint256, Int128);
306308
forward_try_from!(Uint512, Int128);
307309

308310
// uint to Int
309-
impl From<u64> for Int128 {
310-
fn from(val: u64) -> Self {
311-
Int128(val.into())
312-
}
313-
}
314-
315-
impl From<u32> for Int128 {
316-
fn from(val: u32) -> Self {
317-
Int128(val.into())
318-
}
319-
}
320-
321-
impl From<u16> for Int128 {
322-
fn from(val: u16) -> Self {
323-
Int128(val.into())
324-
}
325-
}
326-
327-
impl From<u8> for Int128 {
328-
fn from(val: u8) -> Self {
329-
Int128(val.into())
330-
}
331-
}
311+
primitive_to_wrapped_int!(u8, Int128);
312+
primitive_to_wrapped_int!(u16, Int128);
313+
primitive_to_wrapped_int!(u32, Int128);
314+
primitive_to_wrapped_int!(u64, Int128);
332315

333316
// Int to Int
334317
impl From<Int64> for Int128 {
@@ -341,35 +324,14 @@ try_from_int_to_int!(Int256, Int128);
341324
try_from_int_to_int!(Int512, Int128);
342325

343326
// int to Int
344-
impl From<i128> for Int128 {
345-
fn from(val: i128) -> Self {
346-
Int128(val)
347-
}
348-
}
327+
primitive_to_wrapped_int!(i8, Int128);
328+
primitive_to_wrapped_int!(i16, Int128);
329+
primitive_to_wrapped_int!(i32, Int128);
330+
primitive_to_wrapped_int!(i64, Int128);
331+
primitive_to_wrapped_int!(i128, Int128);
349332

350-
impl From<i64> for Int128 {
351-
fn from(val: i64) -> Self {
352-
Int128(val.into())
353-
}
354-
}
355-
356-
impl From<i32> for Int128 {
357-
fn from(val: i32) -> Self {
358-
Int128(val.into())
359-
}
360-
}
361-
362-
impl From<i16> for Int128 {
363-
fn from(val: i16) -> Self {
364-
Int128(val.into())
365-
}
366-
}
367-
368-
impl From<i8> for Int128 {
369-
fn from(val: i8) -> Self {
370-
Int128(val.into())
371-
}
372-
}
333+
// Int to int
334+
wrapped_int_to_primitive!(Int128, i128);
373335

374336
impl TryFrom<&str> for Int128 {
375337
type Error = StdError;
@@ -626,6 +588,12 @@ mod tests {
626588
assert_eq!(num1, num2);
627589
}
628590

591+
#[test]
592+
fn int128_convert_to() {
593+
let a = Int128::new(5);
594+
assert_eq!(i128::from(a), 5);
595+
}
596+
629597
#[test]
630598
fn int128_convert_from() {
631599
let a = Int128::from(5i128);

packages/std/src/math/int256.rs

Lines changed: 13 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ use crate::{
1717
/// the implementation in the future.
1818
use bnum::types::{I256, U256};
1919

20-
use super::conversion::{grow_be_int, try_from_int_to_int, try_from_uint_to_int};
20+
use super::conversion::{
21+
grow_be_int, primitive_to_wrapped_int, try_from_int_to_int, try_from_uint_to_int,
22+
};
2123
use super::impl_int_serde;
2224
use super::num_consts::NumConsts;
2325

@@ -370,35 +372,11 @@ impl From<Uint64> for Int256 {
370372
}
371373

372374
// uint to Int
373-
impl From<u128> for Int256 {
374-
fn from(val: u128) -> Self {
375-
Int256(val.into())
376-
}
377-
}
378-
379-
impl From<u64> for Int256 {
380-
fn from(val: u64) -> Self {
381-
Int256(val.into())
382-
}
383-
}
384-
385-
impl From<u32> for Int256 {
386-
fn from(val: u32) -> Self {
387-
Int256(val.into())
388-
}
389-
}
390-
391-
impl From<u16> for Int256 {
392-
fn from(val: u16) -> Self {
393-
Int256(val.into())
394-
}
395-
}
396-
397-
impl From<u8> for Int256 {
398-
fn from(val: u8) -> Self {
399-
Int256(val.into())
400-
}
401-
}
375+
primitive_to_wrapped_int!(u8, Int256);
376+
primitive_to_wrapped_int!(u16, Int256);
377+
primitive_to_wrapped_int!(u32, Int256);
378+
primitive_to_wrapped_int!(u64, Int256);
379+
primitive_to_wrapped_int!(u128, Int256);
402380

403381
// Int to Int
404382
try_from_int_to_int!(Int512, Int256);
@@ -416,35 +394,11 @@ impl From<Int64> for Int256 {
416394
}
417395

418396
// int to Int
419-
impl From<i128> for Int256 {
420-
fn from(val: i128) -> Self {
421-
Int256(val.into())
422-
}
423-
}
424-
425-
impl From<i64> for Int256 {
426-
fn from(val: i64) -> Self {
427-
Int256(val.into())
428-
}
429-
}
430-
431-
impl From<i32> for Int256 {
432-
fn from(val: i32) -> Self {
433-
Int256(val.into())
434-
}
435-
}
436-
437-
impl From<i16> for Int256 {
438-
fn from(val: i16) -> Self {
439-
Int256(val.into())
440-
}
441-
}
442-
443-
impl From<i8> for Int256 {
444-
fn from(val: i8) -> Self {
445-
Int256(val.into())
446-
}
447-
}
397+
primitive_to_wrapped_int!(i8, Int256);
398+
primitive_to_wrapped_int!(i16, Int256);
399+
primitive_to_wrapped_int!(i32, Int256);
400+
primitive_to_wrapped_int!(i64, Int256);
401+
primitive_to_wrapped_int!(i128, Int256);
448402

449403
impl TryFrom<&str> for Int256 {
450404
type Error = StdError;

packages/std/src/math/int512.rs

Lines changed: 11 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::{
1616
/// the implementation in the future.
1717
use bnum::types::{I512, U512};
1818

19-
use super::conversion::{grow_be_int, try_from_uint_to_int};
19+
use super::conversion::{grow_be_int, primitive_to_wrapped_int, try_from_uint_to_int};
2020
use super::impl_int_serde;
2121
use super::num_consts::NumConsts;
2222

@@ -365,66 +365,18 @@ impl From<Uint64> for Int512 {
365365
}
366366

367367
// uint to Int
368-
impl From<u128> for Int512 {
369-
fn from(val: u128) -> Self {
370-
Int512(val.into())
371-
}
372-
}
373-
374-
impl From<u64> for Int512 {
375-
fn from(val: u64) -> Self {
376-
Int512(val.into())
377-
}
378-
}
379-
380-
impl From<u32> for Int512 {
381-
fn from(val: u32) -> Self {
382-
Int512(val.into())
383-
}
384-
}
385-
386-
impl From<u16> for Int512 {
387-
fn from(val: u16) -> Self {
388-
Int512(val.into())
389-
}
390-
}
391-
392-
impl From<u8> for Int512 {
393-
fn from(val: u8) -> Self {
394-
Int512(val.into())
395-
}
396-
}
368+
primitive_to_wrapped_int!(u8, Int512);
369+
primitive_to_wrapped_int!(u16, Int512);
370+
primitive_to_wrapped_int!(u32, Int512);
371+
primitive_to_wrapped_int!(u64, Int512);
372+
primitive_to_wrapped_int!(u128, Int512);
397373

398374
// int to Int
399-
impl From<i128> for Int512 {
400-
fn from(val: i128) -> Self {
401-
Int512(val.into())
402-
}
403-
}
404-
405-
impl From<i64> for Int512 {
406-
fn from(val: i64) -> Self {
407-
Int512(val.into())
408-
}
409-
}
410-
411-
impl From<i32> for Int512 {
412-
fn from(val: i32) -> Self {
413-
Int512(val.into())
414-
}
415-
}
416-
417-
impl From<i16> for Int512 {
418-
fn from(val: i16) -> Self {
419-
Int512(val.into())
420-
}
421-
}
422-
423-
impl From<i8> for Int512 {
424-
fn from(val: i8) -> Self {
425-
Int512(val.into())
426-
}
427-
}
375+
primitive_to_wrapped_int!(i8, Int512);
376+
primitive_to_wrapped_int!(i16, Int512);
377+
primitive_to_wrapped_int!(i32, Int512);
378+
primitive_to_wrapped_int!(i64, Int512);
379+
primitive_to_wrapped_int!(i128, Int512);
428380

429381
// Int to Int
430382
impl From<Int64> for Int512 {

0 commit comments

Comments
 (0)