Crate debug_stub_derive [] [src]

This crate provides the DebugStub derive macro.

The DebugStub derive macro can be used as a drop-in replacement for the standard fmt::Debug when certain members of a struct or enum do not or cannot implement the fmt::Debug trait themselves, but the containing struct or enum still wants or needs to implement it.

Examples

Using DebugStub with structs:

#[macro_use]
extern crate debug_stub_derive;

// A struct from an external crate which does not implement the `fmt::Debug`
// trait.
pub struct ExternalCrateStruct;

// A struct in the current crate which wants to cleanly expose
// itself to the outside world with an implementation of `fmt::Debug`.
#[derive(DebugStub)]
pub struct PubStruct {
    a: bool,
    // Define a replacement debug serialization for the external struct.
    #[debug_stub="ReplacementValue"]
    b: ExternalCrateStruct
}

assert_eq!(format!("{:?}", PubStruct {
    a: true,
    b: ExternalCrateStruct

}), "PubStruct { a: true, b: ReplacementValue }");

Using DebugStub with enums:

pub struct ExternalCrateStruct;

#[derive(DebugStub)]
pub enum PubEnum {
    VariantA(
        u64,
        #[debug_stub="ReplacementValue"]
        ExternalCrateStruct
    )
}

assert_eq!(format!("{:?}", PubEnum::VariantA(
    42,
    ExternalCrateStruct

)), "VariantA(42, ReplacementValue)");

Using DebugStub with Option and Result types:

pub struct ExternalCrateStruct;

#[derive(DebugStub)]
pub struct PubStruct {
    #[debug_stub(some="ReplacementSomeValue")]
    a: Option<ExternalCrateStruct>,
    #[debug_stub(ok="ReplacementOkValue")]
    b: Result<ExternalCrateStruct, ()>
}

assert_eq!(format!("{:?}", PubStruct {
    a: Some(ExternalCrateStruct),
    b: Ok(ExternalCrateStruct)

}), "PubStruct { a: Some(ReplacementSomeValue), b: Ok(ReplacementOkValue) }");

Functions

derive_debug_stub

Implementation of the #[derive(DebugStub)] derive macro.