| Crates.io | to-display-derive |
| lib.rs | to-display-derive |
| version | 0.1.2 |
| created_at | 2024-12-29 03:23:50.288712+00 |
| updated_at | 2024-12-29 04:20:09.106577+00 |
| description | A trait that is Display or can be converted to Display |
| homepage | https://github.com/drmingdrmer/to-display |
| repository | https://github.com/drmingdrmer/to-display |
| max_upload_size | |
| id | 1497923 |
| size | 15,748 |
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.
use to_display::ToDisplay;
use to_display::DisplayConfig;
// Option
println!("{}", Some(1u64).display()); // 1
println!("{}", Some(1u64).display().verbose()); // Some(1)
println!("{}", None::<u64>.display().verbose()); // None
// Result
println!("{}", Ok::<u64,u32>(1).display()); // Ok(1)
println!("{}", Err::<u64,u32>(2).display()); // Err(2)
// Collection Limits
let vec = vec![1, 2, 3, 4, 5];
println!("{}", vec.display().limit_items(3)); // [1, 2, 3, ...]
// Time Formatting
let time = Instant::now();
println!("{}", time.display()); // 10:10:10.000000
The following types implement [ToDisplay] out of the box:
i8-i128, u8-u128, isize, usizef32, f64bool, char, String, &strIpAddr, Ipv4Addr, Ipv6Addr, SocketAddrNonZeroI8-NonZeroU128std::time::Instant (requires std-time feature)let now = Instant::now();
println!("{}", now.display()); // -> "10:10:10.000000"
tokio::time::Instant (requires tokio-time feature)let now = Instant::now();
println!("{}", now.display().use_full_time()); // -> "2024-12-28T23:37:31.646201Z+0800"
Option<T> where T: ToDisplayResult<T, E> where T: ToDisplay, E: DisplayVec<T> and slices [T] where T: ToDisplayBTreeMap<K, V> where K: ToDisplay, V: ToDisplayFor types that already implement Display:
#[derive(to_display::ToDisplay)]
struct Foo(u64);
impl std::fmt::Display for Foo {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Foo({})", self.0)
}
}
For types requiring custom display logic or types that don't implement Display,
manual implementation of [ToDisplay] is needed. This involves two steps:
ToDisplay] to create a display wrapper type (e.g., DisplayFoo)Display for the wrapper type to define the formatting logicHere's a complete example:
use std::fmt;
use to_display::{Context, ToDisplay, DisplayConfig};
struct Foo(u64);
impl ToDisplay for Foo {
type Displayer<'a> = DisplayFoo<'a>;
fn display_with_context(&self, context: Context) -> Self::Displayer<'_> {
DisplayFoo { foo: self, context }
}
}
struct DisplayFoo<'a> {
foo: &'a Foo,
context: Context,
}
impl fmt::Display for DisplayFoo<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Foo({})", self.foo.0)
}
}
// Optional: Enable display configuration methods
impl<'a> DisplayConfig for DisplayFoo<'a> {
fn context_mut(&mut self) -> &mut Context {
&mut self.context
}
}
assert_eq!(Foo(42).display().to_string(), "Foo(42)");
std-time: Enables support for std::time::Instanttokio-time: Enables support for tokio::time::Instant