[−][src]Macro const_format::call_debug_fmt
For debug formatting of some specific generic std types, and other types.
Errors
This macro calls the appropriate formatting methods, and returns errors when they happen.
Macro variants
The macro has these variants:
-
slice(alsoarray): to format a slice or an array of *any debug type. -
Option: to format anOptionof *any debug type. -
newtype: to format a single field tuple struct (eg:struct Foo(Bar);) which wraps *any debug type. -
std: to format the standard library types wherePWrapper<ThatType>has aconst_debug_fmtmethod. -
other: to format non-standard-library types that have aconst_debug_fmtmethod.
*"any debug type" meaning types that have a const_debug_fmt method
Example
Printing all of them
Printing all of the kinds of types this supports.
#![feature(const_mut_refs)] use const_format::{ for_examples::{Point3, Unit}, Error, Formatter, FormattingFlags, StrWriter, call_debug_fmt, strwriter_as_str, try_, unwrap, }; use std::num::Wrapping; const CAP: usize = 512; // `call_debug_fmt` implicitly returns on error, // so the function has to return a `Result<_, const_format::Error>` const fn make() -> Result<StrWriter<[u8; CAP]>, Error> { let mut writer = StrWriter::new([0; CAP]); let mut fmt = Formatter::from_sw(&mut writer, FormattingFlags::NEW); let mut fmt = fmt.debug_struct("Foo"); let point = Point3{ x: 5, y: 8, z: 13 }; call_debug_fmt!(array, [Unit, Unit], fmt.field("array") ); call_debug_fmt!(slice, [0u8, 1], fmt.field("slice") ); call_debug_fmt!(Option, Some(point), fmt.field("option") ); call_debug_fmt!(newtype NumWrapping, Wrapping(255u16), fmt.field("newtype") ); call_debug_fmt!(std, false, fmt.field("std_") ); call_debug_fmt!(other, point, fmt.field("other") ); try_!(fmt.finish()); Ok(writer) } const TEXT: &str = { let promoted = &make(); strwriter_as_str!(unwrap!(promoted)) }; const EXPECTED: &str = "\ Foo { \ array: [Unit, Unit], \ slice: [0, 1], \ option: Some(Point3 { x: 5, y: 8, z: 13 }), \ newtype: NumWrapping(255), \ std_: false, \ other: Point3 { x: 5, y: 8, z: 13 } \ }\ "; assert_eq!(TEXT, EXPECTED);
Used as formatc argument
This macro can be used in the formatting macros by using the Formatter in the argument,
with the |formatter_ident| expression_that_uses_formatter syntax.
#![feature(const_mut_refs)] use const_format::{ for_examples::{Point3, Unit}, Error, Formatter, FormattingFlags, StrWriter, call_debug_fmt, formatc, try_, unwrap, }; use std::num::Wrapping; const POINT: Point3 = Point3{ x: 5, y: 8, z: 13 }; const TEXT: &str = formatc!( "a: {},b: {},c: {},d: {},e: {},f: {},", |fmt| call_debug_fmt!(array, [Unit, Unit], fmt ), |fmt| call_debug_fmt!(slice, [0u8, 1], fmt ), |fmt| call_debug_fmt!(Option, Some(POINT), fmt ), |fmt| call_debug_fmt!(newtype NumWrapping, Wrapping(255u16), fmt ), |fmt| call_debug_fmt!(std, false, fmt ), |fmt| call_debug_fmt!(other, POINT, fmt ), ); const EXPECTED: &str = "\ a: [Unit, Unit],\ b: [0, 1],\ c: Some(Point3 { x: 5, y: 8, z: 13 }),\ d: NumWrapping(255),\ e: false,\ f: Point3 { x: 5, y: 8, z: 13 },\ "; assert_eq!(TEXT, EXPECTED);