#string #debugging #logging #no-std

no-std debug-fn

A function adapter that implements Display and Debug

1 stable release

1.0.0 Dec 14, 2024

#904 in Rust patterns

Download history 115/week @ 2024-12-09 21/week @ 2024-12-16 79/week @ 2025-01-06 234/week @ 2025-01-13 653/week @ 2025-01-20

966 downloads per month
Used in 3 crates (2 directly)

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