custom_print/macros/dbg.rs
1/// Defines `dbg` macro that prints and returns the value
2/// of a given expression for quick and dirty debugging.
3///
4/// The implementation of the generated `dbg`-like macro
5/// is based on [`std::dbg`] macro implementation,
6/// but the exact output printed by [`std::dbg`]
7/// should not be relied upon and is subject to future changes.
8///
9/// The first argument specifies the generated macro name.
10/// The writer itself is specified by the rest arguments with the [`define_writer`] macros.
11///
12/// Use [`define_try_dbg`] if you need to define a fallible dbg macros.
13///
14/// # Examples
15///
16#[cfg_attr(feature = "alloc", doc = "```rust")]
17#[cfg_attr(not(feature = "alloc"), doc = "```rust,compile_fail")]
18/// let mut string = String::new();
19/// custom_print::define_dbg!(cdbg, concat, |value: &str| string += value);
20///
21/// assert_eq!(cdbg!("value"), "value");
22/// assert!(string.contains("\"value\""));
23/// ```
24///
25/// [`define_writer`]: macro.define_writer.html
26/// [`define_try_dbg`]: macro.define_try_dbg.html
27#[macro_export]
28macro_rules! define_dbg {
29 ( $( #[$meta:meta] )* $name:ident, $($args:tt)* ) => {
30 $crate::define_dbglike!(
31 $( #[$meta] )*
32 $name,
33 ::core::writeln,
34 expect,
35 ":#?",
36 $($args)*
37 );
38 };
39}
40
41/// Defines `try_dbg` macro that prints and returns the value
42/// of a given expression wrapped in for quick and dirty debugging
43/// or return an error if write failed.
44///
45/// The implementation of the generated `dbg`-like macro
46/// is based on [`std::dbg`] macro implementation,
47/// but the exact output printed by [`std::dbg`]
48/// should not be relied upon and is subject to future changes.
49///
50/// The first argument specifies the generated macro name.
51/// The writer itself is specified by the rest arguments with the [`define_writer`] macros.
52///
53/// Use [`define_dbg`] if you need to define a non-fallible dbg macros.
54///
55/// # Examples
56///
57#[cfg_attr(feature = "alloc", doc = "```rust")]
58#[cfg_attr(not(feature = "alloc"), doc = "```rust,compile_fail")]
59/// let mut string = String::new();
60/// custom_print::define_try_dbg!(try_dbg, concat, |value: &str| string += value);
61///
62/// assert_eq!(try_dbg!("value"), Ok("value"));
63/// assert!(string.contains("\"value\""));
64/// ```
65///
66/// [`define_writer`]: macro.define_writer.html
67/// [`define_dbg`]: macro.define_dbg.html
68#[macro_export]
69macro_rules! define_try_dbg {
70 ( $( #[$meta:meta] )* $name:ident, $($args:tt)* ) => {
71 $crate::define_dbglike!(
72 $( #[$meta] )*
73 $name,
74 ::core::writeln,
75 try,
76 ":#?",
77 $($args)*
78 );
79 };
80}