display_as_debug
A lightweight utility crate with wrappers that let you swap Display and Debug implementations around.
This crate provides a number of utility types, including 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.
This crate is no_std compatible and contains no unsafe code.
Features
- Formatting Wrappers
wrapmodule provides:DisplayAsDebug,DebugAsDisplayfor swappingDisplay↔Debugimplementations- Specialized wrappers for
Option<T>andResult<T, E>that work without requiringT: Debug - Obscuring
Option/Resultwrappers for obscuring values while preserving variant information
DebugXXXextensionsfmtmodule provides extensions for conveniently formattingstd::fmtDebugXXXdebug helper types- Various Format Types
typesmodule provides types for providing information forDebugandDisplay
Installation
It's on crates.io.
Examples
Basic Usage - Display as Debug Wrappers
use TestValue;
use DisplayAsDebug;
use DebugAsDisplay;
// `TestValue` implements both `Display` and `Debug`, but with different output.
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
// All wrappers implement From, so you can use .into()
let wrapped: = DEFAULT.into;
assert_eq!;
Tip: For ergonomic chaining, the
tapcrate's.pipe()andconvmethods works great:use ; let display_as_debug = value.pipe; let display_as_debug = value.;
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 DisplayAsDebug to show user-friendly error messages instead:
See examples/error_from_main.rs for a complete working example.
Debug extensions
The fmt module contains extension traits for the various std::fmt DebugXXX helper types to extend their functionality.
DebugStructExt
use DebugStructExt;
use ;
use ;
let secret = Secret ;
assert_eq!;
DebugTupleExt has a similar API. Similar, but more limited extension exist for DebugList, DebugSet, and DebugMap
Option and Result Wrappers
The wrap module also provides wrappers for Option and Result types that work without requiring T: Debug.
Type Name Wrappers
Show the type name instead of the actual value:
use TypeNameOption;
use TypeNameResult;
use ;
assert_eq!;
let ok: = Ok;
assert_eq!;
assert_eq!;
let err: = Err;
assert_eq!;
Opaque Wrappers
Hide the value completely while preserving the variant information:
use OpaqueOption;
use OpaqueResult;
let opt = Some;
assert_eq!;
let res: = Ok;
assert_eq!;
// Errors are still shown for debugging
let err: = Err;
assert_eq!;
Debug Formatting Types
The types module provides a set of types that can be used to convey various debug formatting information, such as type names, opaque values, and lists.