Skip to main content

SafeDebug

Derive Macro SafeDebug 

Source
#[derive(SafeDebug)]
{
    // Attributes available to this derive:
    #[safe]
}
Expand description

Derive to help prevent leaking personally identifiable information (PII) that deriving Debug might otherwise.

SafeDebug is not a trait and cannot be implemented, nor should you derive Debug explicitly. Only when you derive SafeDebug will types help prevent leaking PII because, by default, only the type name is printed. Only when you enable the debug feature will it derive Debug normally.

You can attribute types, fields, and variants with #[safe(true)] or #[safe(false)] to optionally show or hide members. The default is that no members are shown. The inner most #[safe(..)] attribute determines whether to show or hide a member.

ยงExamples

#[derive(SafeDebug)]
struct Person {
    name: String,
}

let person = Person {
    name: "Kelly Smith".to_string(),
};
if cfg!(feature = "debug") {
    assert_eq!(format!("{person:?}"), r#"Person { name: "Kelly Smith" }"#);
} else {
    assert_eq!(format!("{person:?}"), "Person { .. }");
}

Using the #[safe(..)] attribute, you can selectively show or hide members. The default, when not present or inherited, is to always hide members unless the debug feature is enabled.

use std::ops::Range;

#[derive(SafeDebug)]
struct Employee {
    name: String,
    #[safe(true)]
    position: Position,
}

#[derive(SafeDebug)]
#[safe(true)]
struct Position {
    id: i32,
    title: String,
    #[safe(false)]
    salary: Range<i32>,
}

let employee = Employee {
    name: "Kelly Smith".to_string(),
    position: Position {
        id: 12,
        title: "Staff Engineer".to_string(),
        salary: 200_000..250_000,
    },
};
if cfg!(feature = "debug") {
    assert_eq!(format!("{employee:?}"), r#"Employee { name: "Kelly Smith", position: Position { id: 12, title: "Staff Engineer", salary: 200000..250000 } }"#);
} else {
    assert_eq!(format!("{employee:?}"), r#"Employee { position: Position { id: 12, title: "Staff Engineer", .. }, .. }"#);
}