Expand description

Helper types to omit debug info for select values.

Provides wrapper structs with default Debug impls. This allows you to use the default implementation of Debug for large structs while enabling you to:

  • avoid using Debug impls that traverse deeply nested or overly large structures,
  • avoid using a Debug impl that leaks info that should not be logged.

This can improve:

  • readability (logs can focus on the information you care about),
  • debuggability & security (logs can be more complete without accidentally leaking private info),
  • and performance (complex data structures don’t need to be traversed for debugging unless intentionally requested via Deref).

Example usage: Hiding a user’s password from logs.

use no_debug::NoDebug;

#[derive(Debug)]
struct UserInfo {
  username: String,
  password: NoDebug<String>,
}

let user = UserInfo {
    username: "Cypher1".to_string(),
    password: NoDebug::new("hunter2".to_string())
};

// The password is hidden by default
assert_eq!(
    format!("{:?}", user),
    r#"UserInfo { username: "Cypher1", password: <no debug: alloc::string::String> }"#
);
// Even when accessed
assert_eq!(format!("{:?}", user.password), r#"<no debug: alloc::string::String>"#);
// But is can be extracted easily for operating on the data inside, at which point it is
// visible again.
assert_eq!(format!("{:?}", *user.password), r#""hunter2""#);

Structs

Wraps a type T and provides a Debug impl that does not rely on T being Debug.