-
Notifications
You must be signed in to change notification settings - Fork 58
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
conversion: introduce 0-alloc IntoBig method #177
Merged
+97
−11
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 don't like to have a double-pointer in the API. It's not nice to work with, because it's so unusual.
I can be persuaded otherwise, but, why not just pick one of these options:
z
is nilb
to zero ifz
is nilAnd then get rid of the
**
?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.
Well, the purpose is to convert between uint256 and big.Int. If nil is a valid uint256 value, it we should be able to convert it to big.Int. IMO we''ll shoot ourselves in the foot with all the optional uint256 fields if we panic all of a sudden. Also converting an unset-optional into a set-0 seems bad.
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.
**big.Int is similar to *[] (i.e. slice pointer) FWIW. Maybe unusual to see a double pointer like that, but it's not a unique construct. You can definitely work around it by delegating nil checks to outer code, but then half the conversion happens inside the lib, but the other half (allocating/nilling the big.Int happens outside).
IMO the API "strageness" isn't that bad compared to having everyone become responsible for handling nil/non-nil in their code explicitly.
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.
Point in case, straight from the Go standard library: https://github.com/golang/go/blob/b8f83e22703ee23d49d95154449ce7066402d5c9/src/crypto/internal/boring/boring.go#L83
Converting big ints by placing them into an existing variable :)
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.
Ok!
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.
But if
b
is nil, this will panic. Is that intentional? I guess it is, just want to double-check that's what we wantThere 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.
Apart from sending in nil explicitly, there's no meaningful way you can end up with nil.
i.e. if you have a nil big.int pointer, you can still take it's address and it will be a non-nil pointer you can use to init the big.int. Since nobody's passing double pointers around in general for big.ints, you can't realistically end up with an accidental nil double pointer.