1use std::sync::{Arc, Mutex, MutexGuard};
2
3#[derive(Clone)]
4pub struct ArcWriter<W: std::io::Write> {
5 logs: Arc<Mutex<W>>,
6}
7
8impl<W: std::io::Write> ArcWriter<W> {
9 pub fn new(w: W) -> Self {
10 Self {
11 logs: Arc::new(Mutex::new(w)),
12 }
13 }
14
15 pub fn lock(&self) -> MutexGuard<'_, W> {
16 self.logs.lock().unwrap()
17 }
18}
19
20impl<W: std::io::Write> std::io::Write for ArcWriter<W> {
21 fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
22 self.logs.lock().unwrap().write(buf)
23 }
24
25 fn flush(&mut self) -> std::io::Result<()> {
26 self.logs.lock().unwrap().flush()
27 }
28}
29
30impl<W: std::io::Write> std::io::Write for &ArcWriter<W> {
31 fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
32 self.logs.lock().unwrap().write(buf)
33 }
34
35 fn flush(&mut self) -> std::io::Result<()> {
36 self.logs.lock().unwrap().flush()
37 }
38}