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
/// Defines `flush` macro that calls `flush` method of the specified writer.
///
/// Use [`define_try_flush`] if you need to define a fallible flush macro.
///
/// The macro is intentionally defined instead of a function
/// because of the custom result type specified by the writer.
///
/// # Examples
///
/// ```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_print!(cprint, line_writer);
/// custom_print::define_flush!(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,");
/// ```
///
/// [`define_try_flush`]: macro.define_try_flush.html
/// Defines `try_flush` macro that calls `flush` method of the specified writer.
///
/// Use [`define_flush`] if you need to define a non-fallible flush macros.
///
/// The macro is intentionally defined instead of a function
/// because of the custom result type specified by the writer.
///
/// # Examples
///
/// ```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_try_print!(try_print, line_writer);
/// custom_print::define_try_flush!(try_flush, line_writer);
///
/// assert_eq!(try_print!("first,").ok(), Some(()));
/// assert_eq!(*written.lock().unwrap(), b"");
/// assert_eq!(try_print!("second\nthird,").ok(), Some(()));
/// assert_eq!(*written.lock().unwrap(), b"first,second\n");
/// assert_eq!(try_flush!().ok(), Some(()));
/// assert_eq!(*written.lock().unwrap(), b"first,second\nthird,");
/// ```