1use std::io::{self, Stdout, Write};
2
3pub struct ReplayWriter<W1: Write, W2: Write> {
4 writer1: W1,
5 writer2: W2,
6}
7
8impl<W1: Write, W2: Write> ReplayWriter<W1, W2> {
9 pub fn new(writer1: W1, writer2: W2) -> Self {
10 ReplayWriter { writer1, writer2 }
11 }
12}
13
14impl<W1: Write> ReplayWriter<W1, Stdout> {
15 pub fn replay_stdout(writer1: W1) -> Self {
16 ReplayWriter {
17 writer1,
18 writer2: io::stdout(),
19 }
20 }
21}
22
23impl<W1: Write, W2: Write> Write for ReplayWriter<W1, W2> {
24 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
25 let n = self.writer1.write(buf)?;
26 self.writer2.write_all(&buf[..n])?;
27 Ok(n)
28 }
29
30 fn flush(&mut self) -> io::Result<()> {
31 self.writer1.flush()?;
32 self.writer2.flush()?;
33 Ok(())
34 }
35}
36
37#[test]
38#[ignore = "just to see result"]
39fn test_replay_writer() -> io::Result<()> {
40 let file = std::fs::File::create("output.txt")?;
41
42 let mut writer: Box<dyn Write> = Box::new(ReplayWriter::replay_stdout(file));
43
44 writeln!(&mut writer, "hello,")?;
45 writeln!(&mut writer, "world!")?;
46
47 Ok(())
48}