Skip to content

Commit 1558877

Browse files
authored
Merge pull request #1553 from CosmWasm/as_ref-implementation
Implement AsRef<[u8]> for Binary and HexBinary
2 parents 4525f29 + 3d8bfa0 commit 1558877

File tree

3 files changed

+88
-16
lines changed

3 files changed

+88
-16
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: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,6 @@ impl fmt::Debug for Binary {
8383
}
8484
}
8585

86-
impl From<&[u8]> for Binary {
87-
fn from(binary: &[u8]) -> Self {
88-
Self(binary.to_vec())
89-
}
90-
}
91-
9286
/// Just like Vec<u8>, Binary is a smart pointer to [u8].
9387
/// This implements `*binary` for us and allows us to
9488
/// do `&*binary`, returning a `&[u8]` from a `&Binary`.
@@ -102,14 +96,27 @@ impl Deref for Binary {
10296
}
10397
}
10498

105-
// Reference
99+
impl AsRef<[u8]> for Binary {
100+
fn as_ref(&self) -> &[u8] {
101+
self.as_slice()
102+
}
103+
}
104+
105+
// Slice
106+
impl From<&[u8]> for Binary {
107+
fn from(binary: &[u8]) -> Self {
108+
Self(binary.to_vec())
109+
}
110+
}
111+
112+
// Array reference
106113
impl<const LENGTH: usize> From<&[u8; LENGTH]> for Binary {
107114
fn from(source: &[u8; LENGTH]) -> Self {
108115
Self(source.to_vec())
109116
}
110117
}
111118

112-
// Owned
119+
// Owned array
113120
impl<const LENGTH: usize> From<[u8; LENGTH]> for Binary {
114121
fn from(source: [u8; LENGTH]) -> Self {
115122
Self(source.into())
@@ -496,6 +503,34 @@ mod tests {
496503
assert_eq!(binary_slice, &[7u8, 35, 49, 101, 0, 255]);
497504
}
498505

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+
499534
#[test]
500535
fn binary_implements_hash() {
501536
let a1 = Binary::from([0, 187, 61, 11, 250, 0]);

packages/std/src/hex_binary.rs

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,6 @@ impl fmt::Debug for HexBinary {
7979
}
8080
}
8181

82-
impl From<&[u8]> for HexBinary {
83-
fn from(binary: &[u8]) -> Self {
84-
Self(binary.to_vec())
85-
}
86-
}
87-
8882
/// Just like Vec<u8>, HexBinary is a smart pointer to [u8].
8983
/// This implements `*data` for us and allows us to
9084
/// do `&*data`, returning a `&[u8]` from a `&HexBinary`.
@@ -98,14 +92,27 @@ impl Deref for HexBinary {
9892
}
9993
}
10094

101-
// Reference
95+
impl AsRef<[u8]> for HexBinary {
96+
fn as_ref(&self) -> &[u8] {
97+
self.as_slice()
98+
}
99+
}
100+
101+
// Slice
102+
impl From<&[u8]> for HexBinary {
103+
fn from(binary: &[u8]) -> Self {
104+
Self(binary.to_vec())
105+
}
106+
}
107+
108+
// Array reference
102109
impl<const LENGTH: usize> From<&[u8; LENGTH]> for HexBinary {
103110
fn from(source: &[u8; LENGTH]) -> Self {
104111
Self(source.to_vec())
105112
}
106113
}
107114

108-
// Owned
115+
// Owned array
109116
impl<const LENGTH: usize> From<[u8; LENGTH]> for HexBinary {
110117
fn from(source: [u8; LENGTH]) -> Self {
111118
Self(source.into())
@@ -560,6 +567,34 @@ mod tests {
560567
assert_eq!(data_slice, &[7u8, 35, 49, 101, 0, 255]);
561568
}
562569

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+
563598
#[test]
564599
fn hex_binary_implements_hash() {
565600
let a1 = HexBinary::from([0, 187, 61, 11, 250, 0]);

0 commit comments

Comments
 (0)