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.

Examples
#[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)
}
}
#[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)
}
}
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),
}
}
struct Foo;
struct Bar{}
struct Qux();
enum Baz {}
fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result {
Ok(())
}
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> { }
Can be useful for further prettifying the output.
#[derive(Debug)]
struct Wrapper(&'static str);
#[derive(DelegateDebug)]
#[ddebug(delegate_to(str))] struct Typed(Wrapper);
#[derive(DelegateDebug)] struct Base(Wrapper);
assert_eq!(format!("{:?}", Typed(Wrapper("foo"))), "\"foo\"");
assert_eq!(format!("{:?}", Base(Wrapper("bar"))), "Wrapper(\"bar\")");
#[derive(DelegateDisplay, Debug)]
#[dboth(delegate_to(String))] enum SomeEnum {
Foo(Arc<String>)
}
#[derive(delegate_display::DelegateDisplay)]
#[ddisplay(base_bounds, bounds(T: Display))] struct Generic<T>(T);
#[derive(delegate_display::DelegateDisplay)]
#[ddisplay(base_bounds)]
#[ddisplay(base_bounds)] struct Foo<T>(T);
#[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 } }
#[derive(delegate_display::DelegateDebug)]
union Foo { bar: u8 }