-
Notifications
You must be signed in to change notification settings - Fork 47
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
Add encode_to_fmt
method for efficient encoding
#30
Conversation
940fcad
to
089b9e4
Compare
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.
LGTM.
@sgeisler would you like an additional method directly producing a string based on this? I don't need it, but it makes sense because using pub fn encode<T: AsRef<[u5]>>(hrp: &str, data: T) -> String; |
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.
That last comment gave me the impression that you intend this function to be used by "normal" users. I don't know if I'd like that in it's current form (for some odd reason I considered it an internal optimization to be used only by us before).
If what you want to do is avoid allocating/cloning (which is currently necessary to construct a Bech32
struct) it might be better to create a BorrowedBech32<'a>
struct or use a Cow<'a, T>
in Bech32
. The problem with (&str, &[u5])
as basis for bech32 encoding as proposed here is that it doesn't enforce the invariants that are needed to guarantee that standard compliant bech32 is produced when encoding (e.g. no mixed case).
So you are saying we should make sure that the |
Yes, it's part of the specification that a bech32 string has only one case. And if encoding only lowercase is allowed and it's left to the application to transform it to uppercase if QR encoding is intended.
The IRC discussion lead to the insight that we can just put the encoding validation logic into the encoding function and get rid of the |
The check that makes sure the HRP is valid is quite negligible, no? Most prefixes will be < 10 characters long and the check is just a byte comparison IIRC. The only question is then whether to panic or Err on an incorrect one. |
Superseded by #34. Let's move discussion there. |
@stevenroose Okay to close? |
Export a public method that directly writes a
Vec<u5>
to anfmt::Formatter
.