Lets you derive Display & Debug traits on structs with 0..=1 fields & enums where each variant has 0..=1 fields - see input/output examples below.

master CI badge crates.io badge docs.rs badge dependencies badge

Examples

Newtype structs

```rust // Input

[derive(delegate_display::DelegateDisplay)]

struct Foo(SomeType);

// Output impl fmt::Display for Foo { #[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Display::fmt(&self.0, f) } } ````

Structs with one field

```rust // Input

[derive(delegate_display::DelegateDebug)]

struct Foo { some_field: SomeType }

// Output impl fmt::Debug for Foo { #[inline] fn fmt(&self, f: &mut fmt::Formatter<'>) -> fmt::Result { fmt::Debug::fmt(&self.somefield, f) } } ````

Enums

```rust // Input enum MyEnum { Foo, Bar(SomeType), Qux { baz: SomeType } }

// Output fn fmt(&self, f: &mut fmt::Formatter<'>) -> fmt::Result { match self { Self::Foo => f.writestr("Foo"), Self::Bar(inner) => DebugOrDisplay::fmt(inner, f), Self::Qux { baz } => DebugOrDisplay::fmt(baz, f), } } ````

Empty structs & enums

```rust // Input struct Foo; struct Bar{} struct Qux(); enum Baz {}

// Output fn fmt(&self, : &mut fmt::Formatter<'>) -> fmt::Result { Ok(()) } ````

Custom generic bounds

The attribute names are ddebug for Debug, ddisplay for Display and dboth for a common config for both. ddebug and ddisplay take precendence over dboth.

```rust // Input

[derive(DelegateDisplay, DelegateDebug)]

[dboth(base_bounds)]

[ddisplay(bounds(F: Display, B: Clone + Display))]

enum Foo { Foo(F), Bar(B), }

// Output impl Display for Foo { /* ... /} impl Debug for Foo { / ... */ } ````

Typed delegations

Can be useful for further prettifying the output.

``rust /// Some type thatDerefs to the type we want to use in our formatting, in this case,str`.

[derive(Debug)]

struct Wrapper(&'static str);

[derive(DelegateDebug)]

[ddebug(delegate_to(str))] // ignore Wrapper and debug the str it Derefs instead

struct Typed(Wrapper);

[derive(DelegateDebug)] // Included for comparison

struct Base(Wrapper);

asserteq!(format!("{:?}", Typed(Wrapper("foo"))), "\"foo\""); asserteq!(format!("{:?}", Base(Wrapper("bar"))), "Wrapper(\"bar\")"); ```

Invalid inputs

```rust

[derive(DelegateDisplay, Debug)]

[dboth(delegate_to(String))] // delegate_to is not supported on enums

enum SomeEnum { Foo(Arc) } ```

```rust

[derive(delegate_display::DelegateDisplay)]

[ddisplay(base_bounds, bounds(T: Display))] // base_bounds and bounds are mutually exclusive

struct Generic(T); ```

```rust

[derive(delegate_display::DelegateDisplay)]

[ddisplay(base_bounds)]

[ddisplay(base_bounds)] // dbodh and ddisplay can be mixed, but the same option can't be used twice

struct Foo(T); ```

```rust

[derive(delegate_display::DelegateDebug)]

struct TooManyFields1 { foo: u8, bar: u8, // Only one field permitted } ```

```rust

[derive(delegate_display::DelegateDebug)]

struct TooManyFields2(u8, u8); // too many fields ```

```rust

[derive(delegate_display::DelegateDebug)]

enum SomeEnum { A, // this is ok B(u8), // this is ok C { foo: u8 }, // this is ok D(u8, u8), // Only one field permitted E { foo: u8, bar: u8 } // Only one field permitted } ```

```rust

[derive(delegate_display::DelegateDebug)]

union Foo { bar: u8 } // Unions are not supported ```