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
use std::io::{Result, Write};
pub struct TeeWriter<L, R> {
write: L,
output: R,
}
impl<L, R> TeeWriter<L, R> {
pub fn new(write: L, output: R) -> Self {
Self { write, output }
}
pub fn into_inner(self) -> (L, R) {
(self.write, self.output)
}
pub fn borrow_output(&self) -> &R {
&self.output
}
pub fn borrow_output_mut(&mut self) -> &mut R {
&mut self.output
}
}
impl<L: Write, R: Write> Write for TeeWriter<L, R> {
fn write(&mut self, buf: &[u8]) -> Result<usize> {
let n = self.write.write(buf)?;
let _ = self.output.write(&buf[..n])?;
Ok(n)
}
fn flush(&mut self) -> Result<()> {
self.write.flush()
}
}