pub trait Format {
// Required method
fn format(&self, fmt: Formatter<'_>);
}
Expand description
Trait for types that can be formatted via defmt.
This trait is used by the {:?}
format specifier and can format a wide range of types.
User-defined types can #[derive(Format)]
to get an auto-generated implementation of this
trait.
Note: The implementation of #[derive(Format)]
assumes that no builtin types are shadowed
(for example by defining a struct u8;
). This allows it to represent them more compactly.
Example
Usually, an implementation of this trait can be #[derive]
d automatically:
use defmt::Format;
#[derive(Format)]
struct Header {
source: u8,
destination: u8,
sequence: u16,
}
Manual implementations can make use of the write!
macro:
use defmt::{Format, Formatter, write};
struct Id(u32);
impl Format for Id {
fn format(&self, fmt: Formatter) {
// Format as hexadecimal.
write!(fmt, "Id({:x})", self.0);
}
}