const_format/macros/call_debug_fmt.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
/// For debug formatting of some specific generic std types, and other types.
///
/// # Errors
///
/// This macro propagates errors from the debug formatting methods that
/// it calls, by `return`ing them.
///
/// # Macro variants
///
/// The macro has these variants:
///
/// - `slice` (also `array`): to format a slice or an array of *any debug type.
///
/// - `Option`: to format an `Option` of *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, where `PWrapper<ThatType>`
/// has a `const_debug_fmt` method.<br>
///
/// - `other`: to format non-standard-library types that have a `const_debug_fmt` method.
///
/// *"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.
///
/// ```rust
///
/// use const_format::{
/// for_examples::{Point3, Unit},
/// Error, Formatter, FormattingFlags, StrWriter,
/// call_debug_fmt, 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 = {
/// const PROM: &StrWriter<[u8]> = &unwrap!(make());
/// PROM.as_str_alt()
/// };
///
/// 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,<br>
/// with the `|formatter_ident| expression_that_uses_formatter ` syntax.
///
///
/// ```rust
///
/// 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);
///
/// # Ok::<(), const_format::Error>(())
/// ```
///
#[cfg_attr(feature = "__docsrs", doc(cfg(feature = "fmt")))]
#[macro_export]
macro_rules! call_debug_fmt {
(array, $expr:expr, $formatter:expr $(,)* ) => {{
match (&$expr, $formatter.borrow_mutably()) {
(expr, formatter) => {
let mut n = 0;
let len = expr.len();
let mut f = formatter.debug_list();
while n != len {
$crate::__call_debug_fmt_dispatch!(&expr[n], f.entry());
n += 1;
}
$crate::try_!(f.finish());
}
}
}};
(slice, $expr:expr, $formatter:expr $(,)*) => {
$crate::call_debug_fmt!(array, $expr, $formatter)
};
(Option, $expr:expr, $formatter:expr $(,)*) => {{
match $formatter.borrow_mutably() {
formatter => $crate::try_!(match &$expr {
$crate::pmr::Some(x) => {
let mut f = formatter.debug_tuple("Some");
$crate::__call_debug_fmt_dispatch!(x, f.field());
f.finish()
}
$crate::pmr::None => formatter.write_str("None"),
}),
}
}};
(newtype $name:ident, $expr:expr, $formatter:expr $(,)*) => {
match (&$expr, $formatter.borrow_mutably()) {
(newtype_, formatter) => {
let mut f = formatter.debug_tuple(stringify!($name));
$crate::__call_debug_fmt_dispatch!(&newtype_.0, f.field());
$crate::try_!(f.finish());
}
}
};
(std, $expr:expr, $formatter:expr $(,)*) => {
if let Err(e) = $crate::coerce_to_fmt!(&$expr).const_debug_fmt($formatter) {
return Err(e);
}
};
(other, $expr:expr, $formatter:expr $(,)*) => {
if let Err(e) = $expr.const_debug_fmt($formatter) {
return Err(e);
}
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! __call_debug_fmt_dispatch {
($e:expr, $f:expr) => {
if let Err(e) = $crate::coerce_to_fmt!(&$e).const_debug_fmt($f) {
return Err(e);
}
};
}