6 releases (3 breaking)

0.6.1 Jan 22, 2024
0.6.0 Jan 21, 2024
0.5.1 Mar 11, 2023
0.5.0 Jun 12, 2020
0.3.0 Feb 1, 2020

#20 in Value formatting

Download history 11933/week @ 2024-07-31 13251/week @ 2024-08-07 10170/week @ 2024-08-14 9995/week @ 2024-08-21 11171/week @ 2024-08-28 11474/week @ 2024-09-04 10708/week @ 2024-09-11 11462/week @ 2024-09-18 13264/week @ 2024-09-25 14193/week @ 2024-10-02 14481/week @ 2024-10-09 18822/week @ 2024-10-16 18337/week @ 2024-10-23 17710/week @ 2024-10-30 19561/week @ 2024-11-06 11935/week @ 2024-11-13

71,264 downloads per month
Used in 48 crates (18 directly)

Apache-2.0 OR MIT

7KB
82 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–1.5MB
~32K SLoC