Skip to content
This repository was archived by the owner on Nov 30, 2022. It is now read-only.

Commit 357732b

Browse files
committed
Add an as_bytes method to hash types
We now implement `AsRef` for hash types, sometimes usage of `as_ref` may required type annotations. For such cases we can provide an alternate way to get the underlying bytes to make the API more ergonomic. Add a method `as_bytes` for getting a reference to the inner byte array. Add for new types created by `hash_newtype` also.
1 parent b13b06a commit 357732b

File tree

9 files changed

+30
-0
lines changed

9 files changed

+30
-0
lines changed

src/hash160.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ pub struct Hash(
3636
hex_fmt_impl!(Hash);
3737
serde_impl!(Hash, 20);
3838
borrow_slice_impl!(Hash);
39+
as_bytes_impl!(Hash, 20);
3940

4041
impl str::FromStr for Hash {
4142
type Err = hex::Error;

src/ripemd160.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ pub struct Hash(
8585
hex_fmt_impl!(Hash);
8686
serde_impl!(Hash, 20);
8787
borrow_slice_impl!(Hash);
88+
as_bytes_impl!(Hash, 20);
8889

8990
impl str::FromStr for Hash {
9091
type Err = hex::Error;

src/sha1.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ pub struct Hash(
8080
hex_fmt_impl!(Hash);
8181
serde_impl!(Hash, 20);
8282
borrow_slice_impl!(Hash);
83+
as_bytes_impl!(Hash, 20);
8384

8485
impl str::FromStr for Hash {
8586
type Err = hex::Error;

src/sha256.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ impl str::FromStr for Hash {
8787
hex_fmt_impl!(Hash);
8888
serde_impl!(Hash, 32);
8989
borrow_slice_impl!(Hash);
90+
as_bytes_impl!(Hash, 32);
9091

9192
impl crate::Hash for Hash {
9293
type Engine = HashEngine;
@@ -159,6 +160,7 @@ pub struct Midstate(pub [u8; 32]);
159160
hex_fmt_impl!(Midstate);
160161
serde_impl!(Midstate, 32);
161162
borrow_slice_impl!(Midstate);
163+
as_bytes_impl!(Midstate, 32);
162164

163165
impl str::FromStr for Midstate {
164166
type Err = hex::Error;

src/sha256d.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pub struct Hash(
3131
hex_fmt_impl!(Hash);
3232
serde_impl!(Hash, 32);
3333
borrow_slice_impl!(Hash);
34+
as_bytes_impl!(Hash, 32);
3435

3536
impl str::FromStr for Hash {
3637
type Err = hex::Error;

src/sha256t.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ impl<T: Tag> str::FromStr for Hash<T> {
8080

8181
hex_fmt_impl!(Hash, T:Tag);
8282
borrow_slice_impl!(Hash, T:Tag);
83+
as_bytes_impl!(Hash, 32, T:Tag);
8384

8485
impl<T: Tag> crate::Hash for Hash<T> {
8586
type Engine = sha256::HashEngine;

src/sha512.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ impl str::FromStr for Hash {
136136
hex_fmt_impl!(Hash);
137137
serde_impl!(Hash, 64);
138138
borrow_slice_impl!(Hash);
139+
as_bytes_impl!(Hash, 64);
139140

140141
impl crate::Hash for Hash {
141142
type Engine = HashEngine;

src/siphash24.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ pub struct Hash(
204204
hex_fmt_impl!(Hash);
205205
serde_impl!(Hash, 8);
206206
borrow_slice_impl!(Hash);
207+
as_bytes_impl!(Hash, 8);
207208

208209
impl str::FromStr for Hash {
209210
type Err = hex::Error;

src/util.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,22 @@ macro_rules! borrow_slice_impl(
7070
)
7171
);
7272

73+
/// Adds a method `as_bytes` to a given type `$ty`.
74+
#[macro_export]
75+
macro_rules! as_bytes_impl(
76+
($ty:ident, $len:expr) => (
77+
$crate::as_bytes_impl!($ty, $len, );
78+
);
79+
($ty:ident, $len:expr, $($gen:ident: $gent:ident),*) => (
80+
impl<$($gen: $gent),*> $ty<$($gen),*> {
81+
/// Returns `self` as a byte array reference.
82+
pub fn as_bytes(&self) -> &[u8; $len] {
83+
&self.0
84+
}
85+
}
86+
);
87+
);
88+
7389
macro_rules! engine_input_impl(
7490
() => (
7591
#[cfg(not(fuzzing))]
@@ -128,6 +144,11 @@ macro_rules! hash_newtype {
128144
// Hashes implement Copy so don't need into_hash.
129145
self.0
130146
}
147+
148+
/// Returns `self` as a byte array reference.
149+
pub fn as_bytes(&self) -> &[u8; $len] {
150+
&self.0.as_bytes()
151+
}
131152
}
132153

133154
impl $crate::_export::_core::convert::From<$hash> for $newtype {

0 commit comments

Comments
 (0)