cluExtIO/write/flush/
drop.rs

1
2use std::io::Write;
3use std::io;
4use std::fmt;
5
6#[derive(Debug)]
7///An implementation of `Trait Write`, which calls the flush() method on drop.                                  
8pub struct FlushDropWrite<T: Write>(T);
9
10impl<T: Write> FlushDropWrite<T> {
11	#[inline]
12	pub fn new(a: T) -> Self {
13		FlushDropWrite(a)
14	}
15
16	#[inline(always)]
17	pub fn flush(self) {}
18}
19
20
21impl<T: Write> From<T> for FlushDropWrite<T> {
22	#[inline(always)]
23	fn from(a: T) -> Self {
24		FlushDropWrite::new(a)
25	}
26}
27
28
29impl<T: Write> Write for FlushDropWrite<T> {
30	#[inline(always)]
31	fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
32		self.0.write(buf)
33	}
34
35	#[inline(always)]
36	fn flush(&mut self) -> io::Result<()> {
37		self.0.flush()
38	}
39
40	#[inline(always)]
41	fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
42		self.0.write_all(buf)
43	}
44
45	#[inline(always)]
46	fn write_fmt(&mut self, fmt: fmt::Arguments) -> io::Result<()> {
47		self.0.write_fmt(fmt)
48	}
49}
50
51impl<T: Write + Clone> Clone for FlushDropWrite<T> {
52     #[inline]
53     fn clone(&self) -> Self {
54          Self::new(self.0.clone())
55     }
56}
57
58impl<T: Write> Drop for FlushDropWrite<T> {
59	#[inline(always)]
60	fn drop(&mut self) {
61		let _e = self.0.flush();
62	}
63}
64
65
66impl<T: 'static + Write> Into<Box<Write>> for FlushDropWrite<T> {
67     #[inline]
68     fn into(self) -> Box<Write> {
69          Box::new(self) as Box<Write>
70     }
71}