custom_print/macros/flush.rs
1/// Defines `flush` macro that calls `flush` method of the specified writer.
2///
3/// Use [`define_try_flush`] if you need to define a fallible flush macro.
4///
5/// The macro is intentionally defined instead of a function
6/// because of the custom result type specified by the writer.
7///
8/// # Examples
9///
10/// ```rust
11/// use std::io::{self, LineWriter, Write};
12/// use std::sync::Mutex;
13///
14/// let written: Mutex<Vec<u8>> = Mutex::default();
15///
16/// #[derive(Clone, Debug)]
17/// struct CustomWriter<'a>(&'a Mutex<Vec<u8>>);
18///
19/// impl Write for CustomWriter<'_> {
20/// fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
21/// let mut written = self.0.lock().unwrap();
22/// written.extend_from_slice(buf);
23/// Ok(buf.len())
24/// }
25///
26/// fn flush(&mut self) -> io::Result<()> {
27/// Ok(())
28/// }
29/// }
30///
31/// let custom_writer = CustomWriter(&written);
32/// let mut line_writer = LineWriter::new(custom_writer);
33///
34/// custom_print::define_print!(cprint, line_writer);
35/// custom_print::define_flush!(flush, line_writer);
36///
37/// assert_eq!(cprint!("first,"), ());
38/// assert_eq!(*written.lock().unwrap(), b"");
39/// assert_eq!(cprint!("second\nthird,"), ());
40/// assert_eq!(*written.lock().unwrap(), b"first,second\n");
41/// assert_eq!(flush!(), ());
42/// assert_eq!(*written.lock().unwrap(), b"first,second\nthird,");
43/// ```
44///
45/// [`define_try_flush`]: macro.define_try_flush.html
46#[macro_export]
47macro_rules! define_flush {
48 ( $( #[$meta:meta] )* $name:ident, $($args:tt)* ) => {
49 $( #[$meta] )*
50 #[allow(unused_macros)]
51 macro_rules! $name {
52 () => {
53 $crate::define_writer!($($args)*).flush().expect("failed flushing")
54 };
55 }
56 };
57}
58
59/// Defines `try_flush` macro that calls `flush` method of the specified writer.
60///
61/// Use [`define_flush`] if you need to define a non-fallible flush macros.
62///
63/// The macro is intentionally defined instead of a function
64/// because of the custom result type specified by the writer.
65///
66/// # Examples
67///
68/// ```rust
69/// use std::io::{self, LineWriter, Write};
70/// use std::sync::Mutex;
71///
72/// let written: Mutex<Vec<u8>> = Mutex::default();
73///
74/// #[derive(Clone, Debug)]
75/// struct CustomWriter<'a>(&'a Mutex<Vec<u8>>);
76///
77/// impl Write for CustomWriter<'_> {
78/// fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
79/// let mut written = self.0.lock().unwrap();
80/// written.extend_from_slice(buf);
81/// Ok(buf.len())
82/// }
83///
84/// fn flush(&mut self) -> io::Result<()> {
85/// Ok(())
86/// }
87/// }
88///
89/// let custom_writer = CustomWriter(&written);
90/// let mut line_writer = LineWriter::new(custom_writer);
91///
92/// custom_print::define_try_print!(try_print, line_writer);
93/// custom_print::define_try_flush!(try_flush, line_writer);
94///
95/// assert_eq!(try_print!("first,").ok(), Some(()));
96/// assert_eq!(*written.lock().unwrap(), b"");
97/// assert_eq!(try_print!("second\nthird,").ok(), Some(()));
98/// assert_eq!(*written.lock().unwrap(), b"first,second\n");
99/// assert_eq!(try_flush!().ok(), Some(()));
100/// assert_eq!(*written.lock().unwrap(), b"first,second\nthird,");
101/// ```
102#[macro_export]
103macro_rules! define_try_flush {
104 ( $( #[$meta:meta] )* $name:ident, $($args:tt)* ) => {
105 $( #[$meta] )*
106 #[allow(unused_macros)]
107 macro_rules! $name {
108 () => {
109 $crate::define_try_writer!($($args)*).flush()
110 };
111 }
112 };
113}