#display #derive-debug #debugging #enums #derive #macro

macro build delegate-display

derive(Display, Debug) for structs/enums with one member

11 stable releases

2.1.1 Sep 28, 2023
2.1.0 Sep 26, 2023
1.0.8 Apr 3, 2023
1.0.7 Mar 4, 2023
1.0.1 Feb 28, 2023

#403 in Build Utils

Download history 778/week @ 2023-12-15 407/week @ 2023-12-22 635/week @ 2023-12-29 1112/week @ 2024-01-05 788/week @ 2024-01-12 1633/week @ 2024-01-19 1271/week @ 2024-01-26 1605/week @ 2024-02-02 1242/week @ 2024-02-09 1239/week @ 2024-02-16 1179/week @ 2024-02-23 1675/week @ 2024-03-01 2174/week @ 2024-03-08 2030/week @ 2024-03-15 2129/week @ 2024-03-22 1641/week @ 2024-03-29

8,289 downloads per month
Used in 17 crates (2 directly)

MIT license

27KB
350 lines

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
// 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
// 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.some_field, f)
  }
}
Enums
// Input
enum MyEnum {
  Foo,
  Bar(SomeType),
  Qux { baz: SomeType }
}

// Output
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
// 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 params
  • bounds(...) will let you specify specific bounds
// Input
#[derive(DelegateDisplay, DelegateDebug)]
#[dboth(base_bounds)]
#[ddisplay(bounds(F: Display, B: Clone + Display))]
enum Foo<F, B> {
  Foo(F),
  Bar(B),
}

// Output
impl<F: Display, B: Clone + Display> Display for Foo<F, B> { /* ... */}
impl<F: Debug, B: Debug> Debug for Foo<F, B> { /* ... */ }
Typed delegations

Can be useful for further prettifying the output.

/// Some type that `Deref`s 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 `Deref`s instead
struct Typed(Wrapper);

#[derive(DelegateDebug)] // Included for comparison
struct Base(Wrapper);

assert_eq!(format!("{:?}", Typed(Wrapper("foo"))), "\"foo\"");
assert_eq!(format!("{:?}", Base(Wrapper("bar"))), "Wrapper(\"bar\")");
Invalid inputs
#[derive(DelegateDisplay, Debug)]
#[dboth(delegate_to(String))] // `delegate_to` is not supported on enums
enum SomeEnum {
  Foo(Arc<String>)
}
#[derive(delegate_display::DelegateDisplay)]
#[ddisplay(base_bounds, bounds(T: Display))] // `base_bounds` and `bounds` are mutually exclusive
struct Generic<T>(T);
#[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>(T);
#[derive(delegate_display::DelegateDebug)]
struct TooManyFields1 {
  foo: u8,
  bar: u8, // Only one field permitted
}
#[derive(delegate_display::DelegateDebug)]
struct TooManyFields2(u8, u8); // too many fields
#[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
}
#[derive(delegate_display::DelegateDebug)]
union Foo { bar: u8 } // Unions are not supported

Dependencies

~0.4–0.8MB
~19K SLoC