display_as_debug
A lightweight utility crate with wrappers that let you swap Display and Debug implementations around.
This crate provides adaptors for using a type's Display implementation as its Debug implementation (and vice versa). It's useful when you need human-readable output in debug contexts, want to return friendly error messages from main(), or need to work with types that only implement one trait but you need the other.
Also included are specialized wrappers for Option and Result types that provide a implement Debug for these types when the underlying type may not, or when you wish to hide the underlying type's debug implementation.
This crate is no_std compatible and contains no unsafe code.
Features
- Borrowing and owning adaptors for swapping
Display↔Debugimplementations - Extension traits (
DisplayDebug,DebugDisplay) providing convenient.as_debug()and.as_display()methods - Specialized wrappers for
Option<T>andResult<T, E>that work without requiringT: Debug
Installation
It's on crates.io.
Examples
Basic Usage
use DisplayDebug;
;
let id = UserId;
// Use Display implementation for Debug output
assert_eq!;
// Still uses Display when formatting normally
assert_eq!;
Returning Friendly Errors from main()
When main() returns a Result<(), E>, Rust prints errors using their Debug implementation, which can be verbose and technical. Use DisplayDebugged to show user-friendly error messages instead:
use ;
use fmt;
Instead of seeing AppError { message: "database connection failed", code: 500 }, users see the clean message: Error 500: database connection failed.
See examples/error_from_main.rs for a complete working example.
Debug Implementations with Display Fields
Use the borrowed wrapper in your Debug implementations to incorporate Display-formatted fields without allocating strings:
use DisplayDebug;
;
let id = UserId;
assert_eq!;
Using Debug as Display
The inverse wrappers (DebugDisplay, AsDebugDisplayed, DebugDisplayed) let you use Debug implementations where Display is needed:
use DebugDisplay;
let numbers = vec!;
let formatted = format!;
assert_eq!;
Option and Result Wrappers
For situations where you need to debug Option or Result types but don't want to expose sensitive data or the inner type doesn't implement Debug:
Type Name Wrappers
Show the type name instead of the actual value:
use OptionDebugExt;
use ResultDebugExt;
let opt = Some;
assert_eq!;
let res: = Ok;
assert_eq!;
Opaque Wrappers
Hide the value completely while preserving the variant information:
use OptionDebugExt;
use ResultDebugExt;
let opt = Some;
assert_eq!;
let res: = Ok;
assert_eq!;
// Errors are still shown for debugging
let err: = Err;
assert_eq!;
Alternative: You can also construct the wrapper types directly if you prefer:
use OptionTypeDebug;
use OpaqueResultDebug;
let opt = Some;
assert_eq!;
let res: = Ok;
assert_eq!;
These are especially useful for logging and debugging where you want to know the state of an Option or Result without exposing potentially sensitive data.