Modify _io's fd_buf_reader to do something slightly more useful: produce multiple buffers on demand.
This commit is contained in:
parent
40d6aacb82
commit
15ef6c12d9
|
@ -1,26 +1,46 @@
|
|||
type buf_reader = obj {
|
||||
fn read(vec[u8] buf) -> uint;
|
||||
type buf_reader = unsafe obj {
|
||||
fn read() -> vec[u8];
|
||||
};
|
||||
|
||||
type buf_writer = obj {
|
||||
fn write(vec[u8] buf) -> uint;
|
||||
};
|
||||
fn default_bufsz() -> uint {
|
||||
ret uint(4096);
|
||||
}
|
||||
|
||||
fn mk_buf_reader(str s) -> buf_reader {
|
||||
fn new_buf() -> vec[u8] {
|
||||
let vec[u8] v = vec();
|
||||
let uint i = default_bufsz();
|
||||
while (i > uint(0)) {
|
||||
i -= uint(1);
|
||||
v += vec(u8(0));
|
||||
}
|
||||
// FIXME (issue #93): should be:
|
||||
// ret _vec.alloc[u8](default_bufsz());
|
||||
}
|
||||
|
||||
fn new_buf_reader(str s) -> buf_reader {
|
||||
|
||||
unsafe obj fd_buf_reader(int fd, mutable vec[u8] buf) {
|
||||
|
||||
fn read() -> vec[u8] {
|
||||
|
||||
// Ensure our buf is singly-referenced.
|
||||
if (_vec.rustrt.refcount[u8](buf) != uint(1)) {
|
||||
buf = new_buf();
|
||||
}
|
||||
|
||||
auto len = _vec.len[u8](buf);
|
||||
auto vbuf = _vec.buf[u8](buf);
|
||||
auto count = os.libc.read(fd, vbuf, len);
|
||||
|
||||
obj fd_reader(int fd) {
|
||||
fn read(vec[u8] v) -> uint {
|
||||
auto len = _vec.len[u8](v);
|
||||
auto buf = _vec.buf[u8](v);
|
||||
auto count = os.libc.read(fd, buf, len);
|
||||
if (count < 0) {
|
||||
log "error filling buffer";
|
||||
log sys.rustrt.last_os_error();
|
||||
fail;
|
||||
} else {
|
||||
ret uint(count);
|
||||
ret buf;
|
||||
}
|
||||
}
|
||||
|
||||
drop {
|
||||
os.libc.close(fd);
|
||||
}
|
||||
|
@ -32,5 +52,5 @@ fn mk_buf_reader(str s) -> buf_reader {
|
|||
log sys.rustrt.last_os_error();
|
||||
fail;
|
||||
}
|
||||
ret fd_reader(fd);
|
||||
ret fd_buf_reader(fd, new_buf());
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue