Skip to content

Commit 23de2fc

Browse files
committed
Implement from_be_bytes/from_le_bytes for Uint64 and Uint128
1 parent 73dd089 commit 23de2fc

File tree

3 files changed

+131
-24
lines changed

3 files changed

+131
-24
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ and this project adheres to
1515
`From<Int128> for i128` ([#2268])
1616
- cosmwasm-std: Deprecate `abort` feature. The panic handler is now always
1717
enabled. ([#2337])
18+
- cosmwasm-std: Implement `Uint128::from_{be,le}_bytes` and
19+
`Uint64::from_{be,le}_bytes`. ([#2269])
1820

1921
[#2268]: https://github.com/CosmWasm/cosmwasm/issues/2268
2022
[#2337]: https://github.com/CosmWasm/cosmwasm/issues/2337
23+
[#2269]: https://github.com/CosmWasm/cosmwasm/issues/2269
2124

2225
## Fixed
2326

packages/std/src/math/uint128.rs

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

19-
use super::conversion::{forward_try_from, primitive_to_wrapped_int, wrapped_int_to_primitive};
19+
use super::conversion::{
20+
forward_try_from, from_and_to_bytes, primitive_to_wrapped_int, wrapped_int_to_primitive,
21+
};
2022
use super::impl_int_serde;
2123
use super::num_consts::NumConsts;
2224

@@ -73,17 +75,7 @@ impl Uint128 {
7375
self.0
7476
}
7577

76-
/// Returns a copy of the number as big endian bytes.
77-
#[must_use = "this returns the result of the operation, without modifying the original"]
78-
pub const fn to_be_bytes(self) -> [u8; 16] {
79-
self.0.to_be_bytes()
80-
}
81-
82-
/// Returns a copy of the number as little endian bytes.
83-
#[must_use = "this returns the result of the operation, without modifying the original"]
84-
pub const fn to_le_bytes(self) -> [u8; 16] {
85-
self.0.to_le_bytes()
86-
}
78+
from_and_to_bytes!(u128, 16);
8779

8880
#[must_use]
8981
pub const fn is_zero(&self) -> bool {
@@ -605,6 +597,68 @@ mod tests {
605597
);
606598
}
607599

600+
#[test]
601+
fn uint128_from_be_bytes_works() {
602+
// zero
603+
let original = [0; 16];
604+
let num = Uint128::from_be_bytes(original);
605+
assert!(num.is_zero());
606+
607+
// one
608+
let original = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1];
609+
let num = Uint128::from_be_bytes(original);
610+
assert_eq!(num.u128(), 1);
611+
612+
// 258
613+
let original = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2];
614+
let num = Uint128::from_be_bytes(original);
615+
assert_eq!(num.u128(), 258);
616+
617+
// 2x roundtrip
618+
let original = [1; 16];
619+
let num = Uint128::from_be_bytes(original);
620+
let a: [u8; 16] = num.to_be_bytes();
621+
assert_eq!(a, original);
622+
623+
let original = [
624+
0u8, 222u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 1u8, 2u8, 3u8,
625+
];
626+
let num = Uint128::from_be_bytes(original);
627+
let resulting_bytes: [u8; 16] = num.to_be_bytes();
628+
assert_eq!(resulting_bytes, original);
629+
}
630+
631+
#[test]
632+
fn uint128_from_le_bytes_works() {
633+
// zero
634+
let original = [0; 16];
635+
let num = Uint128::from_le_bytes(original);
636+
assert!(num.is_zero());
637+
638+
// one
639+
let original = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
640+
let num = Uint128::from_le_bytes(original);
641+
assert_eq!(num.u128(), 1);
642+
643+
// 258
644+
let original = [2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
645+
let num = Uint128::from_le_bytes(original);
646+
assert_eq!(num.u128(), 258);
647+
648+
// 2x roundtrip
649+
let original = [1; 16];
650+
let num = Uint128::from_le_bytes(original);
651+
let a: [u8; 16] = num.to_le_bytes();
652+
assert_eq!(a, original);
653+
654+
let original = [
655+
0u8, 222u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 1u8, 2u8, 3u8,
656+
];
657+
let num = Uint128::from_le_bytes(original);
658+
let resulting_bytes: [u8; 16] = num.to_le_bytes();
659+
assert_eq!(resulting_bytes, original);
660+
}
661+
608662
#[test]
609663
fn uint128_convert_into() {
610664
let original = Uint128(12345);

packages/std/src/math/uint64.rs

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

18-
use super::conversion::{forward_try_from, primitive_to_wrapped_int, wrapped_int_to_primitive};
18+
use super::conversion::{
19+
forward_try_from, from_and_to_bytes, primitive_to_wrapped_int, wrapped_int_to_primitive,
20+
};
1921
use super::impl_int_serde;
2022
use super::num_consts::NumConsts;
2123

@@ -69,17 +71,7 @@ impl Uint64 {
6971
self.0
7072
}
7173

72-
/// Returns a copy of the number as big endian bytes.
73-
#[must_use = "this returns the result of the operation, without modifying the original"]
74-
pub const fn to_be_bytes(self) -> [u8; 8] {
75-
self.0.to_be_bytes()
76-
}
77-
78-
/// Returns a copy of the number as little endian bytes.
79-
#[must_use = "this returns the result of the operation, without modifying the original"]
80-
pub const fn to_le_bytes(self) -> [u8; 8] {
81-
self.0.to_le_bytes()
82-
}
74+
from_and_to_bytes!(u64, 8);
8375

8476
#[must_use]
8577
pub const fn is_zero(&self) -> bool {
@@ -572,6 +564,64 @@ mod tests {
572564
assert_eq!(one.to_be_bytes(), [0, 0, 0, 0, 0, 0, 0, 1]);
573565
}
574566

567+
#[test]
568+
fn uint64_from_be_bytes_works() {
569+
// zero
570+
let original = [0; 8];
571+
let num = Uint64::from_be_bytes(original);
572+
assert!(num.is_zero());
573+
574+
// one
575+
let original = [0, 0, 0, 0, 0, 0, 0, 1];
576+
let num = Uint64::from_be_bytes(original);
577+
assert_eq!(num.u64(), 1);
578+
579+
// 258
580+
let original = [0, 0, 0, 0, 0, 0, 1, 2];
581+
let num = Uint64::from_be_bytes(original);
582+
assert_eq!(num.u64(), 258);
583+
584+
// 2x roundtrip
585+
let original = [1; 8];
586+
let num = Uint64::from_be_bytes(original);
587+
let a: [u8; 8] = num.to_be_bytes();
588+
assert_eq!(a, original);
589+
590+
let original = [0u8, 222u8, 0u8, 0u8, 0u8, 1u8, 2u8, 3u8];
591+
let num = Uint64::from_be_bytes(original);
592+
let resulting_bytes: [u8; 8] = num.to_be_bytes();
593+
assert_eq!(resulting_bytes, original);
594+
}
595+
596+
#[test]
597+
fn uint64_from_le_bytes_works() {
598+
// zero
599+
let original = [0; 8];
600+
let num = Uint64::from_le_bytes(original);
601+
assert!(num.is_zero());
602+
603+
// one
604+
let original = [1, 0, 0, 0, 0, 0, 0, 0];
605+
let num = Uint64::from_le_bytes(original);
606+
assert_eq!(num.u64(), 1);
607+
608+
// 258
609+
let original = [2, 1, 0, 0, 0, 0, 0, 0];
610+
let num = Uint64::from_le_bytes(original);
611+
assert_eq!(num.u64(), 258);
612+
613+
// 2x roundtrip
614+
let original = [1; 8];
615+
let num = Uint64::from_le_bytes(original);
616+
let a: [u8; 8] = num.to_le_bytes();
617+
assert_eq!(a, original);
618+
619+
let original = [0u8, 222u8, 0u8, 0u8, 0u8, 1u8, 2u8, 3u8];
620+
let num = Uint64::from_le_bytes(original);
621+
let resulting_bytes: [u8; 8] = num.to_le_bytes();
622+
assert_eq!(resulting_bytes, original);
623+
}
624+
575625
#[test]
576626
fn uint64_convert_into() {
577627
let original = Uint64(12345);

0 commit comments

Comments
 (0)