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
66
67
68
69
70
71

use std::io::Write;
use std::io;
use std::fmt;

#[derive(Debug)]
///An implementation of `Trait Write`, which calls the flush() method on drop.                                  
pub struct FlushDropWrite<T: Write>(T);

impl<T: Write> FlushDropWrite<T> {
	#[inline]
	pub fn new(a: T) -> Self {
		FlushDropWrite(a)
	}

	#[inline(always)]
	pub fn flush(self) {}
}


impl<T: Write> From<T> for FlushDropWrite<T> {
	#[inline(always)]
	fn from(a: T) -> Self {
		FlushDropWrite::new(a)
	}
}


impl<T: Write> Write for FlushDropWrite<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 + Clone> Clone for FlushDropWrite<T> {
     #[inline]
     fn clone(&self) -> Self {
          Self::new(self.0.clone())
     }
}

impl<T: Write> Drop for FlushDropWrite<T> {
	#[inline(always)]
	fn drop(&mut self) {
		let _e = self.0.flush();
	}
}


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