Macro def_usize_addr_formatter

Source
macro_rules! def_usize_addr_formatter {
    (
        $name:ident = $format:literal;

        $($tt:tt)*
    ) => { ... };
    () => { ... };
}
Expand description

Creates implementations for the Debug, LowerHex, and UpperHex traits for the given address types defined by the def_usize_addr.

For each $name = $format;, this macro generates the following items:

  • An implementation of core::fmt::Debug for the address type $name, which formats the address with format_args!($format, format_args!("{:#x}", self.0)),
  • An implementation of core::fmt::LowerHex for the address type $name, which formats the address in the same way as core::fmt::Debug,
  • An implementation of core::fmt::UpperHex for the address type $name, which formats the address with format_args!($format, format_args!("{:#X}", self.0)).

ยงExample

use memory_addr::{PhysAddr, VirtAddr, def_usize_addr, def_usize_addr_formatter};

def_usize_addr! {
    /// An example address type.
    pub type ExampleAddr;
}

def_usize_addr_formatter! {
    ExampleAddr = "EA:{}";
}

assert_eq!(format!("{:?}", PhysAddr::from(0x1abc)), "PA:0x1abc");
assert_eq!(format!("{:x}", VirtAddr::from(0x1abc)), "VA:0x1abc");
assert_eq!(format!("{:X}", ExampleAddr::from(0x1abc)), "EA:0x1ABC");