Skip to content

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
merged 2 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions ci/ci-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ for DIR in lightning lightning-invoice lightning-rapid-gossip-sync; do
RUSTFLAGS="--cfg=c_bindings" cargo test --verbose --color always --no-default-features --features=no-std
popd
done
# This one only works for lightning-invoice
pushd lightning-invoice
# check that compile with no-std and serde works in lightning-invoice
cargo test --verbose --color always --no-default-features --features no-std --features serde
popd

echo -e "\n\nTesting no-std build on a downstream no-std crate"
# check no-std compatibility across dependencies
Expand Down
2 changes: 1 addition & 1 deletion lightning-invoice/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1648,7 +1648,7 @@ impl<'de> Deserialize<'de> for Invoice {
fn deserialize<D>(deserializer: D) -> Result<Invoice, D::Error> where D: Deserializer<'de> {
let bolt11 = String::deserialize(deserializer)?
.parse::<Invoice>()
.map_err(|e| D::Error::custom(format!("{:?}", e)))?;
.map_err(|e| D::Error::custom(alloc::format!("{:?}", e)))?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just changing format! to format_args! would've been better since it may avoid allocation.

Copy link
Collaborator

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?

Copy link
Contributor

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 use format_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.

Copy link
Collaborator

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 :)

Copy link
Contributor

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 any T: Display, there's not 'static bound.

Copy link
Collaborator

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.

Copy link
Contributor

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 a String (possibly something exotic like Arc<str>) or logs it.

Anyway, no point in trying to convince you by arguing if I can make a PR :)

Copy link
Collaborator

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.

Copy link
Contributor

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 vs Custom) should've made it clear?

Copy link
Collaborator

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.


Ok(bolt11)
}
Expand Down