Skip to content

Commit 3b8071b

Browse files
committed
Implement From<Uint{64,128}> for u{64,128} and From<Int{64,128}> for i{64,128}
1 parent 4c4af13 commit 3b8071b

File tree

6 files changed

+60
-16
lines changed

6 files changed

+60
-16
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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,20 @@ 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+
8599
/// Helper macro to implement `TryFrom` for a conversion from a bigger signed int to a smaller one.
86100
/// This is needed because `bnum` does not implement `TryFrom` for those conversions
87101
/// because of limitations of const generics.

packages/std/src/math/int128.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ 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::{forward_try_from, try_from_int_to_int, wrapped_int_to_primitive};
1717
use super::impl_int_serde;
1818
use super::num_consts::NumConsts;
1919

@@ -371,6 +371,9 @@ impl From<i8> for Int128 {
371371
}
372372
}
373373

374+
// Int to int
375+
wrapped_int_to_primitive!(Int128, i128);
376+
374377
impl TryFrom<&str> for Int128 {
375378
type Error = StdError;
376379

@@ -626,6 +629,12 @@ mod tests {
626629
assert_eq!(num1, num2);
627630
}
628631

632+
#[test]
633+
fn int128_convert_to() {
634+
let a = Int128::new(5);
635+
assert_eq!(i128::from(a), 5);
636+
}
637+
629638
#[test]
630639
fn int128_convert_from() {
631640
let a = Int128::from(5i128);

packages/std/src/math/int64.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ 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::{forward_try_from, try_from_int_to_int, wrapped_int_to_primitive};
1717
use super::impl_int_serde;
1818
use super::num_consts::NumConsts;
1919

@@ -339,6 +339,10 @@ impl From<i8> for Int64 {
339339
}
340340
}
341341

342+
// Int to int
343+
wrapped_int_to_primitive!(Int64, i64);
344+
wrapped_int_to_primitive!(Int64, i128);
345+
342346
// Int to Int
343347
try_from_int_to_int!(Int128, Int64);
344348
try_from_int_to_int!(Int256, Int64);
@@ -599,6 +603,15 @@ mod tests {
599603
assert_eq!(num1, num2);
600604
}
601605

606+
#[test]
607+
fn int64_convert_to() {
608+
let a = Int64::new(5);
609+
assert_eq!(i64::from(a), 5);
610+
611+
let a = Int64::new(5);
612+
assert_eq!(i128::from(a), 5);
613+
}
614+
602615
#[test]
603616
fn int64_convert_from() {
604617
let a = Int64::from(5i64);

packages/std/src/math/uint128.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::{
1616
Uint256, Uint64,
1717
};
1818

19-
use super::conversion::forward_try_from;
19+
use super::conversion::{forward_try_from, wrapped_int_to_primitive};
2020
use super::impl_int_serde;
2121
use super::num_consts::NumConsts;
2222

@@ -321,6 +321,7 @@ impl_mul_fraction!(Uint128);
321321
// of the conflict with `TryFrom<&str>` as described here
322322
// https://stackoverflow.com/questions/63136970/how-do-i-work-around-the-upstream-crates-may-add-a-new-impl-of-trait-error
323323

324+
// uint to Uint
324325
impl From<Uint64> for Uint128 {
325326
fn from(val: Uint64) -> Self {
326327
val.u64().into()
@@ -357,6 +358,9 @@ impl From<u8> for Uint128 {
357358
}
358359
}
359360

361+
// Uint to uint
362+
wrapped_int_to_primitive!(Uint128, u128);
363+
360364
forward_try_from!(Uint128, Uint64);
361365

362366
// Int to Uint
@@ -390,12 +394,6 @@ impl From<Uint128> for String {
390394
}
391395
}
392396

393-
impl From<Uint128> for u128 {
394-
fn from(original: Uint128) -> Self {
395-
original.0
396-
}
397-
}
398-
399397
impl fmt::Display for Uint128 {
400398
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
401399
self.0.fmt(f)

packages/std/src/math/uint64.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::{
1515
Uint128,
1616
};
1717

18-
use super::conversion::forward_try_from;
18+
use super::conversion::{forward_try_from, wrapped_int_to_primitive};
1919
use super::impl_int_serde;
2020
use super::num_consts::NumConsts;
2121

@@ -342,6 +342,10 @@ impl From<u8> for Uint64 {
342342
}
343343
}
344344

345+
// Uint to uint
346+
wrapped_int_to_primitive!(Uint64, u64);
347+
wrapped_int_to_primitive!(Uint64, u128);
348+
345349
// Int to Uint
346350
forward_try_from!(Int64, Uint64);
347351
forward_try_from!(Int128, Uint64);
@@ -365,12 +369,6 @@ impl From<Uint64> for String {
365369
}
366370
}
367371

368-
impl From<Uint64> for u64 {
369-
fn from(original: Uint64) -> Self {
370-
original.0
371-
}
372-
}
373-
374372
impl fmt::Display for Uint64 {
375373
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
376374
self.0.fmt(f)
@@ -599,6 +597,10 @@ mod tests {
599597
let a = u64::from(original);
600598
assert_eq!(a, 12345);
601599

600+
let original = Uint64(12345);
601+
let a = u128::from(original);
602+
assert_eq!(a, 12345);
603+
602604
let original = Uint64(12345);
603605
let a = String::from(original);
604606
assert_eq!(a, "12345");

0 commit comments

Comments
 (0)