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

Conversation

thomaseizinger
Copy link

Hashes can be represented as fixed length arrays. That is in fact
what they are internally.
There is value in using Rust's visibility rules to enforce that
no every byte array is a valid hash.
However, every hash is certainly a valid byte array, hence it
should be easy to access their contents as such.

We add a From impl for any hash newtype for the inner byte array.
This makes it possible to pass hash-newtypes the way they are to
any function that needs a byte-array.

We also add a From impl for any inner hash type that allows us
to convert to the inner representation.

Both of these changes are especially useful from a broader ecosystem
perspective:

Because From and Into are part of the standard library, any
application or crate depends on them and can write functions that
support a form of method overloading by accepting anything that is
Into<[u8; 32]>.

Implementing these conversion traits on our types makes them
compatible with this pattern which results in less verbose APIs.

@stevenroose
Copy link
Collaborator

I'm curious what kind of situations you need this for..

@thomaseizinger
Copy link
Author

I'm curious what kind of situations you need this for..

If rust-bitcoin/rust-secp256k1#211 is not accepted, then the From abstraction might not be as useful because on an individual basis, you can also just use into_inner.

One could still argue that it is good use of the standard library to provide these conversion functions if the conversion exists, just to allow a more flexible use of the types :)

@apoelstra
Copy link
Member

concept ACK. I wonder if this would work now with 1.29 as the MSRV.

@thomaseizinger
Copy link
Author

concept ACK. I wonder if this would work now with 1.29 as the MSRV.

I rebased onto latest master and pushed again, let's see what CI thinks.

Hashes can be represented as fixed length arrays. That is in fact
what they are internally.
There is value in using Rust's visibility rules to enforce that
no every byte array is a valid hash.
However, every hash is certainly a valid byte array, hence it
should be easy to access their contents as such.

We add a `From` impl for any hash newtype for the inner byte array.
This makes it possible to pass hash-newtypes the way they are to
any function that needs a byte-array.

We also add a `From` impl for any inner hash type that allows us
to convert to the inner representation.

Both of these changes are especially useful from a broader ecosystem
perspective:

Because `From` and `Into` are part of the standard library, any
application or crate depends on them and can write functions that
support a form of method overloading by accepting anything that is
`Into<[u8; 32]>`.

Implementing these conversion traits on our types makes them
compatible with this pattern which results in less verbose APIs.
@thomaseizinger
Copy link
Author

thomaseizinger commented Dec 23, 2020

concept ACK. I wonder if this would work now with 1.29 as the MSRV.

Nope. We will need Rust 1.41 for parts of this: https://blog.rust-lang.org/2020/01/30/Rust-1.41.0.html#relaxed-restrictions-when-implementing-traits

Closing then.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants