Crates.io | custom_debug |
lib.rs | custom_debug |
version | 0.6.2 |
source | src |
created_at | 2020-02-01 15:31:44.31252 |
updated_at | 2024-11-26 00:29:41.645688 |
description | Derive Debug with a custom format per field |
homepage | |
repository | https://github.com/panicbit/custom_debug |
max_upload_size | |
id | 203945 |
size | 9,143 |
Derive Debug
with a custom format per field.
Here is a showcase of custom_debug
s 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
}
Attributes within a section below are considered mutually exclusive.
skip |
Unconditionally skips a field. |
skip_if = path::to::function |
Skips a field if path::to::function(&field) returns true . |
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). |