Expand description
§display_as_debug
A lightweight utility crate that lets a type’s Display implementation be used for its Debug implementation.
This is useful when a type’s Display output is already meaningful (e.g., human-readable identifiers, unit types, etc.), and it is used in debugging contexts or logging frameworks that rely on Debug. Also included is a reverse implementation, allowing a type’s Debug implementation to be used for its Display implementation, should that be required.
§Features
- Borrowing and owning adaptors for using
Displayimplementations asDebug. - Borrowing and owning adaptors for using
Debugimplementations asDebug. - Extension methods on
DisplayandDebug, respectively, to wrap values in these wrappers.
§Example
use display_as_debug::DisplayAsDebug;
struct TestType;
impl std::fmt::Display for TestType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "display")
}
}
let test_type = TestType;
// Use Display as Debug
assert_eq!(format!("{:?}", test_type.display_as_debug()), "display");
// Still uses Display when formatting normally
assert_eq!(format!("{}", test_type.display_as_debug()), "display");
// Or consume the owned value
let display_adaptor = test_type.display_to_debug();
assert_eq!(format!("{:?}", display_adaptor), "display");The inverse adaptor, DebugAsDisplay, has the same API.
§Use Case
This crate is admittedly mostly a convenience wrapper, but it still may be helpful when:
- Log types using their
Displayformat, but are constrained by APIs requiringDebug. - Want to avoid allocating strings (e.g., via
format!orto_string()) just to satisfyDebug.
For example, when formatting a struct field within a Debug implementation. You can use the adaptor to wrap a value and feed it into field or a similar method from a debug builder to avoid needing to call format!, to_string(), or otherwise contorting the code in order to use the Display implementation of a value.
Traits§
- Debug
AsDisplay - A trait to convert a type to a
DebugAsDisplay. - Display
AsDebug - A trait to convert a type to a
DisplayAsDebug.