Skip to content

Commit dab8f62

Browse files
marijnhunknown
authored and
unknown
committed
---
yaml --- r: 1582 b: refs/heads/master c: dddd7d8 h: refs/heads/master v: v3
1 parent 43c9196 commit dab8f62

File tree

2 files changed

+49
-24
lines changed

2 files changed

+49
-24
lines changed

[refs]

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: ea2c86874448439a31012ccb141450c67e3d94d8
2+
refs/heads/master: dddd7d8f447215a88aa87f438d4f495cf54be84d

trunk/src/lib/io.rs

+48-23
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ tag fileflag {
9191
truncate;
9292
}
9393

94+
// FIXME move into fd_buf_writer
9495
fn writefd(int fd, vec[u8] v) {
9596
auto len = _vec.len[u8](v);
9697
auto count = 0u;
@@ -107,19 +108,17 @@ fn writefd(int fd, vec[u8] v) {
107108
}
108109
}
109110

110-
fn new_buf_writer(str path, vec[fileflag] flags) -> buf_writer {
111-
112-
state obj fd_buf_writer(int fd) {
113-
114-
fn write(vec[u8] v) {
115-
writefd(fd, v);
116-
}
111+
state obj fd_buf_writer(int fd, bool must_close) {
112+
fn write(vec[u8] v) {
113+
writefd(fd, v);
114+
}
117115

118-
drop {
119-
os.libc.close(fd);
120-
}
116+
drop {
117+
if (must_close) {os.libc.close(fd);}
121118
}
119+
}
122120

121+
fn file_buf_writer(str path, vec[fileflag] flags) -> buf_writer {
123122
let int fflags =
124123
os.libc_constants.O_WRONLY() |
125124
os.libc_constants.O_BINARY();
@@ -142,26 +141,52 @@ fn new_buf_writer(str path, vec[fileflag] flags) -> buf_writer {
142141
log sys.rustrt.last_os_error();
143142
fail;
144143
}
145-
ret fd_buf_writer(fd);
144+
ret fd_buf_writer(fd, true);
146145
}
147146

148147
type writer =
149148
state obj {
150-
fn write_str(str s);
151-
fn write_int(int n);
152-
fn write_uint(uint n);
149+
impure fn write_str(str s);
150+
impure fn write_int(int n);
151+
impure fn write_uint(uint n);
153152
};
154153

155-
fn file_writer(str path,
156-
vec[fileflag] flags)
157-
-> writer
158-
{
159-
state obj fw(buf_writer out) {
160-
fn write_str(str s) { out.write(_str.bytes(s)); }
161-
fn write_int(int n) { out.write(_str.bytes(_int.to_str(n, 10u))); }
162-
fn write_uint(uint n) { out.write(_str.bytes(_uint.to_str(n, 10u))); }
154+
state obj new_writer(buf_writer out) {
155+
impure fn write_str(str s) { out.write(_str.bytes(s)); }
156+
impure fn write_int(int n) { out.write(_str.bytes(_int.to_str(n, 10u))); }
157+
impure fn write_uint(uint n) { out.write(_str.bytes(_uint.to_str(n, 10u))); }
158+
}
159+
160+
fn file_writer(str path, vec[fileflag] flags) -> writer {
161+
ret new_writer(file_buf_writer(path, flags));
162+
}
163+
164+
// FIXME it would be great if this could be a const named stdout
165+
fn stdout_writer() -> writer {
166+
ret new_writer(fd_buf_writer(1, false));
167+
}
168+
169+
type str_writer =
170+
state obj {
171+
fn get_writer() -> writer;
172+
fn get_str() -> str;
173+
};
174+
175+
type str_buf = @rec(mutable str buf);
176+
177+
// TODO awkward! it's not possible to implement a writer with an extra method
178+
fn string_writer() -> str_writer {
179+
auto buf = @rec(mutable buf = "");
180+
state obj str_writer_writer(str_buf buf) {
181+
impure fn write_str(str s) { buf.buf += s; }
182+
impure fn write_int(int n) { buf.buf += _int.to_str(n, 10u); }
183+
impure fn write_uint(uint n) { buf.buf += _uint.to_str(n, 10u); }
184+
}
185+
state obj str_writer_wrap(writer wr, str_buf buf) {
186+
fn get_writer() -> writer {ret wr;}
187+
fn get_str() -> str {ret buf.buf;}
163188
}
164-
ret fw(new_buf_writer(path, flags));
189+
ret str_writer_wrap(str_writer_writer(buf), buf);
165190
}
166191

167192
//

0 commit comments

Comments
 (0)