1 stable release
new 1.0.0 | Dec 14, 2024 |
---|
#1057 in Rust patterns
115 downloads per month
5KB
debug-fn
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
.
Example
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");
License
This project is licensed under either of
- Apache License, Version 2.0
- MIT License
at your option.
lib.rs
:
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
.
Example
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");