ToDisplay
A zero-allocation library for formatting non-displayable types like Option<T>.
This crate provides the [ToDisplay] trait which enables customizable string formatting
for types that may not implement std::fmt::Display directly.
Quick Example
use ToDisplay;
use DisplayConfig;
// Option
println!; // 1
println!; // Some(1)
println!; // None
// Result
println!; // Ok(1)
println!; // Err(2)
// Collection Limits
let vec = vec!;
println!; // [1, 2, 3, ...]
// Time Formatting
let time = now;
println!; // 10:10:10.000000
Features
- Zero-allocation string formatting
- Configurable display options (verbose mode, time formats, etc.)
- Extensive type support including generics and collections
- Optional time formatting support via feature flags
Supported Types
Primitive Types
The following types implement [ToDisplay] out of the box:
- Integers:
i8-i128,u8-u128,isize,usize - Floating point:
f32,f64 - Other primitives:
bool,char,String,&str - Network types:
IpAddr,Ipv4Addr,Ipv6Addr,SocketAddr - Non-zero integers:
NonZeroI8-NonZeroU128
Time Types
std::time::Instant(requiresstd-timefeature)
let now = now;
println!; // -> "10:10:10.000000"
tokio::time::Instant(requirestokio-timefeature)
let now = now;
println!; // -> "2024-12-28T23:37:31.646201Z+0800"
Generic Types
Option<T>whereT: ToDisplayResult<T, E>whereT: ToDisplay, E: DisplayVec<T>and slices[T]whereT: ToDisplayBTreeMap<K, V>whereK: ToDisplay, V: ToDisplay
Implementation Methods
Using Derive Macro
For types that already implement Display:
;
Manual Implementation
For types requiring custom display logic or types that don't implement Display,
manual implementation of [ToDisplay] is needed. This involves two steps:
- Implement [
ToDisplay] to create a display wrapper type (e.g.,DisplayFoo) - Implement
Displayfor the wrapper type to define the formatting logic
Here's a complete example:
use fmt;
use ;
;
// Optional: Enable display configuration methods
assert_eq!;
Feature Flags
std-time: Enables support forstd::time::Instanttokio-time: Enables support fortokio::time::Instant