Macro chalk_solve::display::items::write_flags

source ·
macro_rules! write_flags {
    ($writer:ident, $val:expr, $struct_name:ident { $($n:ident $(: $extra_arg:tt)?),* }) => { ... };
    (@default $n:ident : $name:literal) => { ... };
    (@default $n:ident ) => { ... };
}
Expand description

Used in AdtDatum and TraitDatum to write n flags from a flags struct to a writer. Each flag field turns into an if expression + write!, so we can just list the names and not repeat this pattern over and over.

This macro will error if unknown flags are specified. This will also error if any flags are missing.

§Usage

write_flags!(f, self.flags, XFlags { red, green })

Turns into

match self.flags {
    XFlags { red, green } => {
        if red {
            write!(f, "#[red]")?;
        }
        if green {
            write!(f, "#[green]")?;
        }
    }
}