Skip to main content

FormatArg

Derive Macro FormatArg 

Source
#[derive(FormatArg)]
Expand description

Derives rtformat::FormatArg, adapting to whichever of Display and Debug the type implements — implementing either one is enough.

§Routing

  • {} renders via Display when available, falling back to Debug.
  • {:?} and {:#?} render via Debug (pretty in the # case).
  • Any other format type is rejected with Err(fmt::Error) (surfaced as rtformat::FormatError::UnsupportedFormatType), as is {} on a type implementing neither trait and {:?} on a Display-only type.

The dispatch is resolved statically at compile time through inherent-vs-trait method priority, so there is no runtime cost to the adaptation and a missing trait surfaces as a runtime UnsupportedFormatType error rather than a compile failure.

§Generic types

Every type parameter is additionally bound by Display + Debug (serde-style): trait availability on Self must be known when the derived impl is checked, which only holds if the parameters provide it.

§Examples

use core::fmt;
use rtformat::{rformat, Format, FormatArg};

#[derive(Debug, FormatArg)]
struct Color(u8, u8, u8);

impl fmt::Display for Color {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "#{:02x}{:02x}{:02x}", self.0, self.1, self.2)
    }
}

assert_eq!(rformat!("{}", Color(255, 128, 0)), "#ff8000");
assert_eq!(rformat!("{:?}", Color(255, 128, 0)), "Color(255, 128, 0)");