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

Make it easy to access the contents of any hash (newtype) #81

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/hash160.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ hex_fmt_impl!(LowerHex, Hash);
index_impl!(Hash);
serde_impl!(Hash, 20);
borrow_slice_impl!(Hash);
from_hash_for_inner_impl!(Hash);

impl str::FromStr for Hash {
type Err = ::hex::Error;
Expand Down
17 changes: 17 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ macro_rules! hash_newtype {
}
}

impl ::std::convert::From<$newtype> for <$hash as $crate::Hash>::Inner {
fn from(hashtype: $newtype) -> <$hash as $crate::Hash>::Inner {
hashtype.0.into_inner()
}
}

impl $crate::Hash for $newtype {
type Engine = <$hash as $crate::Hash>::Engine;
type Inner = <$hash as $crate::Hash>::Inner;
Expand Down Expand Up @@ -242,5 +248,16 @@ mod test {
let h2: TestNewtype = h.to_string().parse().unwrap();
assert_eq!(h2.as_hash(), h);
}

#[test]
fn convert_using_std_into() {
fn i_accept_plain_arrays<T: Into<[u8; 32]>>(whatever: T) {
let _x = whatever.into();
}

let h1 = TestNewtype::hash(&[]);

i_accept_plain_arrays(h1);
}
}

1 change: 1 addition & 0 deletions src/ripemd160.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ hex_fmt_impl!(LowerHex, Hash);
index_impl!(Hash);
serde_impl!(Hash, 20);
borrow_slice_impl!(Hash);
from_hash_for_inner_impl!(Hash);

impl str::FromStr for Hash {
type Err = ::hex::Error;
Expand Down
1 change: 1 addition & 0 deletions src/sha1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ hex_fmt_impl!(LowerHex, Hash);
index_impl!(Hash);
serde_impl!(Hash, 20);
borrow_slice_impl!(Hash);
from_hash_for_inner_impl!(Hash);

impl str::FromStr for Hash {
type Err = ::hex::Error;
Expand Down
1 change: 1 addition & 0 deletions src/sha256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ hex_fmt_impl!(LowerHex, Hash);
index_impl!(Hash);
serde_impl!(Hash, 32);
borrow_slice_impl!(Hash);
from_hash_for_inner_impl!(Hash);

impl HashTrait for Hash {
type Engine = HashEngine;
Expand Down
1 change: 1 addition & 0 deletions src/sha256d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ hex_fmt_impl!(LowerHex, Hash);
index_impl!(Hash);
serde_impl!(Hash, 32);
borrow_slice_impl!(Hash);
from_hash_for_inner_impl!(Hash);

impl str::FromStr for Hash {
type Err = ::hex::Error;
Expand Down
6 changes: 6 additions & 0 deletions src/sha256t.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ impl<T: Tag> HashTrait for Hash<T> {
}
}

impl<T> From<Hash<T>> for <Hash<T> as HashTrait>::Inner where T: Tag {
fn from(hash: Hash<T>) -> Self {
hash.into_inner()
}
}

/// Macro used to define a newtype tagged hash.
/// It creates two public types:
/// - a sha246t::Tag struct,
Expand Down
1 change: 1 addition & 0 deletions src/sha512.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ hex_fmt_impl!(LowerHex, Hash);
index_impl!(Hash);
serde_impl!(Hash, 64);
borrow_slice_impl!(Hash);
from_hash_for_inner_impl!(Hash);

impl HashTrait for Hash {
type Engine = HashEngine;
Expand Down
1 change: 1 addition & 0 deletions src/siphash24.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ hex_fmt_impl!(LowerHex, Hash);
index_impl!(Hash);
serde_impl!(Hash, 8);
borrow_slice_impl!(Hash);
from_hash_for_inner_impl!(Hash);

impl str::FromStr for Hash {
type Err = ::hex::Error;
Expand Down
10 changes: 10 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,16 @@ macro_rules! engine_input_impl(
)
);

macro_rules! from_hash_for_inner_impl(
($hash:ty) => (
impl From<$hash> for <$hash as HashTrait>::Inner {
fn from(hash: $hash) -> Self {
hash.into_inner()
}
}
)
);



macro_rules! define_slice_to_be {
Expand Down