macro_rules! define_try_writer {
    ( concat, $($args:tt)* ) => { ... };
    ( fmt, $($args:tt)* ) => { ... };
    ( io, $($args:tt)* ) => { ... };
    ( $expr:expr ) => { ... };
}
Expand description

Defines a fallible writer from writer expression arguments or from the provided one.

If more than one argument is used, the first argument specifies the writer type, and the others are used to define the expression:

If only one argument is used, the macro just returns it as a result.

Use define_writer if you need to define a non-fallible writer. This macro is used by define_printlike, define_dbglike and define_try_flush macros.

Examples

use core::fmt::{self, Write};

let mut expr_string = String::new();
let mut expr_writer = custom_print::define_try_writer!(&mut expr_string);
let mut concat_string = String::new();
let mut concat_writer = custom_print::define_writer!(concat, |value: &str| {
    concat_string += value;
});
let mut fallible_writer = custom_print::define_try_writer!(fmt, |_: &str| Err(fmt::Error));

assert_eq!(writeln!(expr_writer, "first"), Ok(()));
assert_eq!(expr_string, "first\n");
assert_eq!(writeln!(concat_writer, "second"), Ok(()));
assert_eq!(concat_string, "second\n");
assert_eq!(writeln!(fallible_writer, "third"), Err(fmt::Error));