macro_rules! coerce_to_fmt {
    ($reference:expr) => { ... };
}
Available on crate feature fmt only.
Expand description

Coerces a reference to a type that has a const_*_fmt method.

Behavior

For arrays it coerces them into a slice, and wraps them in a PWrapper.

For std types, it wraps them in a PWrapper, which implements the const_*_fmt methods.

For non-std types, it just returns back the same reference.

Example

#![feature(const_mut_refs)]

use const_format::{
    for_examples::Unit,
    Formatter, FormattingFlags, PWrapper, StrWriter,
    coerce_to_fmt,
};

const CAP: usize = 128;
const fn make_strwriter() -> StrWriter<[u8; CAP]> {
    let mut writer = StrWriter::new([0; CAP]);
    let mut fmt = Formatter::from_sw(&mut writer, FormattingFlags::NEW);

    // This is equivalent to the `PWrapper::slice(&[0u8, 1])` below
    let _ = coerce_to_fmt!([0u8, 1]).const_debug_fmt(&mut fmt);
    let _ = fmt.write_str(",");

    let _ = PWrapper::slice(&[0u8, 1]).const_debug_fmt(&mut fmt);
    let _ = fmt.write_str(",");


    // This is equivalent to the `PWrapper(100u32)` line
    let _ = coerce_to_fmt!(100u32).const_debug_fmt(&mut fmt);
    let _ = fmt.write_str(",");

    let _ = PWrapper(100u32).const_debug_fmt(&mut fmt);
    let _ = fmt.write_str(",");


    // This is equivalent to the `Unit.const_debug_fmt(&mut fmt)` line
    let _ = coerce_to_fmt!(Unit).const_debug_fmt(&mut fmt);


    let _ = fmt.write_str(",");
    let _ = Unit.const_debug_fmt(&mut fmt);

    writer
}

const TEXT: &str = {
    const TEXT_: &StrWriter = &make_strwriter();
    TEXT_.as_str_alt()
};

assert_eq!(TEXT, "[0, 1],[0, 1],100,100,Unit,Unit");