Skip to content

Commit

Permalink
Merge pull request #1069 from CosmWasm/uint-padding-fix
Browse files Browse the repository at this point in the history
Uint padding fix
  • Loading branch information
uint authored Sep 2, 2021
2 parents 7085568 + eb2284f commit ff232e0
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
6 changes: 6 additions & 0 deletions packages/std/src/math/uint128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,12 @@ mod tests {
assert_eq!(a.to_string(), "0");
}

#[test]
fn uint128_display_padding_works() {
let a = Uint128::from(123u64);
assert_eq!(format!("Embedded: {:05}", a), "Embedded: 00123");
}

#[test]
fn uint128_is_zero_works() {
assert!(Uint128::zero().is_zero());
Expand Down
12 changes: 11 additions & 1 deletion packages/std/src/math/uint256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,11 @@ impl From<Uint256> for String {

impl fmt::Display for Uint256 {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
// The inner type doesn't work as expected with padding, so we
// work around that.
let unpadded = self.0.to_string();

f.pad_integral(true, "", &unpadded)
}
}

Expand Down Expand Up @@ -552,6 +556,12 @@ mod tests {
assert_eq!(a.to_string(), "0");
}

#[test]
fn uint256_display_padding_works() {
let a = Uint256::from(123u64);
assert_eq!(format!("Embedded: {:05}", a), "Embedded: 00123");
}

#[test]
fn uint256_is_zero_works() {
assert!(Uint256::zero().is_zero());
Expand Down
12 changes: 11 additions & 1 deletion packages/std/src/math/uint512.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,11 @@ impl From<Uint512> for String {

impl fmt::Display for Uint512 {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
// The inner type doesn't work as expected with padding, so we
// work around that.
let unpadded = self.0.to_string();

f.pad_integral(true, "", &unpadded)
}
}

Expand Down Expand Up @@ -550,6 +554,12 @@ mod tests {
assert_eq!(a.to_string(), "0");
}

#[test]
fn uint512_display_padding_works() {
let a = Uint512::from(123u64);
assert_eq!(format!("Embedded: {:05}", a), "Embedded: 00123");
}

#[test]
fn uint512_is_zero_works() {
assert!(Uint512::zero().is_zero());
Expand Down
6 changes: 6 additions & 0 deletions packages/std/src/math/uint64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,12 @@ mod tests {
assert_eq!(a.to_string(), "0");
}

#[test]
fn uint64_display_padding_works() {
let a = Uint64::from(123u64);
assert_eq!(format!("Embedded: {:05}", a), "Embedded: 00123");
}

#[test]
fn uint64_is_zero_works() {
assert!(Uint64::zero().is_zero());
Expand Down

0 comments on commit ff232e0

Please sign in to comment.