Trait CustomFormat

Source
pub trait CustomFormat<const SPEC: u128> {
    // Required method
    fn fmt(&self, f: &mut Formatter<'_>) -> Result;
}
Available on crate feature compile-time only.
Expand description

Trait for custom formatting with compile-time format checking

Required Methods§

Source

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter.

§Examples
use custom_format as cfmt;
use custom_format::compile_time::{spec, CustomFormat};

use core::fmt;

#[derive(Debug)]
struct Hex(u8);

impl CustomFormat<{ spec("x") }> for Hex {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:#02x}", self.0)
    }
}

impl CustomFormat<{ spec("X") }> for Hex {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:#02X}", self.0)
    }
}

assert_eq!(cfmt::format!("{0:X?}, {0 :x}, {0 :X}", Hex(0xAB)), "Hex(AB), 0xab, 0xAB");

The following statement doesn’t compile since "z" is not a valid format specifier:

cfmt::println!("{ :z}", Hex(0));

Implementors§