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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
use std::fmt::{self, Arguments};
use std::fs;
use std::io::{self, Stdout, Stderr, Write};
use std::path::{Path, PathBuf};
use std::rc::Rc;
use name::{NameDisplay, NameStore};
pub struct GlobalIo {
pub stdout: Rc<SharedWrite>,
}
impl GlobalIo {
pub fn new(stdout: Rc<SharedWrite>) -> GlobalIo {
GlobalIo{
stdout: stdout,
}
}
pub fn null() -> GlobalIo {
GlobalIo::new(Rc::new(Sink))
}
}
impl Default for GlobalIo {
fn default() -> GlobalIo {
GlobalIo::new(Rc::new(io::stdout()))
}
}
#[derive(Debug)]
pub struct IoError {
pub err: io::Error,
pub path: PathBuf,
pub mode: IoMode,
}
impl IoError {
pub fn new(mode: IoMode, path: &Path, err: io::Error) -> IoError {
IoError{
mode: mode,
path: path.to_owned(),
err: err,
}
}
}
impl fmt::Display for IoError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "failed to {} file `{}`: {}",
self.mode, self.path.display(), self.err)
}
}
impl NameDisplay for IoError {
fn fmt(&self, _names: &NameStore, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum IoMode {
Create,
Open,
Read,
Stat,
Write,
}
impl fmt::Display for IoMode {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(match *self {
IoMode::Create => "create",
IoMode::Open => "open",
IoMode::Read => "read",
IoMode::Stat => "stat",
IoMode::Write => "write",
})
}
}
pub trait SharedWrite {
fn write_all(&self, buf: &[u8]) -> Result<(), IoError>;
fn write_fmt(&self, fmt: Arguments) -> Result<(), IoError>;
fn flush(&self) -> Result<(), IoError>;
}
macro_rules! shared_write {
( $ty:ty => $name:expr ) => {
impl SharedWrite for $ty {
fn write_all(&self, buf: &[u8]) -> Result<(), IoError> {
let mut lock = self.lock();
Write::write_all(&mut lock, buf)
.map_err(|e| IoError::new(
IoMode::Write, Path::new($name), e))
}
fn write_fmt(&self, fmt: Arguments) -> Result<(), IoError> {
let mut lock = self.lock();
Write::write_fmt(&mut lock, fmt)
.map_err(|e| IoError::new(
IoMode::Write, Path::new($name), e))
}
fn flush(&self) -> Result<(), IoError> {
let mut lock = self.lock();
Write::flush(&mut lock)
.map_err(|e| IoError::new(
IoMode::Write, Path::new($name), e))
}
}
}
}
shared_write!{ Stdout => "<stdout>" }
shared_write!{ Stderr => "<stderr>" }
pub struct Sink;
impl SharedWrite for Sink {
fn write_all(&self, _buf: &[u8]) -> Result<(), IoError> { Ok(()) }
fn write_fmt(&self, _fmt: Arguments) -> Result<(), IoError> { Ok(()) }
fn flush(&self) -> Result<(), IoError> { Ok(()) }
}
pub struct File {
file: fs::File,
path: PathBuf,
}
impl File {
pub fn new(file: fs::File, path: PathBuf) -> File {
File{
file: file,
path: path,
}
}
}
impl SharedWrite for File {
fn write_all(&self, buf: &[u8]) -> Result<(), IoError> {
Write::write_all(&mut &self.file, buf)
.map_err(|e| IoError::new(IoMode::Write, &self.path, e))
}
fn write_fmt(&self, fmt: Arguments) -> Result<(), IoError> {
Write::write_fmt(&mut &self.file, fmt)
.map_err(|e| IoError::new(IoMode::Write, &self.path, e))
}
fn flush(&self) -> Result<(), IoError> {
Write::flush(&mut &self.file)
.map_err(|e| IoError::new(IoMode::Write, &self.path, e))
}
}