Skip to content

Commit 3d8bfa0

Browse files
committed
Implement AsRef<[u8]> for Binary and HexBinary
1 parent da76819 commit 3d8bfa0

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ and this project adheres to
1919
[#1533]).
2020
- cosmwasm-std: Upgrade `serde-json-wasm` dependency to 0.5.0 which adds map
2121
support to `to_vec`/`to_binary` and friends.
22+
- cosmwasm-std: Implement `AsRef<[u8]>` for `Binary` and `HexBinary` ([#1550]).
2223

2324
[#1437]: https://github.com/CosmWasm/cosmwasm/issues/1437
2425
[#1481]: https://github.com/CosmWasm/cosmwasm/pull/1481
2526
[#1478]: https://github.com/CosmWasm/cosmwasm/pull/1478
2627
[#1533]: https://github.com/CosmWasm/cosmwasm/pull/1533
28+
[#1550]: https://github.com/CosmWasm/cosmwasm/issues/1550
2729

2830
### Changed
2931

packages/std/src/binary.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ impl Deref for Binary {
9696
}
9797
}
9898

99+
impl AsRef<[u8]> for Binary {
100+
fn as_ref(&self) -> &[u8] {
101+
self.as_slice()
102+
}
103+
}
104+
99105
// Slice
100106
impl From<&[u8]> for Binary {
101107
fn from(binary: &[u8]) -> Self {
@@ -497,6 +503,34 @@ mod tests {
497503
assert_eq!(binary_slice, &[7u8, 35, 49, 101, 0, 255]);
498504
}
499505

506+
#[test]
507+
fn binary_implements_as_ref() {
508+
// Can use as_ref (this we already get via the Deref implementation)
509+
let data = Binary(vec![7u8, 35, 49, 101, 0, 255]);
510+
assert_eq!(data.as_ref(), &[7u8, 35, 49, 101, 0, 255]);
511+
512+
let data = Binary(vec![7u8, 35, 49, 101, 0, 255]);
513+
let data_ref = &data;
514+
assert_eq!(data_ref.as_ref(), &[7u8, 35, 49, 101, 0, 255]);
515+
516+
// Implements as ref
517+
518+
// This is a dummy function to mimic the signature of
519+
// https://docs.rs/sha2/0.10.6/sha2/trait.Digest.html#tymethod.digest
520+
fn hash(data: impl AsRef<[u8]>) -> u64 {
521+
let mut hasher = DefaultHasher::new();
522+
data.as_ref().hash(&mut hasher);
523+
hasher.finish()
524+
}
525+
526+
let data = Binary(vec![7u8, 35, 49, 101, 0, 255]);
527+
hash(data);
528+
529+
let data = Binary(vec![7u8, 35, 49, 101, 0, 255]);
530+
let data_ref = &data;
531+
hash(data_ref);
532+
}
533+
500534
#[test]
501535
fn binary_implements_hash() {
502536
let a1 = Binary::from([0, 187, 61, 11, 250, 0]);

packages/std/src/hex_binary.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ impl Deref for HexBinary {
9292
}
9393
}
9494

95+
impl AsRef<[u8]> for HexBinary {
96+
fn as_ref(&self) -> &[u8] {
97+
self.as_slice()
98+
}
99+
}
100+
95101
// Slice
96102
impl From<&[u8]> for HexBinary {
97103
fn from(binary: &[u8]) -> Self {
@@ -561,6 +567,34 @@ mod tests {
561567
assert_eq!(data_slice, &[7u8, 35, 49, 101, 0, 255]);
562568
}
563569

570+
#[test]
571+
fn hex_binary_implements_as_ref() {
572+
// Can use as_ref (this we already get via the Deref implementation)
573+
let data = HexBinary(vec![7u8, 35, 49, 101, 0, 255]);
574+
assert_eq!(data.as_ref(), &[7u8, 35, 49, 101, 0, 255]);
575+
576+
let data = HexBinary(vec![7u8, 35, 49, 101, 0, 255]);
577+
let data_ref = &data;
578+
assert_eq!(data_ref.as_ref(), &[7u8, 35, 49, 101, 0, 255]);
579+
580+
// Implements as ref
581+
582+
// This is a dummy function to mimic the signature of
583+
// https://docs.rs/sha2/0.10.6/sha2/trait.Digest.html#tymethod.digest
584+
fn hash(data: impl AsRef<[u8]>) -> u64 {
585+
let mut hasher = DefaultHasher::new();
586+
data.as_ref().hash(&mut hasher);
587+
hasher.finish()
588+
}
589+
590+
let data = HexBinary(vec![7u8, 35, 49, 101, 0, 255]);
591+
hash(data);
592+
593+
let data = HexBinary(vec![7u8, 35, 49, 101, 0, 255]);
594+
let data_ref = &data;
595+
hash(data_ref);
596+
}
597+
564598
#[test]
565599
fn hex_binary_implements_hash() {
566600
let a1 = HexBinary::from([0, 187, 61, 11, 250, 0]);

0 commit comments

Comments
 (0)