-
Notifications
You must be signed in to change notification settings - Fork 24
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
decode: Add const-compatible decoder #116
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #116 +/- ##
==========================================
+ Coverage 72.91% 76.42% +3.50%
==========================================
Files 4 4
Lines 288 352 +64
==========================================
+ Hits 210 269 +59
- Misses 78 83 +5 ☔ View full report in Codecov by Sentry. |
024c792
to
a1ed6b3
Compare
Thanks for the PR. I do think this is worth supporting till we get const-traits, since those seem quite far away. I have a couple ideas around the API that I want to test out, I should have time to do that in the next few days. |
Thanks for your response, sounds great! Let me know if I can help in any way. |
I've pushed the API idea I had to main...Nemo157:bs58-rs:const-integrated, essentially making as much as possible of the |
This looks great to me! It's definitely clearer to have it all in one place and calling out the const-compatible functions at the decoder level. And it works perfectly for my use-case, I tested your branch locally. Feel free to push the changes however you think is best, and thanks for doing the work! |
Problem
The base58 decoding functions in this crate work extremely well, but they are not available in
const
contexts.In case you'd like to hear about a specific use-case, in Solana programs, we define the canonical program-id at the top-level of a crate to be consumed by others, ie:
https://github.com/solana-labs/solana-program-library/blob/7e022ac1b2ac527d6607712521685e64d0ff50f8/token/program/src/lib.rs#L85
Currently, that's done with a string literal, so we can decode it in a macro using the normal
bs58::decode
. It only works with string literals, however, and not&str
or any other macro that returns a string.Summary of changes
To allow users to decode any string to base58 in a const context, this PR adds in a new separate decoder which is only meant for const usage.
It's unfortunately much more limited than the normal decoder due to the limitations on
const
in Rust, so many features needed to be completely dropped for it:Sha2
hashing is not available in const contexts, to the"check"
and"cb58"
features cannot be supported, which means that no checks are possibleunwrap()
errors in const, so the error conditions are simply assertionsPlease let me know if you have any questions or comments! Everything is very well documented in this crate, so I aimed to follow the existing conventions.