Skip to content
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

Introduce write_err macro #446

Merged
merged 1 commit into from Jun 15, 2022
Merged

Conversation

tcharding
Copy link
Member

@tcharding tcharding commented Jun 10, 2022

Introduce write_err macro and improve the Display implementation on Error.

This PR is re-written after review below. The std::error::Error implementation is removed in favour of #409. Now only includes the Display stuff.

Copy link
Collaborator

@Kixunil Kixunil left a comment

Choose a reason for hiding this comment

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

While strictly correct, I'd like to see write_err!() used.

src/key.rs Outdated
impl std::error::Error for InvalidParityValue {}
impl std::error::Error for InvalidParityValue {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
None
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is the point of this to express "no, we didn't forget about this"?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm going to remove this, I did not think thoroughly enough when doing this to realise I was just adding the default implementation :) Thanks

src/lib.rs Outdated
NotEnoughMemory => "secp: not enough memory allocated",
InvalidPublicKeySum => "secp: the sum of public keys was invalid or the input vector lengths was less than 1",
InvalidParityValue(_) => "couldn't create parity",
})
Copy link
Collaborator

Choose a reason for hiding this comment

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

I believe it was previously more readable, but we should use write_err!() anyway. I think it's OK to copy-paste the macro?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done, as suggested. Cheers.

The current display code for `Error` is a little unusual. We typically
just implement `Display` and if a `str` is needed use `format!`.

Improve the `Error` type by doing

- Remove the `as_str` function and implement `Display` directly.
- Remove the 'secp: ' prefix of all the error messages.
- Use a newly defined macro `write_err` that writes the error if `std`
  feature is not enabled so that no-std builds do not loose error info.

Note: The `write_err` macro is currently being introduced in
`rust-bitcoin` also. Elect to just duplicate it here and not share it
between the crates.
@tcharding tcharding changed the title Implement std::error::Error Introduce write_err macro Jun 13, 2022
Copy link
Member

@apoelstra apoelstra left a comment

Choose a reason for hiding this comment

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

ACK 946cd83 nice!

@apoelstra apoelstra merged commit 6577fba into rust-bitcoin:master Jun 15, 2022
@tcharding tcharding deleted the 06-10-std-error branch July 21, 2022 03:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants