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

use crate::write::flush::FlushDropWrite;
use crate::write::ext_write::ExtWrite;

use std::marker::PhantomData;
use std::io::Write;
use std::io;
use std::fmt;

///An implementation of `Trait Write` that calls the flush() method when removing a lock.
#[derive(Debug)]
pub struct FlushLockWrite<'a, T: ExtWrite<'a>>(T, PhantomData<&'a ()>);

impl<'a, T: ExtWrite<'a>> FlushLockWrite<'a, T> {
	#[inline]
	pub fn new(a: T) -> Self {
		FlushLockWrite(a, PhantomData)
	}
}

impl<'a, T: ExtWrite<'a>> From<T> for FlushLockWrite<'a, T> {
	#[inline]
	fn from(a: T) -> Self {
		FlushLockWrite::new(a)
	}
}


impl<'a, T: ExtWrite<'a>> Write for FlushLockWrite<'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<'a, T: ExtWrite<'a>> ExtWrite<'a> for FlushLockWrite<'a, T> {
     type LockWrite = FlushDropWrite<T::LockWrite>;

     #[inline]
     fn lock(&'a self) -> Self::LockWrite {
          FlushDropWrite::new(self.0.lock())
     }
}

impl<'a, T: ExtWrite<'a> + Clone> Clone for FlushLockWrite<'a, T> {
     #[inline]
     fn clone(&self) -> Self {
          Self::new(self.0.clone())
     }
}