[][src]Macro const_format::writec

macro_rules! writec {
    ( $writer:expr, $format_string:expr $( $(, $expr:expr )+ )? $(,)? ) => { ... };
    (@inner (($path:path)) $($everything:tt)*  ) => { ... };
}

Writes some formatted standard library and/or user-defined types into a buffer.

This macro evaluates to a Result<(), const_format::Error> which must be handled.

Syntax

The syntax is similar to that of other formatting macros in this crate:

ẁritec!(
    writer_expression,
    "formatting literal",
    positional_arg_0_expression,
    positional_arg_1_expression,
    named_arg_foo = expression,
    named_arg_bar = expression,
)

The syntax is otherwise the same as described in the const_format::fmt module.

Writers

The first argument must be a type that implements the WriteMarker trait, and has these inherent methods:

This example is not tested
const fn borrow_mutably(&mut self) -> &mut Self
const fn make_formatter(&mut self, flags: FormattingFlags) -> Formatter<'_>

This example below shows how to use this macro with a custom type.

Limitations

Integer arguments must have a type inferrable from context, more details in the Integer arguments section.

Examples

Ẁriting a Display impl.

#![feature(const_mut_refs)]

use const_format::{Error, Formatter, StrWriter};
use const_format::{impl_fmt, try_, writec};

pub struct Foo(u32, &'static str);

impl_fmt!{
    impl Foo;
    pub const fn const_display_fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
        try_!(writec!(f, "{},", self.0));
        try_!(writec!(f, "{:?};", self.1));
        Ok(())
    }
}

// Coerces the `&mut StrWriter<[u8; 128]>` to `&mut StrWriter<[u8]>`.
// This is necessary because the `as_str` method is defined for `StrWriter<[u8]>`.
let writer: &mut StrWriter = &mut StrWriter::new([0; 128]);
writec!(writer, "{}", Foo(100, "bar"))?;

assert_eq!(writer.as_str(), r#"100,"bar";"#);

Writing to a custom type

This example demonstrates how you can use the ẁritec macro with a custom type, in this case it's a buffer that is cleared every time it's written.

#![feature(const_mut_refs)]

use const_format::marker_traits::{IsNotAStrWriter, WriteMarker};
use const_format::{Formatter, FormattingFlags};
use const_format::writec;

const ARRAY_CAP: usize = 20;
struct Array {
    len: usize,
    arr: [u8; ARRAY_CAP],
}

impl WriteMarker for Array{
    type Kind = IsNotAStrWriter;
    type This = Self;
}

impl Array {
    // Gets the part of the array that has been written to.
    pub const fn as_bytes(&self) -> &[u8] {
        const_format::utils::slice_up_to_len_alt(&self.arr, self.len)
    }

    pub const fn borrow_mutably(&mut self) -> &mut Self {
        self
    }

    pub const fn make_formatter(&mut self, flags: FormattingFlags) -> Formatter<'_> {
        Formatter::from_custom_cleared(&mut self.arr, &mut self.len, flags)
    }
}


let mut buffer = Array{ arr: [0; ARRAY_CAP], len: 0 };

writec!(buffer, "{:?}", [3u8, 5, 8, 13, 21])?;
assert_eq!(buffer.as_bytes(), b"[3, 5, 8, 13, 21]");

writec!(buffer, "{}{}", "Hello, world!", 100u16)?;
assert_eq!(buffer.as_bytes(), b"Hello, world!100");