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

use std::sync::MutexGuard;
use std::io::Write;
use std::io;
use std::fmt;

#[derive(Debug)]
pub struct GuardWrite<'a, T: 'a +  Write>(MutexGuard<'a, T>);

impl<'a, T: Write> GuardWrite<'a, T> {
     #[inline]
     pub fn guard(t: MutexGuard<'a, T>) -> Self {
          GuardWrite(t)
     }
}

impl<'a, T: Write> From<MutexGuard<'a, T>> for GuardWrite<'a, T> {
	#[inline(always)]
	fn from(a: MutexGuard<'a, T>) -> Self {
		Self::guard(a)
	}
}


impl<'a, T: Write> Write for GuardWrite<'a, T> {
     #[inline(always)]
     fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
          self.0.write(buf)
     }

     #[inline(always)]
     fn flush(&mut self) -> io::Result<()> {
          self.0.flush()
     }

     #[inline(always)]
     fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
          self.0.write_all(buf)
     }

     #[inline(always)]
     fn write_fmt(&mut self, fmt: fmt::Arguments) -> io::Result<()> { 
          self.0.write_fmt(fmt)
     }
}

impl<T: Write> Into<Box<Write>> for GuardWrite<'static, T> {
     #[inline]
     fn into(self) -> Box<Write> {
          Box::new(self) as Box<Write>
     }
}