-
Notifications
You must be signed in to change notification settings - Fork 407
Fix compiling lightning-invoice for no-std + serde #2187
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
Merged
TheBlueMatt
merged 2 commits into
lightningdevkit:main
from
benthecarman:invoice-serde-no-std
Apr 17, 2023
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 hidden or 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 hidden or 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.
Just changing
format!
toformat_args!
would've been better since it may avoid allocation.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 think so?
format_args
holds a reference to everything passed, which in this case would mean it has a lifetime bound on the lifetime of e.More generally, in practice would that ever actually avoid allocation? I'm not particularly farmiliar with serde, but we're passing a
Display
-implementing object to a generic error type, presumably it'll ~always store the string version of it?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.
Firstly, if you call
format!
you have two allocations - one for the string stored inside serde error, the other for the temporary string. So it should be always better to useformat_args!
.Secondly while it's true that most if not all serializers in practice do store string, it's possible that someone somewhere decides to write a serializer that directly logs the errors instead of storing them just to minimize allocations here.
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.
Right, but none of that addresses the fact that the extra lifetime implies this won't compile :)
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 extra lifetime?
serde::de::Error::custom
takes anyT: Display
, there's not'static
bound.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.
Yes, but the format_args will have a lifetime which is bounded by the e here which we're map_err'ing. That lifetime bound will fail because it's no longer a live reference once we return from the closure. Similar to how you can't use format_args in if let bounds.
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's not a problem because
E::custom
doesn't store the argument inside (the signature prevents it). It either stores aString
(possibly something exotic likeArc<str>
) or logs it.Anyway, no point in trying to convince you by arguing if I can make a PR :)
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.
Oh oh, I'd somehow read that as a enum variant, rather than a method, missing that the method turns it into a string, instead of trying to place the
Display
in an enum variant. I really hate Rust sometimes, so many cases of easily seeing what's going on, and a handful of cases where you can't tell what's up from reading the code.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 see. I think casing (
custom
vsCustom
) should've made it clear?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.
If I'd read it more carefully it would have.