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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
use std::fs::File;
use std::fmt::{Debug, Formatter, Error};
use std::io::Result;
pub use std::io::Write;
pub use std::io::Read;
pub use std::path::Path;
pub use std::io::Seek;
pub use std::io::SeekFrom;
pub struct WFile { f: File }
impl WFile {
pub fn sync_all(&self) -> Result<()> {
self.f.sync_all()
}
pub fn sync_data(&self) -> Result<()> {
self.f.sync_data()
}
pub fn set_len(&self, size: u64) -> Result<()> {
self.f.set_len(size)
}
pub fn metadata(&self) -> Result<std::fs::Metadata> {
self.f.metadata()
}
}
pub struct RFile { f: File }
impl RFile {
pub fn metadata(&self) -> Result<std::fs::Metadata> {
self.f.metadata()
}
}
impl Debug for WFile {
fn fmt(&self, fmtr: &mut Formatter) -> std::result::Result<(), Error> {
self.f.fmt(fmtr)
}
}
impl Debug for RFile {
fn fmt(&self, fmtr: &mut Formatter) -> std::result::Result<(), Error> {
self.f.fmt(fmtr)
}
}
impl Seek for RFile {
fn seek(&mut self, pos: SeekFrom) -> Result<u64> {
self.f.seek(pos)
}
}
impl Seek for WFile {
fn seek(&mut self, pos: SeekFrom) -> Result<u64> {
self.f.seek(pos)
}
}
impl Write for WFile {
fn write(&mut self, buf: &[u8]) -> Result<usize> {
self.f.write(buf)
}
fn flush(&mut self) -> Result<()> {
self.f.flush()
}
fn write_all(&mut self, buf: &[u8]) -> Result<()> {
self.f.write_all(buf)
}
fn write_fmt(&mut self, fmt: std::fmt::Arguments) -> Result<()> {
self.f.write_fmt(fmt)
}
fn by_ref(&mut self) -> &mut Self {
self
}
}
impl Read for RFile {
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
self.f.read(buf)
}
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize> {
self.f.read_to_end(buf)
}
fn read_to_string(&mut self, buf: &mut String) -> Result<usize> {
self.f.read_to_string(buf)
}
fn by_ref(&mut self) -> &mut Self {
self
}
}
pub fn open<P: AsRef<Path>>(path: P) -> Result<RFile> {
Ok(RFile {
f: try!(File::open(path))
})
}
pub fn create<P: AsRef<Path>>(path: P) -> Result<WFile> {
Ok(WFile {
f: try!(File::create(path))
})
}