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
#[derive(delegate_display::DelegateDisplay)]
struct Foo(SomeType);
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
#[derive(delegate_display::DelegateDebug)]
struct Foo { some_field: SomeType }
impl fmt::Debug for Foo {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.some_field, f)
}
}
Enums
enum MyEnum {
Foo,
Bar(SomeType),
Qux { baz: SomeType }
}
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Foo => f.write_str("Foo"),
Self::Bar(inner) => DebugOrDisplay::fmt(inner, f),
Self::Qux { baz } => DebugOrDisplay::fmt(baz, f),
}
}
Empty structs & enums
struct Foo;
struct Bar{}
struct Qux();
enum Baz {}
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 params
bounds(...)
will let you specify specific bounds
#[derive(DelegateDisplay, DelegateDebug)]
#[dboth(base_bounds)]
#[ddisplay(bounds(F: Display, B: Clone + Display))]
enum Foo<F, B> {
Foo(F),
Bar(B),
}
impl<F: Display, B: Clone + Display> Display for Foo<F, B> { }
impl<F: Debug, B: Debug> Debug for Foo<F, B> { }
Invalid inputs
#[derive(delegate_display::DelegateDebug)]
struct TooManyFields1 {
foo: u8,
bar: u8, }
#[derive(delegate_display::DelegateDebug)]
struct TooManyFields2(u8, u8);
#[derive(delegate_display::DelegateDebug)]
enum SomeEnum {
A, B(u8), C { foo: u8 }, D(u8, u8), E { foo: u8, bar: u8 } }