Crates.io | debug-fn |
lib.rs | debug-fn |
version | 1.0.0 |
source | src |
created_at | 2024-12-14 09:50:38.166989+00 |
updated_at | 2024-12-14 09:50:38.166989+00 |
description | A function adapter that implements Display and Debug |
homepage | |
repository | https://github.com/mahkoh/debug-fn |
max_upload_size | |
id | 1483024 |
size | 7,132 |
This crate provides an adapter that allows you to turn any closure
Fn(&mut Formatter<'_>) -> fmt::Result
into a type that implements Display
and
Debug
.
use core::fmt;
use core::fmt::Formatter;
use debug_fn::debug_fn;
fn hello(f: &mut Formatter<'_>, user_id: Option<u64>) -> fmt::Result {
if let Some(user_id) = user_id {
write!(f, "user number {}", user_id)
} else {
write!(f, "anonymous user")
}
}
assert_eq!(format!("Hello {}", debug_fn(|f| hello(f, Some(1234)))), "Hello user number 1234");
assert_eq!(format!("Hello {}", debug_fn(|f| hello(f, None))), "Hello anonymous user");
This project is licensed under either of
at your option.