Introduce impl_bytelike_traits macro#2861
Conversation
0f4511f to
302c331
Compare
dc5a377 to
d3f36fe
Compare
|
In 4287bf2ff35ca21cdd1f1e27341239fbcc499965: The new Other than that d3f36fe5a76dde18bb9238cd54f9612d6d5e5943 looks great! I wonder if later we will want to extend this macro to also add the as_byte_array etc inherents. |
d3f36fe to
9a0a711
Compare
|
Only change is adding the |
|
#2893 should fix CI. |
apoelstra
left a comment
There was a problem hiding this comment.
ACK 9a0a711462118ccd49683ef5564a0447ce471725
9a0a711 to
23c3a40
Compare
apoelstra
left a comment
There was a problem hiding this comment.
ACK 23c3a40545087084734af85ea0002099576641fe
hashes/src/sha256.rs
Outdated
There was a problem hiding this comment.
Isn't midstate mainly intended for creating tags? It thus shouldn't need all the traits.
But even more seriously, didn't we agree to have length in the midstate? That'd be incompatible with serialization. Do we really want to do this?
There was a problem hiding this comment.
Good points. I think we should back up, make the Midstate be a struct with a separate length and byte array (and maybe even a byte array for unhashed bytes? but then it would be identical to the Engine type)
We should provide inherents to access both fields, as well as a convenience function to check whether the length is a full multiple of 64 (or maybe this should be an invariant, and when extracting a midstate from an engine that has a non-multiple-of-64 it should fail?)
I think maybe we need to promote this to its own issue/discussion. Meanwhile I think the existing PR is API-diff-minimizing so we can accept it for now.
There was a problem hiding this comment.
Promoted to #2918
I'll leave it up to you whether you want to resolve that before accepting this PR.
There was a problem hiding this comment.
I think we should resolve it before accepting this specific change. The rest of the PR looks fine. (And no, I really don't want to add API that we suspect we will to remove in a subsequent PR potentially in a next release. Too much noise.)
|
Note to self, pick this up after #3010 merges. |
23c3a40 to
4b63cfe
Compare
|
Re-basing this after all the recent work on |
4b63cfe to
b895bbb
Compare
|
There are bugs in siphash still. Will try again another time. |
b895bbb to
089d50c
Compare
hashes/src/sha256t.rs
Outdated
There was a problem hiding this comment.
I'm pretty sure I've already said that the perf limitation is still in this macro. Is this broken rebase?
There was a problem hiding this comment.
Ah damn, it was risky rebasing this late yesterday evening, sorry for stealing your time.
805311c to
c128342
Compare
apoelstra
left a comment
There was a problem hiding this comment.
ACK c12834230aee70aa716550ce03170b5b7aa40242 successfully ran local tests; though I wonder if we want to move this macro to internals or even to hex?
|
Some considerations:
But if none of that matters, I can move it. |
I prefer keeping it out until there's a good reason (e.g. perf problems reversing the hashes). But I'm not strongly opposed to having it in
That's not a problem,
This was my intention for a while and I still think it's a good approach. But note that we want to also forbid adding items to the generated types because Rust considers them to belong to the caller crate which means orphan rules don't apply and adding any trait impl or associated item later is a breaking change. Versioning them separately might be needed/useful. |
|
Can we do it as a follow up (#3320) or do you guys want it done now? |
|
I'd prefer as a followup. It's more risky than I'd considered. |
|
Sweet ,lets go! |
hashes/src/macros.rs
Outdated
There was a problem hiding this comment.
Can't serde simply call as_ref instead of indexing? Should be a trivial change.
hashes/src/macros.rs
Outdated
There was a problem hiding this comment.
Maybe we should have Mut versions too. At least with some additional macro option.
There was a problem hiding this comment.
Agreed, but I do think it should be optional (and for hashes, off). I'd be fine adding in a followup. I guess, whatever followup moves the macro and makes it more generally useable.
There was a problem hiding this comment.
and for hashes, off
Why? I think it should be only off for things like Sighash. I agree it can be in followup. The reason I haven't acked is the SliceIndex stuff.
There was a problem hiding this comment.
I guess it's harmless, but why would anybody want to mutate a hash?
Good observation though that with Sighash there would be meaningful harm, so there's a reason to go out of our way to avoid it.
There was a problem hiding this comment.
why would anybody want to mutate a hash?
I really don't know except for mem::swap, but that'd just swap the entire hash without our support. (Though swapping with something weakly typed doesn't sound too crazy.)
My reason is mainly basic Rust consistency that unless we have a good reason to avoid a trait impl we should have the impl. This is also one of the Rust API guidelines.
This PR does not add This PR has been stalled for almost three months, can we just get it in and move on? |
|
Holy f***, there is already a draft PR to do the |
c128342 to
6524e1c
Compare
|
Currently is broken. Note the first patch was added after I struggled with the compiler only to find the same problem in patch 3. First one gets this error: Then if adding Before I rebased I did not see this problem, and I hard reset to double check nothing had changed - well something has changed but I can't work out what. |
6524e1c to
0ab26a0
Compare
|
Hey @apoelstra if you get a second could you take a look at this please, non-urgent though. If you check out the first commit and try and build you'll get error: expressions must be enclosed in braces to be used as const generic argumentsLooks simple, but I cannot workout how to fix it. The compiler suggestion just leads to another error. Thank in advance. |
|
@tcharding super weird. But this "fixes" it diff --git a/hashes/src/internal_macros.rs b/hashes/src/internal_macros.rs
index c231b237b..625b8d8d0 100644
--- a/hashes/src/internal_macros.rs
+++ b/hashes/src/internal_macros.rs
@@ -20,7 +20,7 @@
/// `from_engine` obviously implements the finalization algorithm.
macro_rules! hash_trait_impls {
($bits:expr, $reverse:expr $(, $gen:ident: $gent:ident)*) => {
- $crate::impl_bytelike_traits!(Hash, $bits / 8, $reverse $(, $gen: $gent)*);
+ $crate::impl_bytelike_traits!(Hash, { $bits / 8 }, $reverse $(, $gen: $gent)*);
impl<$($gen: $gent),*> $crate::GeneralHash for Hash<$($gen),*> {
type Engine = HashEngine;
diff --git a/hashes/src/macros.rs b/hashes/src/macros.rs
index fe658b17b..7a0cfdbb5 100644
--- a/hashes/src/macros.rs
+++ b/hashes/src/macros.rs
@@ -41,7 +41,7 @@ fn from_str(s: &str) -> $crate::_export::_core::result::Result<Self, Self::Err>
$crate::hex::impl_fmt_traits! {
#[display_backward($reverse)]
impl<$($gen: $gent),*> fmt_traits for $ty<$($gen),*> {
- const LENGTH: usize = $len;
+ const LENGTH: usize = ($len); // parens required due to rustc parser weirdness
}
}IMO there is some bug in the rustc parser somewhere. |
Nice find, out of interest what led you to try this? I tried putting |
|
The compiler suggested adding {}s as I did. At first I tried putting braces around all the uses of |
We have a couple of problems: 1. There are two macros currently for fmt stuff that do similar things, `arr_newtype_fmt_impl` and `hex_fmt_impl` - the difference is not immediately obvious, its the way that the byte array is iterated. 2. Our hash types are missing `AsRef<[u8; len]>` and `Borrow<[u8; len]>`. Introduce a new macro and remove a bunch of other macros. Include extensive docs but hide the macro from public docs because its not really for consumers of the library. The macro requires `$crate::hex` to point to `hex-conservative`. Note the macro is pretty generic (as in general purpose), `hashes` might not be the right home for it. Potentially a better place would be in `hex` itself?
0ab26a0 to
90b2ac0
Compare
Nice! I did everything else you did but did not make this leap. |
| assert_eq!(display, format!("{}", &outpoint)); | ||
|
|
||
| let pretty_debug = "OutPoint {\n txid: 0000000000000000000000000000000000000000000000000000000000000000,\n vout: 4294967295,\n}"; | ||
| let pretty_debug = "OutPoint {\n txid: 0x0000000000000000000000000000000000000000000000000000000000000000,\n vout: 4294967295,\n}"; |
There was a problem hiding this comment.
In 90b2ac0:
Even though this is just debug output, I think it makes the crate look sloppy and we should find a way to avoid it.
There was a problem hiding this comment.
Do you mean the change in this patch looks sloppy or that using 0x in pretty debugout put looks sloppy? (I'm guessing the second one). Notice that this is already the current behaviour for Txids, before this PR we had different behaviour for pretty printing Txid and OutPoint::txid - this PR makes it uniform.
There was a problem hiding this comment.
I mean using 0x for txids.
Good catch finding the non-uniformity -- but we should get rid of the 0x in other places rather than adding one here.
There was a problem hiding this comment.
Can we merge this as is and fix the Txid as follow up? This PR really just uncovers the issue, it doesn't introduce it.
We have a couple of problems:
There are two macros currently for fmt stuff that do similar things,
arr_newtype_fmt_implandhex_fmt_impl- the difference is not immediately obvious, its the way that the byte array is iterated.Our hash types are missing
AsRef<[u8; len]>andBorrow<[u8; len]>.Introduce a new macro and remove a bunch of other macros. Include extensive docs but hide the macro from public docs because its not really for consumers of the library.
The macro requires
$crate::hexto point tohex-conservative.Note the macro is pretty generic (as in general purpose),
hashesmight not be the right home for it. Potentially a better place would be inhexitself?