custom_print/macros/print.rs
1/// Defines `print` macro that defines a print macro that uses the specified writer.
2///
3/// The first argument specifies the generated macro name.
4/// The writer itself is specified by the rest arguments with the [`define_writer`] macros.
5///
6/// See also [`define_println`] and [`define_try_print`] macros.
7///
8/// # Examples
9///
10/// ```rust
11/// let mut string = String::new();
12/// custom_print::define_print!(cprint, fmt, |value: &str| string += value);
13///
14/// assert_eq!(cprint!("value"), ());
15/// assert_eq!(string, "value");
16/// ```
17///
18/// [`define_writer`]: macro.define_writer.html
19/// [`define_println`]: macro.define_println.html
20/// [`define_try_print`]: macro.define_try_print.html
21#[macro_export]
22macro_rules! define_print {
23 ( $( #[$meta:meta] )* $name:ident, $( $args:tt )* ) => {
24 $crate::define_printlike!(
25 $( #[$meta] )*
26 $name,
27 ::core::write,
28 expect,
29 $($args)*
30 );
31 };
32}
33
34/// Defines `println` macro that defines a println macro that uses the specified writer.
35///
36/// The first argument specifies the generated macro name.
37/// The writer itself is specified by the rest arguments with the [`define_writer`] macros.
38///
39/// See also [`define_print`] and [`define_try_println`] macros.
40///
41/// # Examples
42///
43/// ```rust
44/// let mut string = String::new();
45/// custom_print::define_println!(cprintln, fmt, |value: &str| string += value);
46///
47/// assert_eq!(cprintln!("value"), ());
48/// assert_eq!(string, "value\n");
49/// ```
50///
51/// [`define_writer`]: macro.define_writer.html
52/// [`define_print`]: macro.define_print.html
53/// [`define_try_println`]: macro.define_try_println.html
54#[macro_export]
55macro_rules! define_println {
56 ( $( #[$meta:meta] )* $name:ident, $( $args:tt )* ) => {
57 $crate::define_printlike!(
58 $( #[$meta] )*
59 $name,
60 ::core::writeln,
61 expect,
62 $($args)*
63 );
64 };
65}
66
67/// Defines `try_print` macro that defines a fallible print macro that uses the specified writer.
68///
69/// The first argument specifies the generated macro name.
70/// The writer itself is specified by the rest arguments with the [`define_writer`] macros.
71///
72/// See also [`define_print`] and [`define_try_println`] macros.
73///
74/// # Examples
75///
76/// ```rust
77/// let mut string = String::new();
78/// custom_print::define_try_print!(try_print, fmt, |value: &str| string += value);
79///
80/// assert_eq!(try_print!("value"), Ok(()));
81/// assert_eq!(string, "value");
82/// ```
83///
84/// [`define_writer`]: macro.define_writer.html
85/// [`define_print`]: macro.define_print.html
86/// [`define_try_println`]: macro.define_try_println.html
87#[macro_export]
88macro_rules! define_try_print {
89 ( $( #[$meta:meta] )* $name:ident, $( $args:tt )* ) => {
90 $crate::define_printlike!(
91 $( #[$meta] )*
92 $name,
93 ::core::write, try,
94 $($args)*
95 );
96 };
97}
98
99/// Defines `try_println` macro that defines
100/// a fallible println macro that uses the specified writer.
101///
102/// The first argument specifies the generated macro name.
103/// The writer itself is specified by the rest arguments with the [`define_writer`] macros.
104///
105/// See also [`define_println`] and [`define_try_print`] macros.
106///
107/// # Examples
108///
109/// ```rust
110/// let mut string = String::new();
111/// custom_print::define_try_println!(try_println, fmt, |value: &str| string += value);
112///
113/// assert_eq!(try_println!("value"), Ok(()));
114/// assert_eq!(string, "value\n");
115/// ```
116///
117/// [`define_writer`]: macro.define_writer.html
118/// [`define_println`]: macro.define_println.html
119/// [`define_try_print`]: macro.define_try_print.html
120#[macro_export]
121macro_rules! define_try_println {
122 ( $( #[$meta:meta] )* $name:ident, $( $args:tt )* ) => {
123 $crate::define_printlike!(
124 $( #[$meta] )*
125 $name,
126 ::core::writeln,
127 try,
128 $($args)*
129 );
130 };
131}