-
-
Notifications
You must be signed in to change notification settings - Fork 463
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 displaydoc
crate to derive fmt::Display
for SignalProtocolError
#296
add displaydoc
crate to derive fmt::Display
for SignalProtocolError
#296
Conversation
https://crates.io/crates/displaydoc might come handy as well. ;) |
Right, we've previously had a PR to switch to |
Got it! I'll try out displaydoc! Might steal from #287! |
c2c1067
to
9df218d
Compare
Ok, redone to use |
9df218d
to
e7c573f
Compare
thiserror
crate to derive fmt::Display
for SignalProtocolError
displaydoc
crate to derive fmt::Display
for SignalProtocolError
rust/protocol/src/error.rs
Outdated
use std::fmt; | ||
use std::panic::UnwindSafe; | ||
|
||
pub type Result<T> = std::result::Result<T, SignalProtocolError>; | ||
|
||
/// Wraps a boxed error struct and delegates the [std::error::Error] trait to it. | ||
/// | ||
/// A [Box] wrapping an error apparently does not implement [std::error::Error] itself, which breaks |
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.
Hm, that implementation is in the stdlib. Something else must be going on.
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.
Unfortunately, there is a distinction between Box<E: Error>
and Box<dyn Error>
. I have added this explanatory link to the docstring: https://stackoverflow.com/a/62452516/2518889.
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 believe we could work around this by adding a type parameter E: Error
to the definition of SignalProtocolError
, but that seems like it would make instantiating SignalProtocolError
even more annoying than wrapping in CallbackErrorWrapper
.
f7a8ba8
to
1fd0010
Compare
rust/protocol/src/error.rs
Outdated
/// Wraps a boxed error struct and delegates the [std::error::Error] trait to it. | ||
/// | ||
/// Unlike a `Box<E: Error>` wrapping a concrete error type, a `Box<dyn Error>` wrapping | ||
/// a type-erased trait object does *not* implement [std::error::Error] itself (see [this | ||
/// stackoverflow answer] for details). This breaks the ability to nicely use the `#[source]` | ||
/// annotation from the [thiserror::Error] derive macro. In order to avoid manually impling | ||
/// [std::error::Error::source] for each case of [SignalProtocolError], we create this wrapper | ||
/// struct to delegate to the underlying `dyn Error` in order to nicely derive [thiserror::Error] | ||
/// for [SignalProtocolError::ApplicationCallbackError]. | ||
/// | ||
/// [this stackoverflow answer]: https://stackoverflow.com/a/62452516/2518889 |
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 the resulting code is clearer than just implementing source
manually (and therefore Error). In the choice between making one place a lot more complicated and many places a little more complicated, I'd usually err on the side of containing the complexity. (If only we didn't need UnwindSafe 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.
Thank you so much for investigating where that might be added to thiserror
! I have removed the thiserror
dependency for now along with CallbackErrorWrapper
until we can avoid making many places a little more complicated as described.
3980aab
to
0a76d24
Compare
- remove thiserror for now until we can derive UnwindSafe
0a76d24
to
497ded2
Compare
Okay, the Java failure is our fault for me coincidentally picking a bad nightly, rust-lang/cargo#9919, so I'll merge this anyway. |
Problem
I looked into @jrose-signal's comment #287 (comment) comparing the use of
thiserror
vs adding real docstrings as in #287. I believe they are totally orthogonal.With that in mind, it looks like
displaydoc
might be able to just save some of our boilerplate inerror.rs
.Solution
displaydoc::Display
and add docstrings to every case ofSignalProtocolError
to auto-derivefmt::Display
.Result
Our
fmt::Display
implementation forSignalProtocolError
requires less code because we can rely on a macro to derive it! We also end up adding a first pass of documentation to all of our error cases!