custom-print 1.0.0

Define custom println and dbg macros in wasm and other targets
Documentation
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/// 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
/// [`define_macro`]: macro.define_macro.html
/// [`define_writer`]: macro.define_writer.html
/// [`define_print`]: macro.define_print.html
/// [`define_println`]: macro.define_println.html
/// [`define_dbg`]: macro.define_dbg.html
/// [`define_flush`]: macro.define_flush.html
/// [`define_try_print`]: macro.define_try_print.html
/// [`define_try_println`]: macro.define_try_println.html
/// [`define_try_dbg`]: macro.define_try_dbg.html
/// [`define_try_flush`]: macro.define_try_flush.html
#[macro_export]
macro_rules! define_macros {
    (
        $( #[$meta1:meta] )*
        { $( $( #[$meta2:meta] )* $template:ident $(as $name:ident)? ),* $(,)? },
        $( $args:tt )*
    ) => {
        $crate::_define_macros_impl!(
            $( #[$meta1] )*
            { $( $( #[$meta2] )* $template $( as $name )? ),* },
            $( $args )*
        );
    };
}

#[doc(hidden)]
#[macro_export]
macro_rules! _define_macros_impl {
    (
        $( #[$meta1:meta] )*
        { $( #[$meta2:meta] )* $template:ident $(as $name:ident)? $(, $($rest:tt)* )? },
        $( $args:tt )*
    ) => {
        $crate::define_macro!(
            $( #[$meta1] )*
            $( #[$meta2] )*
            $template $(as $name)?, $($args)*
        );
        $crate::_define_macros_impl!(
            $( #[$meta1] )*
            { $($($rest)*)? }, $($args)*
        );
    };
    ( $( #[$meta1:meta] )* { $(,)? } $(, $( $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
/// [`define_writer`]: macro.define_writer.html
/// [`define_print`]: macro.define_print.html
/// [`define_println`]: macro.define_println.html
/// [`define_dbg`]: macro.define_dbg.html
/// [`define_flush`]: macro.define_flush.html
/// [`define_try_print`]: macro.define_try_print.html
/// [`define_try_println`]: macro.define_try_println.html
/// [`define_try_dbg`]: macro.define_try_dbg.html
/// [`define_try_flush`]: macro.define_try_flush.html
#[macro_export]
macro_rules! define_macro {
    ( $( #[$meta:meta] )* print       as $name:ident, $( $args:tt )* ) => {
        $crate::define_print!      ( $( #[$meta] )* $name, $( $args )* )
    };
    ( $( #[$meta:meta] )* println     as $name:ident, $( $args:tt )* ) => {
        $crate::define_println!    ( $( #[$meta] )* $name, $( $args )* )
    };
    ( $( #[$meta:meta] )* dbg         as $name:ident, $( $args:tt )* ) => {
        $crate::define_dbg!        ( $( #[$meta] )* $name, $( $args )* )
    };
    ( $( #[$meta:meta] )* flush       as $name:ident, $( $args:tt )* ) => {
        $crate::define_flush!      ( $( #[$meta] )* $name, $( $args )* )
    };
    ( $( #[$meta:meta] )* try_print   as $name:ident, $( $args:tt )* ) => {
        $crate::define_try_print!  ( $( #[$meta] )* $name, $( $args )* )
    };
    ( $( #[$meta:meta] )* try_println as $name:ident, $( $args:tt )* ) => {
        $crate::define_try_println!( $( #[$meta] )* $name, $( $args )* )
    };
    ( $( #[$meta:meta] )* try_dbg     as $name:ident, $( $args:tt )* ) => {
        $crate::define_try_dbg!    ( $( #[$meta] )* $name, $( $args )* )
    };
    ( $( #[$meta:meta] )* try_flush   as $name:ident, $( $args:tt )* ) => {
        $crate::define_try_flush!  ( $( #[$meta] )* $name, $( $args )* )
    };

    ( $( #[$meta:meta] )* print,        $( $args:tt )* ) => {
        $crate::define_print!  ( $( #[$meta] )* print,        $( $args )* );
    };
    ( $( #[$meta:meta] )* eprint,       $( $args:tt )* ) => {
        $crate::define_print!  ( $( #[$meta] )* eprint,       $( $args )* );
    };
    ( $( #[$meta:meta] )* cprint,       $( $args:tt )* ) => {
        $crate::define_print!  ( $( #[$meta] )* cprint,       $( $args )* );
    };
    ( $( #[$meta:meta] )* ceprint,      $( $args:tt )* ) => {
        $crate::define_print!  ( $( #[$meta] )* ceprint,      $( $args )* );
    };
    ( $( #[$meta:meta] )* println,      $( $args:tt )* ) => {
        $crate::define_println!( $( #[$meta] )* println,      $( $args )* );
    };
    ( $( #[$meta:meta] )* eprintln,     $( $args:tt )* ) => {
         $crate::define_println!( $( #[$meta] )* eprintln,     $( $args )* );
    };
    ( $( #[$meta:meta] )* cprintln,     $( $args:tt )* ) => {
        $crate::define_println!( $( #[$meta] )* cprintln,     $( $args )* );
    };
    ( $( #[$meta:meta] )* ceprintln,    $( $args:tt )* ) => {
        $crate::define_println!( $( #[$meta] )* ceprintln,    $( $args )* );
    };
    ( $( #[$meta:meta] )* dbg,          $( $args:tt )* ) => {
        $crate::define_dbg!    ( $( #[$meta] )* dbg,          $( $args )* );
    };
    ( $( #[$meta:meta] )* edbg,         $( $args:tt )* ) => {
        $crate::define_dbg!    ( $( #[$meta] )* edbg,         $( $args )* );
    };
    ( $( #[$meta:meta] )* cdbg,         $( $args:tt )* ) => {
        $crate::define_dbg!    ( $( #[$meta] )* cdbg,         $( $args )* );
    };
    ( $( #[$meta:meta] )* flush,        $( $args:tt )* ) => {
        $crate::define_flush!  ( $( #[$meta] )* flush,        $( $args )* );
    };
    ( $( #[$meta:meta] )* eflush,       $( $args:tt )* ) => {
        $crate::define_flush!  ( $( #[$meta] )* eflush,       $( $args )* );
    };

    ( $( #[$meta:meta] )* try_print,    $( $args:tt )* ) => {
        $crate::define_try_print!  ( $( #[$meta] )* try_print,    $( $args )* );
    };
    ( $( #[$meta:meta] )* try_eprint,   $( $args:tt )* ) => {
        $crate::define_try_print!  ( $( #[$meta] )* try_eprint,   $( $args )* );
    };
    ( $( #[$meta:meta] )* try_println,  $( $args:tt )* ) => {
        $crate::define_try_println!( $( #[$meta] )* try_println,  $( $args )* );
    };
    ( $( #[$meta:meta] )* try_eprintln, $( $args:tt )* ) => {
        $crate::define_try_println!( $( #[$meta] )* try_eprintln, $( $args )* );
    };
    ( $( #[$meta:meta] )* try_dbg,      $( $args:tt )* ) => {
        $crate::define_try_dbg!    ( $( #[$meta] )* try_dbg,      $( $args )* );
    };
    ( $( #[$meta:meta] )* try_edbg,     $( $args:tt )* ) => {
        $crate::define_try_dbg!    ( $( #[$meta] )* try_edbg,     $( $args )* );
    };
    ( $( #[$meta:meta] )* try_flush,    $( $args:tt )* ) => {
        $crate::define_try_flush!  ( $( #[$meta] )* try_flush,    $( $args )* );
    };
    ( $( #[$meta:meta] )* try_eflush,   $( $args:tt )* ) => {
        $crate::define_try_flush!  ( $( #[$meta] )* try_eflush,   $( $args )* );
    };
}