More robust and versatile implementation of derive(Debug) and derive(Display). Unlike the version ofderive(Debug)
in the standard library, these macros will always successfully generate implementation - even if a member does not
implement Debug/Display. In that case, the generated implementation will print a replacement string of the form
<TypeName>.
More Robust
These derive macros always work, even when derive(std::fmt::Debug) fails.
# use _rt; // required for doc-tests in this crate only
;
;
Drop in Usage
Anything that derives [std::fmt::Debug] or [core::fmt::Debug] can derive fmt_derive::Debug instead
without any changes required.
However, there is a small problem when useing both at the same time:
# use _rt; // required for doc-tests in this crate only
#
// error[E0252]: the name `Debug` is defined multiple times
use Debug;
use Debug;
The same problem exists for Display:
# use _rt; // required for doc-tests in this crate only
#
// error[E0252]: the name `Display` is defined multiple times
use Display;
use Display;
If you encounter this problem, there are two simple solutions
- Qualifying the derive macro.
#[derive(fmt_derive::Debug)]works just fine - Rewriting the imports.
use fmt_derive::Debug;also pulls in the [std::fmt::Debug]/[core::fmt::Debug] trait (not just the macro).
# use _rt; // required for doc-tests in this crate only
use Debug; // replace `use std::fmt::Debug;` and `use core::fmt::Debug;`
;
;
More Versatile
The derived implementation can be easily customized using additional attributes.
Custom Format Expressions
A custom representation can be quickly derived using a format expression for the whole structure, enumeration, or
untagged unions. This is the expected case when deriving Display or when
# use _rt; // required for doc-tests in this crate only
use ;
;
Custom Format Expressions for Enumeration Variants
For enumerations, variants can also be customized:
# use _rt; // required for doc-tests in this crate only
use Debug;
Custom Format Expressions for Individual Fields
Or by customizing an individual field:
# use _rt; // required for doc-tests in this crate only
use Debug;
] u32);
Ignoring a Field
Although it is possible to derive a debug message for any field, it is sometimes preferable to not print a field at all:
# use _rt; // required for doc-tests in this crate only
use Debug;
] fn);
TODO
- The format expressions on enumeration variants and fields show that this crate will probably need a better method of referring to fields (currently,
selfis accessible, which requires unwrapping the variant each time it is used)