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.
Newtype structs
```rust // Input
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
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
.
base_bounds
will add whatever trait is being derived as a generic bound to each of the struct/enum's generic paramsbounds(...)
will let you specify specific bounds```rust // Input
enum Foo
// Output
impl
Typed delegations
Can be useful for further prettifying the output.
``rust
/// Some type that
Derefs to the type we want to use in our formatting, in this case,
str`.
struct Wrapper(&'static str);
Wrapper
and debug the str
it Deref
s insteadstruct Typed(Wrapper);
struct Base(Wrapper);
asserteq!(format!("{:?}", Typed(Wrapper("foo"))), "\"foo\""); asserteq!(format!("{:?}", Base(Wrapper("bar"))), "Wrapper(\"bar\")"); ```
Invalid inputs
```rust
delegate_to
is not supported on enumsenum SomeEnum {
Foo(Arc
```rust
base_bounds
and bounds
are mutually exclusivestruct Generic
```rust
dbodh
and ddisplay
can be mixed, but the same option can't be used twicestruct Foo
```rust
struct TooManyFields1 { foo: u8, bar: u8, // Only one field permitted } ```
```rust
struct TooManyFields2(u8, u8); // too many fields ```
```rust
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
union Foo { bar: u8 } // Unions are not supported ```