15 releases

0.6.1 Jan 22, 2024
0.5.1 Mar 11, 2023
0.5.0 Jun 12, 2020
0.4.0 Feb 1, 2020
0.1.4 Nov 27, 2018

#859 in Procedural macros

Download history 5507/week @ 2023-12-23 7216/week @ 2023-12-30 10844/week @ 2024-01-06 12242/week @ 2024-01-13 13468/week @ 2024-01-20 13387/week @ 2024-01-27 12704/week @ 2024-02-03 15616/week @ 2024-02-10 17932/week @ 2024-02-17 15128/week @ 2024-02-24 17060/week @ 2024-03-02 18405/week @ 2024-03-09 16388/week @ 2024-03-16 17031/week @ 2024-03-23 18011/week @ 2024-03-30 14836/week @ 2024-04-06

68,401 downloads per month
Used in 57 crates (11 directly)

Apache-2.0 OR MIT

24KB
554 lines

custom_debug

Derive Debug with a custom format per field.

Example usage

Here is a showcase of custom_debugs features:

    use custom_debug::Debug;
    use std::fmt;

    #[derive(Debug)]
    struct Foo {
        #[debug(format = "{} things")]
        x: i32,
        #[debug(skip)]
        y: i32,
        #[debug(with = hex_fmt)]
        z: i32,
        #[debug(skip_if = Option::is_none)]
        label: Option<String>,
    }

    fn hex_fmt<T: fmt::Debug>(n: &T, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "0x{:02X?}", n)
    }

The resulting debug output would look something like this:

Foo {
    x: 42 things,
    z: 0xAB
}

Field attributes reference

Attributes within a section below are considered mutually exclusive.

Skip attributes

skip Unconditionally skips a field.
skip_if = path::to::function Skips a field if path::to::function(&field) returns true.

Format attributes

format = "format string {}" Formats a field using a format string. Must contain a placeholder ({}) with modifiers of your choice.
with = path::to::formatter Formats a field using path::to::formatter. The required signature is fn(&T, &mut std::fmt::Formatter) -> std::fmt::Result where T is a type compatible with the field's type (i.e. the function can be generic and coercions apply).

Dependencies

~1.2–1.6MB
~35K SLoC