1
Fork 0

Convert core::io to use explicit self (for issue #4118 and issue #2004)

This commit is contained in:
gareth 2012-12-24 18:52:53 +00:00
parent cd6960edf0
commit feff3a9c6c
4 changed files with 184 additions and 190 deletions

View file

@ -188,11 +188,11 @@ fn SipState(key0: u64, key1: u64) -> SipState {
} }
impl &SipState : io::Writer { impl SipState : io::Writer {
// Methods for io::writer // Methods for io::writer
#[inline(always)] #[inline(always)]
fn write(msg: &[const u8]) { fn write(&self, msg: &[const u8]) {
macro_rules! u8to64_le ( macro_rules! u8to64_le (
($buf:expr, $i:expr) => ($buf:expr, $i:expr) =>
@ -282,16 +282,16 @@ impl &SipState : io::Writer {
self.ntail = left; self.ntail = left;
} }
fn seek(_x: int, _s: io::SeekStyle) { fn seek(&self, _x: int, _s: io::SeekStyle) {
fail; fail;
} }
fn tell() -> uint { fn tell(&self) -> uint {
self.length self.length
} }
fn flush() -> int { fn flush(&self) -> int {
0 0
} }
fn get_type() -> io::WriterType { fn get_type(&self) -> io::WriterType {
io::File io::File
} }
} }

View file

@ -49,122 +49,122 @@ pub trait Reader {
/// Read up to len bytes (or EOF) and put them into bytes (which /// Read up to len bytes (or EOF) and put them into bytes (which
/// must be at least len bytes long). Return number of bytes read. /// must be at least len bytes long). Return number of bytes read.
// FIXME (#2982): This should probably return an error. // FIXME (#2982): This should probably return an error.
fn read(bytes: &[mut u8], len: uint) -> uint; fn read(&self, bytes: &[mut u8], len: uint) -> uint;
/// Read a single byte, returning a negative value for EOF or read error. /// Read a single byte, returning a negative value for EOF or read error.
fn read_byte() -> int; fn read_byte(&self) -> int;
/// Return whether the stream is currently at EOF position. /// Return whether the stream is currently at EOF position.
fn eof() -> bool; fn eof(&self) -> bool;
/// Move the current position within the stream. The second parameter /// Move the current position within the stream. The second parameter
/// determines the position that the first parameter is relative to. /// determines the position that the first parameter is relative to.
fn seek(position: int, style: SeekStyle); fn seek(&self, position: int, style: SeekStyle);
/// Return the current position within the stream. /// Return the current position within the stream.
fn tell() -> uint; fn tell(&self) -> uint;
} }
/// Generic utility functions defined on readers. /// Generic utility functions defined on readers.
pub trait ReaderUtil { pub trait ReaderUtil {
/// Read len bytes into a new vec. /// Read len bytes into a new vec.
fn read_bytes(len: uint) -> ~[u8]; fn read_bytes(&self, len: uint) -> ~[u8];
/// Read up until the first '\n' char (which is not returned), or EOF. /// Read up until the first '\n' char (which is not returned), or EOF.
fn read_line() -> ~str; fn read_line(&self) -> ~str;
/// Read n utf-8 encoded chars. /// Read n utf-8 encoded chars.
fn read_chars(n: uint) -> ~[char]; fn read_chars(&self, n: uint) -> ~[char];
/// Read a single utf-8 encoded char. /// Read a single utf-8 encoded char.
fn read_char() -> char; fn read_char(&self) -> char;
/// Read up until the first null byte (which is not returned), or EOF. /// Read up until the first null byte (which is not returned), or EOF.
fn read_c_str() -> ~str; fn read_c_str(&self) -> ~str;
/// Read all the data remaining in the stream in one go. /// Read all the data remaining in the stream in one go.
fn read_whole_stream() -> ~[u8]; fn read_whole_stream(&self) -> ~[u8];
/// Iterate over every byte until the iterator breaks or EOF. /// Iterate over every byte until the iterator breaks or EOF.
fn each_byte(it: fn(int) -> bool); fn each_byte(&self, it: fn(int) -> bool);
/// Iterate over every char until the iterator breaks or EOF. /// Iterate over every char until the iterator breaks or EOF.
fn each_char(it: fn(char) -> bool); fn each_char(&self, it: fn(char) -> bool);
/// Iterate over every line until the iterator breaks or EOF. /// Iterate over every line until the iterator breaks or EOF.
fn each_line(it: fn(&str) -> bool); fn each_line(&self, it: fn(&str) -> bool);
/// Read n (between 1 and 8) little-endian unsigned integer bytes. /// Read n (between 1 and 8) little-endian unsigned integer bytes.
fn read_le_uint_n(nbytes: uint) -> u64; fn read_le_uint_n(&self, nbytes: uint) -> u64;
/// Read n (between 1 and 8) little-endian signed integer bytes. /// Read n (between 1 and 8) little-endian signed integer bytes.
fn read_le_int_n(nbytes: uint) -> i64; fn read_le_int_n(&self, nbytes: uint) -> i64;
/// Read n (between 1 and 8) big-endian unsigned integer bytes. /// Read n (between 1 and 8) big-endian unsigned integer bytes.
fn read_be_uint_n(nbytes: uint) -> u64; fn read_be_uint_n(&self, nbytes: uint) -> u64;
/// Read n (between 1 and 8) big-endian signed integer bytes. /// Read n (between 1 and 8) big-endian signed integer bytes.
fn read_be_int_n(nbytes: uint) -> i64; fn read_be_int_n(&self, nbytes: uint) -> i64;
/// Read a little-endian uint (number of bytes depends on system). /// Read a little-endian uint (number of bytes depends on system).
fn read_le_uint() -> uint; fn read_le_uint(&self) -> uint;
/// Read a little-endian int (number of bytes depends on system). /// Read a little-endian int (number of bytes depends on system).
fn read_le_int() -> int; fn read_le_int(&self) -> int;
/// Read a big-endian uint (number of bytes depends on system). /// Read a big-endian uint (number of bytes depends on system).
fn read_be_uint() -> uint; fn read_be_uint(&self) -> uint;
/// Read a big-endian int (number of bytes depends on system). /// Read a big-endian int (number of bytes depends on system).
fn read_be_int() -> int; fn read_be_int(&self) -> int;
/// Read a big-endian u64 (8 bytes). /// Read a big-endian u64 (8 bytes).
fn read_be_u64() -> u64; fn read_be_u64(&self) -> u64;
/// Read a big-endian u32 (4 bytes). /// Read a big-endian u32 (4 bytes).
fn read_be_u32() -> u32; fn read_be_u32(&self) -> u32;
/// Read a big-endian u16 (2 bytes). /// Read a big-endian u16 (2 bytes).
fn read_be_u16() -> u16; fn read_be_u16(&self) -> u16;
/// Read a big-endian i64 (8 bytes). /// Read a big-endian i64 (8 bytes).
fn read_be_i64() -> i64; fn read_be_i64(&self) -> i64;
/// Read a big-endian i32 (4 bytes). /// Read a big-endian i32 (4 bytes).
fn read_be_i32() -> i32; fn read_be_i32(&self) -> i32;
/// Read a big-endian i16 (2 bytes). /// Read a big-endian i16 (2 bytes).
fn read_be_i16() -> i16; fn read_be_i16(&self) -> i16;
/// Read a little-endian u64 (8 bytes). /// Read a little-endian u64 (8 bytes).
fn read_le_u64() -> u64; fn read_le_u64(&self) -> u64;
/// Read a little-endian u32 (4 bytes). /// Read a little-endian u32 (4 bytes).
fn read_le_u32() -> u32; fn read_le_u32(&self) -> u32;
/// Read a little-endian u16 (2 bytes). /// Read a little-endian u16 (2 bytes).
fn read_le_u16() -> u16; fn read_le_u16(&self) -> u16;
/// Read a litle-endian i64 (8 bytes). /// Read a litle-endian i64 (8 bytes).
fn read_le_i64() -> i64; fn read_le_i64(&self) -> i64;
/// Read a litle-endian i32 (4 bytes). /// Read a litle-endian i32 (4 bytes).
fn read_le_i32() -> i32; fn read_le_i32(&self) -> i32;
/// Read a litle-endian i16 (2 bytes). /// Read a litle-endian i16 (2 bytes).
fn read_le_i16() -> i16; fn read_le_i16(&self) -> i16;
/// Read a u8 (1 byte). /// Read a u8 (1 byte).
fn read_u8() -> u8; fn read_u8(&self) -> u8;
/// Read a i8 (1 byte). /// Read a i8 (1 byte).
fn read_i8() -> i8; fn read_i8(&self) -> i8;
} }
impl<T: Reader> T : ReaderUtil { impl<T: Reader> T : ReaderUtil {
fn read_bytes(len: uint) -> ~[u8] { fn read_bytes(&self,len: uint) -> ~[u8] {
let mut bytes = vec::with_capacity(len); let mut bytes = vec::with_capacity(len);
unsafe { vec::raw::set_len(&mut bytes, len); } unsafe { vec::raw::set_len(&mut bytes, len); }
@ -174,7 +174,7 @@ impl<T: Reader> T : ReaderUtil {
move bytes move bytes
} }
fn read_line() -> ~str { fn read_line(&self) -> ~str {
let mut bytes = ~[]; let mut bytes = ~[];
loop { loop {
let ch = self.read_byte(); let ch = self.read_byte();
@ -184,7 +184,7 @@ impl<T: Reader> T : ReaderUtil {
str::from_bytes(bytes) str::from_bytes(bytes)
} }
fn read_chars(n: uint) -> ~[char] { fn read_chars(&self, n: uint) -> ~[char] {
// returns the (consumed offset, n_req), appends characters to &chars // returns the (consumed offset, n_req), appends characters to &chars
fn chars_from_bytes<T: Reader>(bytes: &~[u8], chars: &mut ~[char]) fn chars_from_bytes<T: Reader>(bytes: &~[u8], chars: &mut ~[char])
-> (uint, uint) { -> (uint, uint) {
@ -245,7 +245,7 @@ impl<T: Reader> T : ReaderUtil {
move chars move chars
} }
fn read_char() -> char { fn read_char(&self) -> char {
let c = self.read_chars(1); let c = self.read_chars(1);
if vec::len(c) == 0 { if vec::len(c) == 0 {
return -1 as char; // FIXME will this stay valid? // #2004 return -1 as char; // FIXME will this stay valid? // #2004
@ -254,7 +254,7 @@ impl<T: Reader> T : ReaderUtil {
return c[0]; return c[0];
} }
fn read_c_str() -> ~str { fn read_c_str(&self) -> ~str {
let mut bytes: ~[u8] = ~[]; let mut bytes: ~[u8] = ~[];
loop { loop {
let ch = self.read_byte(); let ch = self.read_byte();
@ -263,25 +263,25 @@ impl<T: Reader> T : ReaderUtil {
str::from_bytes(bytes) str::from_bytes(bytes)
} }
fn read_whole_stream() -> ~[u8] { fn read_whole_stream(&self) -> ~[u8] {
let mut bytes: ~[u8] = ~[]; let mut bytes: ~[u8] = ~[];
while !self.eof() { bytes.push_all(self.read_bytes(2048u)); } while !self.eof() { bytes.push_all(self.read_bytes(2048u)); }
move bytes move bytes
} }
fn each_byte(it: fn(int) -> bool) { fn each_byte(&self, it: fn(int) -> bool) {
while !self.eof() { while !self.eof() {
if !it(self.read_byte()) { break; } if !it(self.read_byte()) { break; }
} }
} }
fn each_char(it: fn(char) -> bool) { fn each_char(&self, it: fn(char) -> bool) {
while !self.eof() { while !self.eof() {
if !it(self.read_char()) { break; } if !it(self.read_char()) { break; }
} }
} }
fn each_line(it: fn(s: &str) -> bool) { fn each_line(&self, it: fn(s: &str) -> bool) {
while !self.eof() { while !self.eof() {
if !it(self.read_line()) { break; } if !it(self.read_line()) { break; }
} }
@ -289,7 +289,7 @@ impl<T: Reader> T : ReaderUtil {
// FIXME int reading methods need to deal with eof - issue #2004 // FIXME int reading methods need to deal with eof - issue #2004
fn read_le_uint_n(nbytes: uint) -> u64 { fn read_le_uint_n(&self, nbytes: uint) -> u64 {
assert nbytes > 0 && nbytes <= 8; assert nbytes > 0 && nbytes <= 8;
let mut val = 0u64, pos = 0, i = nbytes; let mut val = 0u64, pos = 0, i = nbytes;
@ -301,11 +301,11 @@ impl<T: Reader> T : ReaderUtil {
val val
} }
fn read_le_int_n(nbytes: uint) -> i64 { fn read_le_int_n(&self, nbytes: uint) -> i64 {
extend_sign(self.read_le_uint_n(nbytes), nbytes) extend_sign(self.read_le_uint_n(nbytes), nbytes)
} }
fn read_be_uint_n(nbytes: uint) -> u64 { fn read_be_uint_n(&self, nbytes: uint) -> u64 {
assert nbytes > 0 && nbytes <= 8; assert nbytes > 0 && nbytes <= 8;
let mut val = 0u64, i = nbytes; let mut val = 0u64, i = nbytes;
@ -316,79 +316,79 @@ impl<T: Reader> T : ReaderUtil {
val val
} }
fn read_be_int_n(nbytes: uint) -> i64 { fn read_be_int_n(&self, nbytes: uint) -> i64 {
extend_sign(self.read_be_uint_n(nbytes), nbytes) extend_sign(self.read_be_uint_n(nbytes), nbytes)
} }
fn read_le_uint() -> uint { fn read_le_uint(&self) -> uint {
self.read_le_uint_n(uint::bytes) as uint self.read_le_uint_n(uint::bytes) as uint
} }
fn read_le_int() -> int { fn read_le_int(&self) -> int {
self.read_le_int_n(int::bytes) as int self.read_le_int_n(int::bytes) as int
} }
fn read_be_uint() -> uint { fn read_be_uint(&self) -> uint {
self.read_be_uint_n(uint::bytes) as uint self.read_be_uint_n(uint::bytes) as uint
} }
fn read_be_int() -> int { fn read_be_int(&self) -> int {
self.read_be_int_n(int::bytes) as int self.read_be_int_n(int::bytes) as int
} }
fn read_be_u64() -> u64 { fn read_be_u64(&self) -> u64 {
self.read_be_uint_n(8) as u64 self.read_be_uint_n(8) as u64
} }
fn read_be_u32() -> u32 { fn read_be_u32(&self) -> u32 {
self.read_be_uint_n(4) as u32 self.read_be_uint_n(4) as u32
} }
fn read_be_u16() -> u16 { fn read_be_u16(&self) -> u16 {
self.read_be_uint_n(2) as u16 self.read_be_uint_n(2) as u16
} }
fn read_be_i64() -> i64 { fn read_be_i64(&self) -> i64 {
self.read_be_int_n(8) as i64 self.read_be_int_n(8) as i64
} }
fn read_be_i32() -> i32 { fn read_be_i32(&self) -> i32 {
self.read_be_int_n(4) as i32 self.read_be_int_n(4) as i32
} }
fn read_be_i16() -> i16 { fn read_be_i16(&self) -> i16 {
self.read_be_int_n(2) as i16 self.read_be_int_n(2) as i16
} }
fn read_le_u64() -> u64 { fn read_le_u64(&self) -> u64 {
self.read_le_uint_n(8) as u64 self.read_le_uint_n(8) as u64
} }
fn read_le_u32() -> u32 { fn read_le_u32(&self) -> u32 {
self.read_le_uint_n(4) as u32 self.read_le_uint_n(4) as u32
} }
fn read_le_u16() -> u16 { fn read_le_u16(&self) -> u16 {
self.read_le_uint_n(2) as u16 self.read_le_uint_n(2) as u16
} }
fn read_le_i64() -> i64 { fn read_le_i64(&self) -> i64 {
self.read_le_int_n(8) as i64 self.read_le_int_n(8) as i64
} }
fn read_le_i32() -> i32 { fn read_le_i32(&self) -> i32 {
self.read_le_int_n(4) as i32 self.read_le_int_n(4) as i32
} }
fn read_le_i16() -> i16 { fn read_le_i16(&self) -> i16 {
self.read_le_int_n(2) as i16 self.read_le_int_n(2) as i16
} }
fn read_u8() -> u8 { fn read_u8(&self) -> u8 {
self.read_byte() as u8 self.read_byte() as u8
} }
fn read_i8() -> i8 { fn read_i8(&self) -> i8 {
self.read_byte() as i8 self.read_byte() as i8
} }
} }
@ -409,36 +409,38 @@ fn convert_whence(whence: SeekStyle) -> i32 {
} }
impl *libc::FILE: Reader { impl *libc::FILE: Reader {
fn read(bytes: &[mut u8], len: uint) -> uint { fn read(&self, bytes: &[mut u8], len: uint) -> uint {
do vec::as_mut_buf(bytes) |buf_p, buf_len| { do vec::as_mut_buf(bytes) |buf_p, buf_len| {
assert buf_len >= len; assert buf_len >= len;
let count = libc::fread(buf_p as *mut c_void, 1u as size_t, let count = libc::fread(buf_p as *mut c_void, 1u as size_t,
len as size_t, self); len as size_t, *self);
count as uint count as uint
} }
} }
fn read_byte() -> int { return libc::fgetc(self) as int; } fn read_byte(&self) -> int { return libc::fgetc(*self) as int; }
fn eof() -> bool { return libc::feof(self) != 0 as c_int; } fn eof(&self) -> bool { return libc::feof(*self) != 0 as c_int; }
fn seek(offset: int, whence: SeekStyle) { fn seek(&self, offset: int, whence: SeekStyle) {
assert libc::fseek(self, offset as c_long, convert_whence(whence)) assert libc::fseek(*self, offset as c_long, convert_whence(whence))
== 0 as c_int; == 0 as c_int;
} }
fn tell() -> uint { return libc::ftell(self) as uint; } fn tell(&self) -> uint { return libc::ftell(*self) as uint; }
} }
// A forwarding impl of reader that also holds on to a resource for the // A forwarding impl of reader that also holds on to a resource for the
// duration of its lifetime. // duration of its lifetime.
// FIXME there really should be a better way to do this // #2004 // FIXME there really should be a better way to do this // #2004
impl<T: Reader, C> {base: T, cleanup: C}: Reader { impl<T: Reader, C> {base: T, cleanup: C}: Reader {
fn read(bytes: &[mut u8], len: uint) -> uint { fn read(&self, bytes: &[mut u8], len: uint) -> uint {
self.base.read(bytes, len) self.base.read(bytes, len)
} }
fn read_byte() -> int { self.base.read_byte() } fn read_byte(&self) -> int { self.base.read_byte() }
fn eof() -> bool { self.base.eof() } fn eof(&self) -> bool { self.base.eof() }
fn seek(off: int, whence: SeekStyle) { self.base.seek(off, whence) } fn seek(&self, off: int, whence: SeekStyle) {
fn tell() -> uint { self.base.tell() } self.base.seek(off, whence)
}
fn tell(&self) -> uint { self.base.tell() }
} }
struct FILERes { struct FILERes {
@ -487,7 +489,7 @@ pub struct BytesReader {
} }
impl BytesReader: Reader { impl BytesReader: Reader {
fn read(bytes: &[mut u8], len: uint) -> uint { fn read(&self, bytes: &[mut u8], len: uint) -> uint {
let count = uint::min(len, self.bytes.len() - self.pos); let count = uint::min(len, self.bytes.len() - self.pos);
let view = vec::view(self.bytes, self.pos, self.bytes.len()); let view = vec::view(self.bytes, self.pos, self.bytes.len());
@ -497,18 +499,18 @@ impl BytesReader: Reader {
count count
} }
fn read_byte() -> int { fn read_byte(&self) -> int {
if self.pos == self.bytes.len() { return -1; } if self.pos == self.bytes.len() { return -1; }
let b = self.bytes[self.pos]; let b = self.bytes[self.pos];
self.pos += 1u; self.pos += 1u;
return b as int; return b as int;
} }
fn eof() -> bool { self.pos == self.bytes.len() } fn eof(&self) -> bool { self.pos == self.bytes.len() }
fn seek(offset: int, whence: SeekStyle) { fn seek(&self, offset: int, whence: SeekStyle) {
let pos = self.pos; let pos = self.pos;
self.pos = seek_in_buf(offset, pos, self.bytes.len(), whence); self.pos = seek_in_buf(offset, pos, self.bytes.len(), whence);
} }
fn tell() -> uint { self.pos } fn tell(&self) -> uint { self.pos }
} }
pub pure fn with_bytes_reader<t>(bytes: &[u8], f: fn(Reader) -> t) -> t { pub pure fn with_bytes_reader<t>(bytes: &[u8], f: fn(Reader) -> t) -> t {
@ -532,34 +534,34 @@ pub enum WriterType { Screen, File }
pub trait Writer { pub trait Writer {
/// Write all of the given bytes. /// Write all of the given bytes.
fn write(v: &[const u8]); fn write(&self, v: &[const u8]);
/// Move the current position within the stream. The second parameter /// Move the current position within the stream. The second parameter
/// determines the position that the first parameter is relative to. /// determines the position that the first parameter is relative to.
fn seek(int, SeekStyle); fn seek(&self, int, SeekStyle);
/// Return the current position within the stream. /// Return the current position within the stream.
fn tell() -> uint; fn tell(&self) -> uint;
/// Flush the output buffer for this stream (if there is one). /// Flush the output buffer for this stream (if there is one).
fn flush() -> int; fn flush(&self) -> int;
/// Determine if this Writer is writing to a file or not. /// Determine if this Writer is writing to a file or not.
fn get_type() -> WriterType; fn get_type(&self) -> WriterType;
} }
impl<T: Writer, C> {base: T, cleanup: C}: Writer { impl<T: Writer, C> {base: T, cleanup: C}: Writer {
fn write(bs: &[const u8]) { self.base.write(bs); } fn write(&self, bs: &[const u8]) { self.base.write(bs); }
fn seek(off: int, style: SeekStyle) { self.base.seek(off, style); } fn seek(&self, off: int, style: SeekStyle) { self.base.seek(off, style); }
fn tell() -> uint { self.base.tell() } fn tell(&self) -> uint { self.base.tell() }
fn flush() -> int { self.base.flush() } fn flush(&self) -> int { self.base.flush() }
fn get_type() -> WriterType { File } fn get_type(&self) -> WriterType { File }
} }
impl *libc::FILE: Writer { impl *libc::FILE: Writer {
fn write(v: &[const u8]) { fn write(&self, v: &[const u8]) {
do vec::as_const_buf(v) |vbuf, len| { do vec::as_const_buf(v) |vbuf, len| {
let nout = libc::fwrite(vbuf as *c_void, 1, len as size_t, self); let nout = libc::fwrite(vbuf as *c_void, 1, len as size_t, *self);
if nout != len as size_t { if nout != len as size_t {
error!("error writing buffer"); error!("error writing buffer");
log(error, os::last_os_error()); log(error, os::last_os_error());
@ -567,14 +569,14 @@ impl *libc::FILE: Writer {
} }
} }
} }
fn seek(offset: int, whence: SeekStyle) { fn seek(&self, offset: int, whence: SeekStyle) {
assert libc::fseek(self, offset as c_long, convert_whence(whence)) assert libc::fseek(*self, offset as c_long, convert_whence(whence))
== 0 as c_int; == 0 as c_int;
} }
fn tell() -> uint { libc::ftell(self) as uint } fn tell(&self) -> uint { libc::ftell(*self) as uint }
fn flush() -> int { libc::fflush(self) as int } fn flush(&self) -> int { libc::fflush(*self) as int }
fn get_type() -> WriterType { fn get_type(&self) -> WriterType {
let fd = libc::fileno(self); let fd = libc::fileno(*self);
if libc::isatty(fd) == 0 { File } if libc::isatty(fd) == 0 { File }
else { Screen } else { Screen }
} }
@ -589,12 +591,12 @@ pub fn FILE_writer(f: *libc::FILE, cleanup: bool) -> Writer {
} }
impl fd_t: Writer { impl fd_t: Writer {
fn write(v: &[const u8]) { fn write(&self, v: &[const u8]) {
let mut count = 0u; let mut count = 0u;
do vec::as_const_buf(v) |vbuf, len| { do vec::as_const_buf(v) |vbuf, len| {
while count < len { while count < len {
let vb = ptr::const_offset(vbuf, count) as *c_void; let vb = ptr::const_offset(vbuf, count) as *c_void;
let nout = libc::write(self, vb, len as size_t); let nout = libc::write(*self, vb, len as size_t);
if nout < 0 as ssize_t { if nout < 0 as ssize_t {
error!("error writing buffer"); error!("error writing buffer");
log(error, os::last_os_error()); log(error, os::last_os_error());
@ -604,17 +606,17 @@ impl fd_t: Writer {
} }
} }
} }
fn seek(_offset: int, _whence: SeekStyle) { fn seek(&self, _offset: int, _whence: SeekStyle) {
error!("need 64-bit foreign calls for seek, sorry"); error!("need 64-bit foreign calls for seek, sorry");
fail; fail;
} }
fn tell() -> uint { fn tell(&self) -> uint {
error!("need 64-bit foreign calls for tell, sorry"); error!("need 64-bit foreign calls for tell, sorry");
fail; fail;
} }
fn flush() -> int { 0 } fn flush(&self) -> int { 0 }
fn get_type() -> WriterType { fn get_type(&self) -> WriterType {
if libc::isatty(self) == 0 { File } else { Screen } if libc::isatty(*self) == 0 { File } else { Screen }
} }
} }
@ -752,145 +754,145 @@ pub fn u64_from_be_bytes(data: &[const u8],
pub trait WriterUtil { pub trait WriterUtil {
/// Write a single utf-8 encoded char. /// Write a single utf-8 encoded char.
fn write_char(ch: char); fn write_char(&self, ch: char);
/// Write every char in the given str, encoded as utf-8. /// Write every char in the given str, encoded as utf-8.
fn write_str(s: &str); fn write_str(&self, s: &str);
/// Write the given str, as utf-8, followed by '\n'. /// Write the given str, as utf-8, followed by '\n'.
fn write_line(s: &str); fn write_line(&self, s: &str);
/// Write the result of passing n through `int::to_str_bytes`. /// Write the result of passing n through `int::to_str_bytes`.
fn write_int(n: int); fn write_int(&self, n: int);
/// Write the result of passing n through `uint::to_str_bytes`. /// Write the result of passing n through `uint::to_str_bytes`.
fn write_uint(n: uint); fn write_uint(&self, n: uint);
/// Write a little-endian uint (number of bytes depends on system). /// Write a little-endian uint (number of bytes depends on system).
fn write_le_uint(n: uint); fn write_le_uint(&self, n: uint);
/// Write a little-endian int (number of bytes depends on system). /// Write a little-endian int (number of bytes depends on system).
fn write_le_int(n: int); fn write_le_int(&self, n: int);
/// Write a big-endian uint (number of bytes depends on system). /// Write a big-endian uint (number of bytes depends on system).
fn write_be_uint(n: uint); fn write_be_uint(&self, n: uint);
/// Write a big-endian int (number of bytes depends on system). /// Write a big-endian int (number of bytes depends on system).
fn write_be_int(n: int); fn write_be_int(&self, n: int);
/// Write a big-endian u64 (8 bytes). /// Write a big-endian u64 (8 bytes).
fn write_be_u64(n: u64); fn write_be_u64(&self, n: u64);
/// Write a big-endian u32 (4 bytes). /// Write a big-endian u32 (4 bytes).
fn write_be_u32(n: u32); fn write_be_u32(&self, n: u32);
/// Write a big-endian u16 (2 bytes). /// Write a big-endian u16 (2 bytes).
fn write_be_u16(n: u16); fn write_be_u16(&self, n: u16);
/// Write a big-endian i64 (8 bytes). /// Write a big-endian i64 (8 bytes).
fn write_be_i64(n: i64); fn write_be_i64(&self, n: i64);
/// Write a big-endian i32 (4 bytes). /// Write a big-endian i32 (4 bytes).
fn write_be_i32(n: i32); fn write_be_i32(&self, n: i32);
/// Write a big-endian i16 (2 bytes). /// Write a big-endian i16 (2 bytes).
fn write_be_i16(n: i16); fn write_be_i16(&self, n: i16);
/// Write a little-endian u64 (8 bytes). /// Write a little-endian u64 (8 bytes).
fn write_le_u64(n: u64); fn write_le_u64(&self, n: u64);
/// Write a little-endian u32 (4 bytes). /// Write a little-endian u32 (4 bytes).
fn write_le_u32(n: u32); fn write_le_u32(&self, n: u32);
/// Write a little-endian u16 (2 bytes). /// Write a little-endian u16 (2 bytes).
fn write_le_u16(n: u16); fn write_le_u16(&self, n: u16);
/// Write a little-endian i64 (8 bytes). /// Write a little-endian i64 (8 bytes).
fn write_le_i64(n: i64); fn write_le_i64(&self, n: i64);
/// Write a little-endian i32 (4 bytes). /// Write a little-endian i32 (4 bytes).
fn write_le_i32(n: i32); fn write_le_i32(&self, n: i32);
/// Write a little-endian i16 (2 bytes). /// Write a little-endian i16 (2 bytes).
fn write_le_i16(n: i16); fn write_le_i16(&self, n: i16);
/// Write a u8 (1 byte). /// Write a u8 (1 byte).
fn write_u8(n: u8); fn write_u8(&self, n: u8);
/// Write a i8 (1 byte). /// Write a i8 (1 byte).
fn write_i8(n: i8); fn write_i8(&self, n: i8);
} }
impl<T: Writer> T : WriterUtil { impl<T: Writer> T : WriterUtil {
fn write_char(ch: char) { fn write_char(&self, ch: char) {
if ch as uint < 128u { if ch as uint < 128u {
self.write(&[ch as u8]); self.write(&[ch as u8]);
} else { } else {
self.write_str(str::from_char(ch)); self.write_str(str::from_char(ch));
} }
} }
fn write_str(s: &str) { str::byte_slice(s, |v| self.write(v)) } fn write_str(&self, s: &str) { str::byte_slice(s, |v| self.write(v)) }
fn write_line(s: &str) { fn write_line(&self, s: &str) {
self.write_str(s); self.write_str(s);
self.write_str(&"\n"); self.write_str(&"\n");
} }
fn write_int(n: int) { fn write_int(&self, n: int) {
int::to_str_bytes(n, 10u, |bytes| self.write(bytes)) int::to_str_bytes(n, 10u, |bytes| self.write(bytes))
} }
fn write_uint(n: uint) { fn write_uint(&self, n: uint) {
uint::to_str_bytes(false, n, 10u, |bytes| self.write(bytes)) uint::to_str_bytes(false, n, 10u, |bytes| self.write(bytes))
} }
fn write_le_uint(n: uint) { fn write_le_uint(&self, n: uint) {
u64_to_le_bytes(n as u64, uint::bytes, |v| self.write(v)) u64_to_le_bytes(n as u64, uint::bytes, |v| self.write(v))
} }
fn write_le_int(n: int) { fn write_le_int(&self, n: int) {
u64_to_le_bytes(n as u64, int::bytes, |v| self.write(v)) u64_to_le_bytes(n as u64, int::bytes, |v| self.write(v))
} }
fn write_be_uint(n: uint) { fn write_be_uint(&self, n: uint) {
u64_to_be_bytes(n as u64, uint::bytes, |v| self.write(v)) u64_to_be_bytes(n as u64, uint::bytes, |v| self.write(v))
} }
fn write_be_int(n: int) { fn write_be_int(&self, n: int) {
u64_to_be_bytes(n as u64, int::bytes, |v| self.write(v)) u64_to_be_bytes(n as u64, int::bytes, |v| self.write(v))
} }
fn write_be_u64(n: u64) { fn write_be_u64(&self, n: u64) {
u64_to_be_bytes(n, 8u, |v| self.write(v)) u64_to_be_bytes(n, 8u, |v| self.write(v))
} }
fn write_be_u32(n: u32) { fn write_be_u32(&self, n: u32) {
u64_to_be_bytes(n as u64, 4u, |v| self.write(v)) u64_to_be_bytes(n as u64, 4u, |v| self.write(v))
} }
fn write_be_u16(n: u16) { fn write_be_u16(&self, n: u16) {
u64_to_be_bytes(n as u64, 2u, |v| self.write(v)) u64_to_be_bytes(n as u64, 2u, |v| self.write(v))
} }
fn write_be_i64(n: i64) { fn write_be_i64(&self, n: i64) {
u64_to_be_bytes(n as u64, 8u, |v| self.write(v)) u64_to_be_bytes(n as u64, 8u, |v| self.write(v))
} }
fn write_be_i32(n: i32) { fn write_be_i32(&self, n: i32) {
u64_to_be_bytes(n as u64, 4u, |v| self.write(v)) u64_to_be_bytes(n as u64, 4u, |v| self.write(v))
} }
fn write_be_i16(n: i16) { fn write_be_i16(&self, n: i16) {
u64_to_be_bytes(n as u64, 2u, |v| self.write(v)) u64_to_be_bytes(n as u64, 2u, |v| self.write(v))
} }
fn write_le_u64(n: u64) { fn write_le_u64(&self, n: u64) {
u64_to_le_bytes(n, 8u, |v| self.write(v)) u64_to_le_bytes(n, 8u, |v| self.write(v))
} }
fn write_le_u32(n: u32) { fn write_le_u32(&self, n: u32) {
u64_to_le_bytes(n as u64, 4u, |v| self.write(v)) u64_to_le_bytes(n as u64, 4u, |v| self.write(v))
} }
fn write_le_u16(n: u16) { fn write_le_u16(&self, n: u16) {
u64_to_le_bytes(n as u64, 2u, |v| self.write(v)) u64_to_le_bytes(n as u64, 2u, |v| self.write(v))
} }
fn write_le_i64(n: i64) { fn write_le_i64(&self, n: i64) {
u64_to_le_bytes(n as u64, 8u, |v| self.write(v)) u64_to_le_bytes(n as u64, 8u, |v| self.write(v))
} }
fn write_le_i32(n: i32) { fn write_le_i32(&self, n: i32) {
u64_to_le_bytes(n as u64, 4u, |v| self.write(v)) u64_to_le_bytes(n as u64, 4u, |v| self.write(v))
} }
fn write_le_i16(n: i16) { fn write_le_i16(&self, n: i16) {
u64_to_le_bytes(n as u64, 2u, |v| self.write(v)) u64_to_le_bytes(n as u64, 2u, |v| self.write(v))
} }
fn write_u8(n: u8) { self.write([n]) } fn write_u8(&self, n: u8) { self.write([n]) }
fn write_i8(n: i8) { self.write([n as u8]) } fn write_i8(&self, n: i8) { self.write([n as u8]) }
} }
#[allow(non_implicitly_copyable_typarams)] #[allow(non_implicitly_copyable_typarams)]
@ -926,7 +928,7 @@ pub struct BytesWriter {
} }
impl BytesWriter: Writer { impl BytesWriter: Writer {
fn write(v: &[const u8]) { fn write(&self, v: &[const u8]) {
do self.bytes.swap |bytes| { do self.bytes.swap |bytes| {
let mut bytes = move bytes; let mut bytes = move bytes;
let v_len = v.len(); let v_len = v.len();
@ -946,22 +948,14 @@ impl BytesWriter: Writer {
move bytes move bytes
} }
} }
fn seek(offset: int, whence: SeekStyle) { fn seek(&self, offset: int, whence: SeekStyle) {
let pos = self.pos; let pos = self.pos;
let len = self.bytes.len(); let len = self.bytes.len();
self.pos = seek_in_buf(offset, pos, len, whence); self.pos = seek_in_buf(offset, pos, len, whence);
} }
fn tell() -> uint { self.pos } fn tell(&self) -> uint { self.pos }
fn flush() -> int { 0 } fn flush(&self) -> int { 0 }
fn get_type() -> WriterType { File } fn get_type(&self) -> WriterType { File }
}
impl @BytesWriter : Writer {
fn write(v: &[const u8]) { (*self).write(v) }
fn seek(offset: int, whence: SeekStyle) { (*self).seek(offset, whence) }
fn tell() -> uint { (*self).tell() }
fn flush() -> int { (*self).flush() }
fn get_type() -> WriterType { (*self).get_type() }
} }
pub pure fn BytesWriter() -> BytesWriter { pub pure fn BytesWriter() -> BytesWriter {
@ -1091,7 +1085,7 @@ pub mod fsync {
} }
// Type of objects that may want to fsync // Type of objects that may want to fsync
pub trait FSyncable { fn fsync(l: Level) -> int; } pub trait FSyncable { fn fsync(&self, l: Level) -> int; }
// Call o.fsync after executing blk // Call o.fsync after executing blk
pub fn obj_sync(o: FSyncable, opt_level: Option<Level>, pub fn obj_sync(o: FSyncable, opt_level: Option<Level>,

View file

@ -646,19 +646,19 @@ mod util {
} }
impl BufReader: Reader { impl BufReader: Reader {
fn read(bytes: &[mut u8], len: uint) -> uint { fn read(&self, bytes: &[mut u8], len: uint) -> uint {
self.as_bytes_reader(|r| r.read(bytes, len) ) self.as_bytes_reader(|r| r.read(bytes, len) )
} }
fn read_byte() -> int { fn read_byte(&self) -> int {
self.as_bytes_reader(|r| r.read_byte() ) self.as_bytes_reader(|r| r.read_byte() )
} }
fn eof() -> bool { fn eof(&self) -> bool {
self.as_bytes_reader(|r| r.eof() ) self.as_bytes_reader(|r| r.eof() )
} }
fn seek(offset: int, whence: io::SeekStyle) { fn seek(&self, offset: int, whence: io::SeekStyle) {
self.as_bytes_reader(|r| r.seek(offset, whence) ) self.as_bytes_reader(|r| r.seek(offset, whence) )
} }
fn tell() -> uint { fn tell(&self) -> uint {
self.as_bytes_reader(|r| r.tell() ) self.as_bytes_reader(|r| r.tell() )
} }
} }

View file

@ -788,7 +788,7 @@ impl TcpSocket {
/// Implementation of `io::reader` trait for a buffered `net::tcp::tcp_socket` /// Implementation of `io::reader` trait for a buffered `net::tcp::tcp_socket`
impl TcpSocketBuf: io::Reader { impl TcpSocketBuf: io::Reader {
fn read(buf: &[mut u8], len: uint) -> uint { fn read(&self, buf: &[mut u8], len: uint) -> uint {
// Loop until our buffer has enough data in it for us to read from. // Loop until our buffer has enough data in it for us to read from.
while self.data.buf.len() < len { while self.data.buf.len() < len {
let read_result = read(&self.data.sock, 0u); let read_result = read(&self.data.sock, 0u);
@ -821,7 +821,7 @@ impl TcpSocketBuf: io::Reader {
count count
} }
fn read_byte() -> int { fn read_byte(&self) -> int {
let mut bytes = ~[0]; let mut bytes = ~[0];
if self.read(bytes, 1u) == 0 { if self.read(bytes, 1u) == 0 {
if self.end_of_stream { if self.end_of_stream {
@ -833,21 +833,21 @@ impl TcpSocketBuf: io::Reader {
bytes[0] as int bytes[0] as int
} }
} }
fn eof() -> bool { fn eof(&self) -> bool {
self.end_of_stream self.end_of_stream
} }
fn seek(dist: int, seek: io::SeekStyle) { fn seek(&self, dist: int, seek: io::SeekStyle) {
log(debug, fmt!("tcp_socket_buf seek stub %? %?", dist, seek)); log(debug, fmt!("tcp_socket_buf seek stub %? %?", dist, seek));
// noop // noop
} }
fn tell() -> uint { fn tell(&self) -> uint {
0u // noop 0u // noop
} }
} }
/// Implementation of `io::reader` trait for a buffered `net::tcp::tcp_socket` /// Implementation of `io::reader` trait for a buffered `net::tcp::tcp_socket`
impl TcpSocketBuf: io::Writer { impl TcpSocketBuf: io::Writer {
pub fn write(data: &[const u8]) unsafe { pub fn write(&self, data: &[const u8]) unsafe {
let socket_data_ptr = let socket_data_ptr =
ptr::addr_of(&(*((*(self.data)).sock).socket_data)); ptr::addr_of(&(*((*(self.data)).sock).socket_data));
let w_result = write_common_impl(socket_data_ptr, let w_result = write_common_impl(socket_data_ptr,
@ -858,17 +858,17 @@ impl TcpSocketBuf: io::Writer {
err_data.err_name, err_data.err_msg)); err_data.err_name, err_data.err_msg));
} }
} }
fn seek(dist: int, seek: io::SeekStyle) { fn seek(&self, dist: int, seek: io::SeekStyle) {
log(debug, fmt!("tcp_socket_buf seek stub %? %?", dist, seek)); log(debug, fmt!("tcp_socket_buf seek stub %? %?", dist, seek));
// noop // noop
} }
fn tell() -> uint { fn tell(&self) -> uint {
0u 0u
} }
fn flush() -> int { fn flush(&self) -> int {
0 0
} }
fn get_type() -> io::WriterType { fn get_type(&self) -> io::WriterType {
io::File io::File
} }
} }