Skip to content

Commit

Permalink
Implement From<Uint{64,128}> for u{64,128} and From<Int{64,128}> for …
Browse files Browse the repository at this point in the history
…i{64,128}
  • Loading branch information
webmaster128 committed Jan 22, 2025
1 parent 4c4af13 commit 3b8071b
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 16 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ and this project adheres to

## [Unreleased]

## Added

- cosmwasm-std: Implement `From<Uint64> for u{64,128}`,
`From<Uint128> for u128`, `From<Int64> for i{64,128}`, and
`From<Int128> for i128` ([#2268])

[#2268]: https://github.com/CosmWasm/cosmwasm/issues/2268

## Fixed

- cosmwasm-schema: The schema export now doesn't overwrite existing
Expand Down
14 changes: 14 additions & 0 deletions packages/std/src/math/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,20 @@ macro_rules! forward_try_from {
}
pub(crate) use forward_try_from;

/// Helper macro to implement `From` for a type that is just a wrapper around another type.
/// This can be used for all our integer conversions where `bnum` implements `From`.
macro_rules! wrapped_int_to_primitive {
($input: ty, $output: ty) => {
impl From<$input> for $output {
fn from(value: $input) -> Self {
// By convention all our Uint*/Int* types store the value in .0
value.0.into()
}
}
};
}
pub(crate) use wrapped_int_to_primitive;

/// Helper macro to implement `TryFrom` for a conversion from a bigger signed int to a smaller one.
/// This is needed because `bnum` does not implement `TryFrom` for those conversions
/// because of limitations of const generics.
Expand Down
11 changes: 10 additions & 1 deletion packages/std/src/math/int128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
__internal::forward_ref_partial_eq,
};

use super::conversion::{forward_try_from, try_from_int_to_int};
use super::conversion::{forward_try_from, try_from_int_to_int, wrapped_int_to_primitive};
use super::impl_int_serde;
use super::num_consts::NumConsts;

Expand Down Expand Up @@ -371,6 +371,9 @@ impl From<i8> for Int128 {
}
}

// Int to int
wrapped_int_to_primitive!(Int128, i128);

impl TryFrom<&str> for Int128 {
type Error = StdError;

Expand Down Expand Up @@ -626,6 +629,12 @@ mod tests {
assert_eq!(num1, num2);
}

#[test]
fn int128_convert_to() {
let a = Int128::new(5);
assert_eq!(i128::from(a), 5);
}

#[test]
fn int128_convert_from() {
let a = Int128::from(5i128);
Expand Down
15 changes: 14 additions & 1 deletion packages/std/src/math/int64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
__internal::forward_ref_partial_eq,
};

use super::conversion::{forward_try_from, try_from_int_to_int};
use super::conversion::{forward_try_from, try_from_int_to_int, wrapped_int_to_primitive};
use super::impl_int_serde;
use super::num_consts::NumConsts;

Expand Down Expand Up @@ -339,6 +339,10 @@ impl From<i8> for Int64 {
}
}

// Int to int
wrapped_int_to_primitive!(Int64, i64);
wrapped_int_to_primitive!(Int64, i128);

// Int to Int
try_from_int_to_int!(Int128, Int64);
try_from_int_to_int!(Int256, Int64);
Expand Down Expand Up @@ -599,6 +603,15 @@ mod tests {
assert_eq!(num1, num2);
}

#[test]
fn int64_convert_to() {
let a = Int64::new(5);
assert_eq!(i64::from(a), 5);

let a = Int64::new(5);
assert_eq!(i128::from(a), 5);
}

#[test]
fn int64_convert_from() {
let a = Int64::from(5i64);
Expand Down
12 changes: 5 additions & 7 deletions packages/std/src/math/uint128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::{
Uint256, Uint64,
};

use super::conversion::forward_try_from;
use super::conversion::{forward_try_from, wrapped_int_to_primitive};
use super::impl_int_serde;
use super::num_consts::NumConsts;

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

// uint to Uint
impl From<Uint64> for Uint128 {
fn from(val: Uint64) -> Self {
val.u64().into()
Expand Down Expand Up @@ -357,6 +358,9 @@ impl From<u8> for Uint128 {
}
}

// Uint to uint
wrapped_int_to_primitive!(Uint128, u128);

forward_try_from!(Uint128, Uint64);

// Int to Uint
Expand Down Expand Up @@ -390,12 +394,6 @@ impl From<Uint128> for String {
}
}

impl From<Uint128> for u128 {
fn from(original: Uint128) -> Self {
original.0
}
}

impl fmt::Display for Uint128 {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
Expand Down
16 changes: 9 additions & 7 deletions packages/std/src/math/uint64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
Uint128,
};

use super::conversion::forward_try_from;
use super::conversion::{forward_try_from, wrapped_int_to_primitive};
use super::impl_int_serde;
use super::num_consts::NumConsts;

Expand Down Expand Up @@ -342,6 +342,10 @@ impl From<u8> for Uint64 {
}
}

// Uint to uint
wrapped_int_to_primitive!(Uint64, u64);
wrapped_int_to_primitive!(Uint64, u128);

// Int to Uint
forward_try_from!(Int64, Uint64);
forward_try_from!(Int128, Uint64);
Expand All @@ -365,12 +369,6 @@ impl From<Uint64> for String {
}
}

impl From<Uint64> for u64 {
fn from(original: Uint64) -> Self {
original.0
}
}

impl fmt::Display for Uint64 {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
Expand Down Expand Up @@ -599,6 +597,10 @@ mod tests {
let a = u64::from(original);
assert_eq!(a, 12345);

let original = Uint64(12345);
let a = u128::from(original);
assert_eq!(a, 12345);

let original = Uint64(12345);
let a = String::from(original);
assert_eq!(a, "12345");
Expand Down

0 comments on commit 3b8071b

Please sign in to comment.