-
Notifications
You must be signed in to change notification settings - Fork 0
Description
I've started using custom_debug to reduce the amount of hand-written Debug boilerplate in the codebases I work with. It's especially useful when there's, e.g., a single field in a large enum that you want to exclude, where otherwise you would have to rewrite the Debug logic for the whole enum, or at least the whole variant.
However, one thing I'm struggling with is the proliferation of fmt_* methods I end up with, for the different formatting possibilities. This is exacerbated by the fact that the with = "..." annotation doesn't 'compose' well with other crates like hex_fmt, nor with the format = "..." annotation.
I think a solution to this could be to introduce an as = "..." annotation, which would de-sugar thusly:
#[derive(custom_debug::Debug)]
struct MyType {
#[debug(as = "hex_fmt::HexFmt")]
foo: [u8; 32],
}
// generated impl
impl fmt::Debug for MyType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("MyType")
.field("foo", &hex_fmt::HexFmt(&self.foo))
.finish()
}
}It could even compose with format = "...":
#[derive(custom_debug::Debug)]
struct MyType {
#[debug(as = "hex_fmt::HexFmt", format = "{:6?}")]
foo: [u8; 32],
}
// generated impl
impl fmt::Debug for MyType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("MyType")
.field("foo", &format_args!("{:6?}", hex_fmt::HexFmt(&self.foo)))
.finish()
}
}The value given to as = "..." for a field of type T would have to refer to a function with signature fn(&T) -> impl fmt::Debug.
I'm not very familiar with derive macros, but if this is something you'd be open to adding I could look to create a PR.