Trait defmt::Format[][src]

pub trait Format {
    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);
    }
}

Note that write! can only be called once, as it consumes the Formatter.

Required methods

Writes the defmt representation of self to fmt.

Implementations on Foreign Types

Implementors