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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
/// Defines multiple `print`-like and `dbg`-like macros. /// /// The first argument braced in curly braces and contains /// comma-seperated macro template names and optionally their custom macro names. /// See the [`define_macro`] declaration for the list of available templates. /// The rest tokens specified in `$($args)*` are used as input for [`define_writer`] macro. /// /// Depending on the specified templates, the macro uses /// [`define_print`], [`define_println`], [`define_dbg`], [`define_flush`], /// [`define_try_print`], [`define_try_println`], [`define_try_dbg`] or [`define_try_flush`] /// for each generated macro. /// /// If you need to define a single `print`-like or `dbg`-like macro, use [`define_macro`]. /// /// # Macro ambiguity /// /// When using std-prelude, std macros cannot be replaced in [textual scope] using this macro. /// This is a consequence of the ambiguous precedence between /// a macro-expanded macro and a macro from the outer scope. /// Use alternative names like `cprint`, `ceprint`, `cprintln`, `ceprintln`, `cdbg`, /// and then override std macros using proxy macro or use declaration. /// /// Overriding with a proxy macro is a better way because /// it overrides macros in [textual scope] and accordingly in all submodules: /// ``` /// custom_print::define_macros!({ cprint, cprintln }, once: crate::write); /// macro_rules! print { ($($args:tt)*) => { cprint!($($args)*); } } /// macro_rules! println { ($($args:tt)*) => { cprintln!($($args)*); } } /// # fn main() {} /// mod submodule { /* println is already defined in all submodules */ } /// ``` /// /// Alternatively, use can rebind macro from the [textual scope] to the [path-based scope], /// but then it will be necessary not to forget to import macro into submodules scope: /// ``` /// custom_print::define_macros!({ cprint, cprintln }, once: crate::write); /// use cprint as print; /// use cprintln as println; /// # fn main() {} /// mod submodule { use crate::{print, println}; /* ... */ } /// ``` /// /// # Examples /// /// An example with a simple string writer: /// ```rust /// use core::fmt::Write; /// let mut string = String::new(); /// custom_print::define_macros!({ cprint, cprintln }, &mut string); /// /// assert_eq!(cprintln!("first"), ()); /// assert_eq!(string, "first\n"); /// assert_eq!(cprint!("second"), ()); /// assert_eq!(string, "first\nsecond"); /// ``` /// /// An example with an extern functions that takes a UTF-8 chars pointer and byte length /// and works in `no_std` context: #[cfg_attr(feature = "alloc", doc = "```rust")] #[cfg_attr(not(feature = "alloc"), doc = "```rust,compile_fail")] /// #![no_std] /// extern crate std; /// /// # pub mod ffi { /// # #[no_mangle] pub extern "C" fn console_log(_: *const u8, _: usize) {} /// # #[no_mangle] pub extern "C" fn console_warn(_: *const u8, _: usize) {} /// # } /// # /// custom_print::define_macros!({ print, println }, /// concat, extern "C" fn console_log(_: *const u8, _: usize)); /// custom_print::define_macros!({ eprint, eprintln, dbg }, /// concat, extern "C" fn console_warn(_: *const u8, _: usize)); /// /// fn main() { /// println!("println"); /// print!("print"); /// eprintln!("eprintln"); /// eprint!("eprint"); /// dbg!("dbg"); /// } /// ``` /// /// An example with a closure that takes an [`str`] reference in `no_std` and `no_alloc`: #[cfg_attr(feature = "std", doc = " ```rust")] #[cfg_attr(not(feature = "std"), doc = " ```rust,compile_fail")] /// #![no_std] /// custom_print::define_macros!({ print, println }, fmt, |_value: &str| { /* ... */ }); /// /// fn main() { /// println!("println"); /// print!("print"); /// } /// ``` /// /// An example with a function that takes a [`c_char`] pointer and overriding /// [`std::print`] and [`std::println`] functions: #[cfg_attr(feature = "std", doc = "```rust")] #[cfg_attr(not(feature = "std"), doc = "```rust,compile_fail")] /// fn write(_value: *const std::os::raw::c_char) { /* ... */ } /// /// custom_print::define_macros!({ cprint, cprintln }, concat, crate::write); /// macro_rules! print { ($($args:tt)*) => { cprint!($($args)*); } } /// macro_rules! println { ($($args:tt)*) => { cprintln!($($args)*); } } /// /// fn main() { /// println!("println"); /// print!("print"); /// } /// ``` /// /// An example with `LineWriter` and flushing. /// ```rust /// use std::io::{self, LineWriter, Write}; /// use std::sync::Mutex; /// /// let written: Mutex<Vec<u8>> = Mutex::default(); /// /// #[derive(Clone, Debug)] /// struct CustomWriter<'a>(&'a Mutex<Vec<u8>>); /// /// impl Write for CustomWriter<'_> { /// fn write(&mut self, buf: &[u8]) -> io::Result<usize> { /// let mut written = self.0.lock().unwrap(); /// written.extend_from_slice(buf); /// Ok(buf.len()) /// } /// /// fn flush(&mut self) -> io::Result<()> { /// Ok(()) /// } /// } /// /// let custom_writer = CustomWriter(&written); /// let mut line_writer = LineWriter::new(custom_writer); /// /// custom_print::define_macros!({cprint, flush}, line_writer); /// /// assert_eq!(cprint!("first,"), ()); /// assert_eq!(*written.lock().unwrap(), b""); /// assert_eq!(cprint!("second\nthird,"), ()); /// assert_eq!(*written.lock().unwrap(), b"first,second\n"); /// assert_eq!(flush!(), ()); /// assert_eq!(*written.lock().unwrap(), b"first,second\nthird,"); /// ``` /// /// [`c_char`]: https://doc.rust-lang.org/std/os/raw/type.c_char.html /// [textual scope]: https://doc.rust-lang.org/nightly/reference/macros-by-example.html#scoping-exporting-and-importing /// [path-based scope]: https://doc.rust-lang.org/nightly/reference/macros-by-example.html#scoping-exporting-and-importing #[macro_export] macro_rules! define_macros { ( { $( $template:ident $(as $name:ident)? ),* $(,)? }, $($args:tt)* ) => { $crate::_define_macros_impl!( { $( $template $(as $name)? ),* }, $($args)* ); }; } #[doc(hidden)] #[macro_export] macro_rules! _define_macros_impl { ( { $template:ident $(as $name:ident)? $(, $($rest:tt)* )? }, $($args:tt)* ) => { $crate::define_macro!( $template $(as $name)?, $($args)* ); $crate::_define_macros_impl!( { $($($rest)*)? }, $($args)* ); }; ( { $(,)? } $(, $($args:tt)* )? ) => {}; } /// Defines custom `print`-like and `dbg`-like macro. /// /// The first argument contains the macro name by which its template is determined, /// or both the template name and the macro name using the syntax `template as name`. /// The rest tokens specified in `$($args)*` are used as input for [`define_writer`] macro. /// /// Depending on the specified template, the macro uses /// [`define_print`], [`define_println`], [`define_dbg`], [`define_flush`], /// [`define_try_print`], [`define_try_println`], [`define_try_dbg`] or [`define_try_flush`]. /// /// If you need to define multiple `print`-like and `dbg`-like macros, use [`define_macros`]. /// /// # Naming /// /// The macros with the `try_` prefix are producing fallible write expressions. /// The macros with the `e` prefix are proposed to be used with `stderr`-like writers. /// The macros with the `c` prefix are proposed to be used /// instead of the standard macros /// or to shadow the standard macros in the following lines of code. /// /// # Macro ambiguity /// /// When using std-prelude, std macros cannot be replaced in [textual scope] using this macro. /// This is a consequence of the ambiguous precedence between /// a macro-expanded macro and a macro from the outer scope. /// Use alternative names like `cprint`, `ceprint`, `cprintln`, `ceprintln`, `cdbg`, /// and then override std macros using proxy macro or use declaration. /// /// Overriding with a proxy macro is a better way because /// it overrides macros in [textual scope] and accordingly in all submodules: /// ``` /// custom_print::define_macro!(cprintln, once: crate::write); /// macro_rules! println { ($($args:tt)*) => { cprintln!($($args)*); } } /// # fn main() {} /// mod submodule { /* println is already defined in all submodules */ } /// ``` /// /// Alternatively, use can override macro in the [path-based scope], /// but then it will be necessary not to forget to import macro into submodules scope: /// ``` /// custom_print::define_macro!(cprintln, once: crate::write); /// use cprintln as println; /// # fn main() {} /// mod submodule { use crate::println; /* ... */ } /// ``` /// /// # Examples /// /// An example with a simple string writer: /// ```rust /// use core::fmt::Write; /// let mut string = String::new(); /// custom_print::define_macro!(cprint, &mut string); /// /// assert_eq!(cprint!("value"), ()); /// assert_eq!(string, "value"); /// ``` /// /// An example with an extern functions that takes a UTF-8 chars pointer and byte length /// and works in `no_std` context: #[cfg_attr(feature = "alloc", doc = "```rust")] #[cfg_attr(not(feature = "alloc"), doc = "```rust,compile_fail")] /// #![no_std] /// extern crate std; /// /// # pub mod ffi { /// # #[no_mangle] pub extern "C" fn console_log(_: *const u8, _: usize) {} /// # #[no_mangle] pub extern "C" fn console_warn(_: *const u8, _: usize) {} /// # } /// # /// custom_print::define_macro!(println, /// concat, extern "C" fn console_log(_: *const u8, _: usize)); /// custom_print::define_macro!(eprintln, /// concat, extern "C" fn console_warn(_: *const u8, _: usize)); /// /// fn main() { /// println!("println"); /// eprintln!("eprintln"); /// } /// ``` /// /// An example with a closure that takes an [`str`] reference in `no_std` and `no_alloc`: #[cfg_attr(feature = "std", doc = " ```rust")] #[cfg_attr(not(feature = "std"), doc = " ```rust,compile_fail")] /// #![no_std] /// custom_print::define_macro!(println, fmt, |_value: &str| { /* ... */ }); /// /// fn main() { /// println!("println"); /// } /// ``` /// /// An example with a function that takes a [`c_char`] pointer and overriding /// [`std::println`] function: #[cfg_attr(feature = "std", doc = "```rust")] #[cfg_attr(not(feature = "std"), doc = "```rust,compile_fail")] /// fn write(_value: *const std::os::raw::c_char) { /* ... */ } /// /// custom_print::define_macro!(cprintln, concat, crate::write); /// macro_rules! println { ($($args:tt)*) => { cprintln!($($args)*); } } /// /// fn main() { /// println!("println"); /// } /// ``` /// /// An example with `LineWriter` and flushing. /// ```rust /// use std::io::{self, LineWriter, Write}; /// use std::sync::Mutex; /// /// let written: Mutex<Vec<u8>> = Mutex::default(); /// /// #[derive(Clone, Debug)] /// struct CustomWriter<'a>(&'a Mutex<Vec<u8>>); /// /// impl Write for CustomWriter<'_> { /// fn write(&mut self, buf: &[u8]) -> io::Result<usize> { /// let mut written = self.0.lock().unwrap(); /// written.extend_from_slice(buf); /// Ok(buf.len()) /// } /// /// fn flush(&mut self) -> io::Result<()> { /// Ok(()) /// } /// } /// /// let custom_writer = CustomWriter(&written); /// let mut line_writer = LineWriter::new(custom_writer); /// /// custom_print::define_macro!(cprint, line_writer); /// custom_print::define_macro!(flush, line_writer); /// /// assert_eq!(cprint!("first,"), ()); /// assert_eq!(*written.lock().unwrap(), b""); /// assert_eq!(cprint!("second\nthird,"), ()); /// assert_eq!(*written.lock().unwrap(), b"first,second\n"); /// assert_eq!(flush!(), ()); /// assert_eq!(*written.lock().unwrap(), b"first,second\nthird,"); /// ``` /// /// [`c_char`]: https://doc.rust-lang.org/std/os/raw/type.c_char.html /// [textual scope]: https://doc.rust-lang.org/nightly/reference/macros-by-example.html#scoping-exporting-and-importing /// [path-based scope]: https://doc.rust-lang.org/nightly/reference/macros-by-example.html#scoping-exporting-and-importing #[macro_export] macro_rules! define_macro { ( print as $name:ident, $($args:tt)* ) => {$crate::define_print! ($name,$($args)*);}; ( println as $name:ident, $($args:tt)* ) => {$crate::define_println!($name,$($args)*);}; ( dbg as $name:ident, $($args:tt)* ) => {$crate::define_dbg! ($name,$($args)*);}; ( flush as $name:ident, $($args:tt)* ) => {$crate::define_flush! ($name,$($args)*);}; ( try_print as $name:ident, $($args:tt)* ) => {$crate::define_try_print! ($name,$($args)*);}; ( try_println as $name:ident, $($args:tt)* ) => {$crate::define_try_println!($name,$($args)*);}; ( try_dbg as $name:ident, $($args:tt)* ) => {$crate::define_try_dbg! ($name,$($args)*);}; ( try_flush as $name:ident, $($args:tt)* ) => {$crate::define_try_flush! ($name,$($args)*);}; ( print, $($args:tt)* ) => { $crate::define_print! ( print, $($args)* ); }; ( eprint, $($args:tt)* ) => { $crate::define_print! ( eprint, $($args)* ); }; ( cprint, $($args:tt)* ) => { $crate::define_print! ( cprint, $($args)* ); }; ( ceprint, $($args:tt)* ) => { $crate::define_print! ( ceprint, $($args)* ); }; ( println, $($args:tt)* ) => { $crate::define_println!( println, $($args)* ); }; ( eprintln, $($args:tt)* ) => { $crate::define_println!( eprintln, $($args)* ); }; ( cprintln, $($args:tt)* ) => { $crate::define_println!( cprintln, $($args)* ); }; ( ceprintln, $($args:tt)* ) => { $crate::define_println!( ceprintln, $($args)* ); }; ( dbg, $($args:tt)* ) => { $crate::define_dbg! ( dbg, $($args)* ); }; ( cdbg, $($args:tt)* ) => { $crate::define_dbg! ( cdbg, $($args)* ); }; ( edbg, $($args:tt)* ) => { $crate::define_dbg! ( edbg, $($args)* ); }; ( flush, $($args:tt)* ) => { $crate::define_flush! ( flush, $($args)* ); }; ( eflush, $($args:tt)* ) => { $crate::define_flush! ( eflush, $($args)* ); }; ( try_print, $($args:tt)* ) => { $crate::define_try_print! ( try_print, $($args)* ); }; ( try_eprint, $($args:tt)* ) => { $crate::define_try_print! ( try_eprint, $($args)* ); }; ( try_println, $($args:tt)* ) => { $crate::define_try_println!( try_println, $($args)* ); }; ( try_eprintln, $($args:tt)* ) => { $crate::define_try_println!( try_eprintln, $($args)* ); }; ( try_dbg, $($args:tt)* ) => { $crate::define_try_dbg! ( try_dbg, $($args)* ); }; ( try_edbg, $($args:tt)* ) => { $crate::define_try_dbg! ( try_edbg, $($args)* ); }; ( try_flush, $($args:tt)* ) => { $crate::define_try_flush! ( try_flush, $($args)* ); }; ( try_eflush, $($args:tt)* ) => { $crate::define_try_flush! ( try_eflush, $($args)* ); }; }