-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
const generics #116
const generics #116
Conversation
This reverts commit eebdb2c.
sorry, had this up on my phone in my pocket which caused the closing and opening. Almost posted a chunk of garbled text as a comment too... |
While stepping through the macro I noticed that to use As for I honestly am not sure where to go from here, It could be something I don't know or it might just be that |
@mriise I had a look at the derive macro part (which is indeed quite complicated, it took me a while to get into again). You can cherry-pick this commit: 19a44ff It only fixes the derive macro, the build still fails. I indeed just moved things to |
Thank you so much @vmx, IDK what it was that was confusing me so much. |
Fix for above was as simple as using a different generic for the digest, and rust compiler being amazing as it is implies fn multihash_from_digest<'a, D, const S: usize>(digest: &'a D) -> Multihash<SIZE>
where
D: Digest<S>,
Self: From<&'a D>; I removed I believe the last mole to whack is serde. |
It is confusing. It took my quite some time to dig into this code again. The final fix looks trivial, but getting there wasn't :)
I can't really tell what's best from reading the threads, which also means I don't have a strong preference. So I suggest trying things out and see what works (best). |
@vmx simplest option was to use Everything builds, passes all tests, and benches, so I think it should be good to review. just saw this
|
@@ -73,27 +70,37 @@ pub trait MultihashDigest: | |||
/// ``` | |||
#[cfg_attr(feature = "serde-codec", derive(serde::Deserialize))] | |||
#[cfg_attr(feature = "serde-codec", derive(serde::Serialize))] | |||
#[cfg_attr(feature = "serde-codec", serde(bound = "S: Size"))] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dont think removing this has any Ill effects, but im not 100% sure.
did not mean to force push that... |
build fails because of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vmx I think this answers your question
impl StatefulHasher for Sha2_256Truncated20 { | ||
type Size = U20; | ||
type Digest = Sha2Digest<Self::Size>; | ||
impl StatefulHasher<20> for Sha2_256Truncated20 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should still be an associated constant
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what's Self::SIZE
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Self::SIZE
replaced size()
since while it could be a const fn
it made more sense (in my mind) to make it a public associated constant.
It would make sense to move back to const fn size()
in the case of having SIZE
be an associated constant without being repetitive.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems using an assosicated const as a paramater is still part of the incomplete const_generics
feature in nightly, though with enough feature flags we can make it look something like this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not being able to have it as associate constant in current stable Rust sucks :-/
I guess we leave it like that then, until it;s possible in stable Rust? What do others think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
are we in a hurry to merge this? it's a usability regression, and comparing it to the benefits, maybe we should hold off until more of const-generics is stabilized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not in a hurry. Though I fear that it will take a long time until it's usable on stable.
@mriise What do you think about perhaps fixing everything up a single commit (once we are happy with the state of it) and we just keep it open? Then people who want const generic support can easily cherry-pick it themselves and it should also be easier to rebase it if we make other changes (e.g. fixing the blake3 hashing). This way all your work is not lost, still usable and hopefully find it's way in some day.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although it is disappointing, I would have to agree. I'll do the final bits of cleanup and request another review.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, is this really a regression? Or could we use this to trim down our digest list a bit (e.g., for all the Blake2s variants).
A different way to implement const generics for certian static sized algorithms could be this. This should compile fine and is probably part of the incomplete part. This differs a bit more in complexity but only requires one flag instead of the three in my last suggested option. |
|
@mriise thanks for your work on this PR even if it takes a while for it to get merged |
@mriise Sure, as we identified it surely isn't time sensitive. I'm also disappointed that it cannot just land. I was warned, but I had hoped we really only need the |
TL;DR for anyone in the future, this PR made the change of |
@vmx Can you consider merging this PR and releasing a new version? I really need it. |
@koushiro As mentioned in this comment, this PR makes the library harder to use. So the folks currently involved in this PR decided to wait until |
There may be an alternate approach i've been thinking of... I will play with it a bit and will let you guys know if it will work out. |
Well, I about tried everything I could think. At this point it boils down to 3 alternatives:
|
I'd be in favour of keeping the current API. I don't see a reason to change the API, just for moving from GenericArray to const generics. Though I've little experience on that side of things, so if there would be a good reason, please let me know. Thanks again @mriise for looking into that. |
There was one last thing I thought of before setting this aside again that relied heavily on const expressions to convert typenum into const generics, but implementation details are a bit fuzzy and the current implementation isn't ready for it anyway. So this may be a thing to look at again if full const generics gets stabalized before GATs do (it is anyone's guess at this point i think). And no problem @vmx, this has been a good challenge for me. |
still requires nightly features for into/from<[u8; S]> but otherwise OK |
@vmx and I discussed this offline and, while annoying, the issues with the interface changes don't outweigh the benefits. We're going to try to move this forward. |
Rebased and squashed from #116. Closes #114. BREAKING CHANGE: This change replaces uses of typenum with const generics. Importantly, the MultihashDigest, Hasher, and StatefulHasher traits are now generic over the digest size due to compiler limitations. However, `Hasher` exposes this size as an associated const, so `MyHasherImpl::SIZE` can often be used to fill in the size parameter with some help from type inference. Additionally: - All usages of typenum must be replaced with usize constants. - The new MSRV is 1.51.0 due to const generics. - IdentityDigest is now a tuple struct of usize and the digest instead of u8 and a generic array.
Changes merged into the next branch. |
What this PR does
[0u8; SIZE]
\What it doesn't do:
modify user api to make use of const generics e.g.(this can't be done without more modification of the proc macro, so instead should probably be another PR)Blake3::<SIZE>::digest(&[u8])
buildmodify proc macrofix SHA macro_rules!some other things that need to be squashed to even build.