#string #debugging #logging #no-std

no-std debug-fn

A function adapter that implements Display and Debug

1 stable release

new 1.0.0 Dec 14, 2024

#1057 in Rust patterns

Download history 115/week @ 2024-12-09

115 downloads per month

MIT/Apache

5KB

debug-fn

crates.io docs.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");

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");

No runtime deps