-
Notifications
You must be signed in to change notification settings - Fork 1.7k
rust: use thiserror
for error types
#4341
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ limitations under the License. | |
|
||
//! Checksums as used by TFRecords. | ||
|
||
use std::fmt::{self, Debug}; | ||
use std::fmt::{self, Debug, Display}; | ||
|
||
/// A CRC-32C (Castagnoli) checksum that has undergone a masking permutation. | ||
/// | ||
|
@@ -30,7 +30,13 @@ pub struct MaskedCrc(pub u32); | |
// Implement `Debug` manually to use zero-padded hex output. | ||
impl Debug for MaskedCrc { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(f, "MaskedCrc({:#010x?})", self.0) | ||
write!(f, "MaskedCrc({})", self) | ||
} | ||
} | ||
|
||
impl Display for MaskedCrc { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't really understand why the In Rust docs, some examples don't include it [1], while others do [2]. From what I gather, [1] https://doc.rust-lang.org/std/fmt/struct.Formatter.html#examples There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You’re right—it isn’t needed here. You can read
The reason that it’s here is that I write trait implementations by impl Display for Foo {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
todo!()
}
} Given that it’s optional, I don’t have a strong preference. In the I’m also weakly inclined to deem it acceptable to write the code that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the links and explanation! The RFC sheds a lot of light for me on why they decided it was good to explicitly use the '_ placeholder. I don't yet have an opinion on when we should do it, but Rust reference also says "For lifetimes in paths, using '_ is preferred", so in this case it's seems idiomatic. |
||
write!(f, "{:#010x?}", self.0) | ||
} | ||
} | ||
|
||
|
@@ -78,9 +84,11 @@ mod tests { | |
#[test] | ||
fn test_debug() { | ||
let long_crc = MaskedCrc(0xf1234567); | ||
assert_eq!(format!("{}", long_crc), "0xf1234567"); | ||
assert_eq!(format!("{:?}", long_crc), "MaskedCrc(0xf1234567)"); | ||
|
||
let short_crc = MaskedCrc(0x00000123); | ||
assert_eq!(format!("{}", short_crc), "0x00000123"); | ||
assert_eq!(format!("{:?}", short_crc), "MaskedCrc(0x00000123)"); | ||
} | ||
} |
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.
Is this comment still relevant for
Debug
, now that it writesMasked({})
without using the:#010x
padding option?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, I think so. The point of the comment is to explain why we don’t
just write
#[derive(Debug)]
. This patch changes the implementation,but not the behavior. If we derived
Debug
, the output would still beMaskedCrc(4660)
rather thanMaskedCrc(0x1234)
, so the comment isstill as helpful as it was before.
In fact, I think it’s a bit more helpful than before, since before the
:#010x?
was at least immediately following in the code. Now, if youdon’t look at the
Display
implementation, it’d be reasonable to ask,“this just wraps the
Display
in the struct name; at that point, whynot just derive
Debug
?”—which the comment answers.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 guess the tests answer it, too. :-)