Skip to content

Commit

Permalink
Merge pull request #2075 from CosmWasm/aw/tryfrom-decimals
Browse files Browse the repository at this point in the history
Implement TryFrom for the decimals' respective integers
  • Loading branch information
aumetra authored Mar 27, 2024
2 parents d992018 + 1eedc8d commit 092d222
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ and this project adheres to
ECDSA signature verification over secp256r1. ([#1983], [#2057], [#2058])
- cosmwasm-vm: Add metrics for the pinned memory cache ([#2059])
- cosmwasm-derive: The crate used in the expansion can now be renamed ([#2068])
- cosmwasm-std: The decimal types now implement `TryFrom` for their respective
integer representations ([#2075])

[#1983]: https://github.com/CosmWasm/cosmwasm/pull/1983
[#2057]: https://github.com/CosmWasm/cosmwasm/pull/2057
[#2058]: https://github.com/CosmWasm/cosmwasm/pull/2058
[#2068]: https://github.com/CosmWasm/cosmwasm/pull/2068
[#2075]: https://github.com/CosmWasm/cosmwasm/pull/2075

### Changed

Expand Down
16 changes: 16 additions & 0 deletions packages/std/src/math/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,15 @@ impl TryFrom<SignedDecimal256> for Decimal {
}
}

impl TryFrom<Uint128> for Decimal {
type Error = DecimalRangeExceeded;

#[inline]
fn try_from(value: Uint128) -> Result<Self, Self::Error> {
Self::from_atomics(value, 0)
}
}

impl FromStr for Decimal {
type Err = StdError;

Expand Down Expand Up @@ -838,6 +847,13 @@ mod tests {
);
}

#[test]
fn decimal_try_from_integer() {
let int = Uint128::new(0xDEADBEEF);
let decimal = Decimal::try_from(int).unwrap();
assert_eq!(int.to_string(), decimal.to_string());
}

#[test]
fn decimal_try_from_signed_works() {
assert_eq!(
Expand Down
16 changes: 16 additions & 0 deletions packages/std/src/math/decimal256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,15 @@ impl TryFrom<SignedDecimal256> for Decimal256 {
}
}

impl TryFrom<Uint256> for Decimal256 {
type Error = Decimal256RangeExceeded;

#[inline]
fn try_from(value: Uint256) -> Result<Self, Self::Error> {
Self::from_atomics(value, 0)
}
}

impl FromStr for Decimal256 {
type Err = StdError;

Expand Down Expand Up @@ -789,6 +798,13 @@ mod tests {
Decimal256::from_str(input).unwrap()
}

#[test]
fn decimal256_try_from_integer() {
let int = Uint256::from_u128(0xDEADBEEF);
let decimal = Decimal256::try_from(int).unwrap();
assert_eq!(int.to_string(), decimal.to_string());
}

#[test]
fn decimal256_new() {
let expected = Uint256::from(300u128);
Expand Down
16 changes: 16 additions & 0 deletions packages/std/src/math/signed_decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,15 @@ impl TryFrom<Decimal256> for SignedDecimal {
}
}

impl TryFrom<Int128> for SignedDecimal {
type Error = SignedDecimalRangeExceeded;

#[inline]
fn try_from(value: Int128) -> Result<Self, Self::Error> {
Self::from_atomics(value, 0)
}
}

impl FromStr for SignedDecimal {
type Err = StdError;

Expand Down Expand Up @@ -981,6 +990,13 @@ mod tests {
);
}

#[test]
fn try_from_integer() {
let int = Int128::new(0xDEADBEEF);
let decimal = SignedDecimal::try_from(int).unwrap();
assert_eq!(int.to_string(), decimal.to_string());
}

#[test]
fn signed_decimal_from_atomics_works() {
let one = SignedDecimal::one();
Expand Down
16 changes: 16 additions & 0 deletions packages/std/src/math/signed_decimal_256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,15 @@ impl TryFrom<Decimal256> for SignedDecimal256 {
}
}

impl TryFrom<Int256> for SignedDecimal256 {
type Error = SignedDecimal256RangeExceeded;

#[inline]
fn try_from(value: Int256) -> Result<Self, Self::Error> {
Self::from_atomics(value, 0)
}
}

impl FromStr for SignedDecimal256 {
type Err = StdError;

Expand Down Expand Up @@ -907,6 +916,13 @@ mod tests {
SignedDecimal256::from_str(input).unwrap()
}

#[test]
fn try_from_integer() {
let int = Int256::from_i128(0xDEADBEEF);
let decimal = SignedDecimal256::try_from(int).unwrap();
assert_eq!(int.to_string(), decimal.to_string());
}

#[test]
fn signed_decimal_256_new() {
let expected = Int256::from(300i128);
Expand Down

0 comments on commit 092d222

Please sign in to comment.