Skip to content

Commit 95423d2

Browse files
committed
libcore: call [u8] values bytes, not bufs
1 parent e3b1471 commit 95423d2

File tree

2 files changed

+52
-48
lines changed

2 files changed

+52
-48
lines changed

src/libcore/io.rs

Lines changed: 50 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub trait Reader {
3737
// FIXME (#2004): Seekable really should be orthogonal.
3838

3939
// FIXME (#2982): This should probably return an error.
40-
fn read(buf: &[mut u8], len: uint) -> uint;
40+
fn read(bytes: &[mut u8], len: uint) -> uint;
4141
fn read_byte() -> int;
4242
fn unread_byte(int);
4343
fn eof() -> bool;
@@ -65,32 +65,32 @@ pub trait ReaderUtil {
6565

6666
impl<T: Reader> T : ReaderUtil {
6767
fn read_bytes(len: uint) -> ~[u8] {
68-
let mut buf = vec::with_capacity(len);
69-
unsafe { vec::raw::set_len(&mut buf, len); }
68+
let mut bytes = vec::with_capacity(len);
69+
unsafe { vec::raw::set_len(&mut bytes, len); }
7070

71-
let count = self.read(buf, len);
71+
let count = self.read(bytes, len);
7272

73-
unsafe { vec::raw::set_len(&mut buf, count); }
74-
move buf
73+
unsafe { vec::raw::set_len(&mut bytes, count); }
74+
move bytes
7575
}
7676
fn read_line() -> ~str {
77-
let mut buf = ~[];
77+
let mut bytes = ~[];
7878
loop {
7979
let ch = self.read_byte();
8080
if ch == -1 || ch == 10 { break; }
81-
buf.push(ch as u8);
81+
bytes.push(ch as u8);
8282
}
83-
str::from_bytes(buf)
83+
str::from_bytes(bytes)
8484
}
8585

8686
fn read_chars(n: uint) -> ~[char] {
8787
// returns the (consumed offset, n_req), appends characters to &chars
88-
fn chars_from_bytes<T: Reader>(buf: &~[u8], chars: &mut ~[char])
88+
fn chars_from_bytes<T: Reader>(bytes: &~[u8], chars: &mut ~[char])
8989
-> (uint, uint) {
9090
let mut i = 0;
91-
let buf_len = buf.len();
92-
while i < buf_len {
93-
let b0 = buf[i];
91+
let bytes_len = bytes.len();
92+
while i < bytes_len {
93+
let b0 = bytes[i];
9494
let w = str::utf8_char_width(b0);
9595
let end = i + w;
9696
i += 1;
@@ -100,12 +100,12 @@ impl<T: Reader> T : ReaderUtil {
100100
loop;
101101
}
102102
// can't satisfy this char with the existing data
103-
if end > buf_len {
104-
return (i - 1, end - buf_len);
103+
if end > bytes_len {
104+
return (i - 1, end - bytes_len);
105105
}
106106
let mut val = 0;
107107
while i < end {
108-
let next = buf[i] as int;
108+
let next = bytes[i] as int;
109109
i += 1;
110110
assert (next > -1);
111111
assert (next & 192 == 128);
@@ -119,7 +119,7 @@ impl<T: Reader> T : ReaderUtil {
119119
}
120120
return (i, 0);
121121
}
122-
let mut buf: ~[u8] = ~[];
122+
let mut bytes: ~[u8] = ~[];
123123
let mut chars: ~[char] = ~[];
124124
// might need more bytes, but reading n will never over-read
125125
let mut nbread = n;
@@ -130,15 +130,15 @@ impl<T: Reader> T : ReaderUtil {
130130
// we're split in a unicode char?
131131
break;
132132
}
133-
buf.push_all(data);
134-
let (offset, nbreq) = chars_from_bytes::<T>(&buf, &mut chars);
133+
bytes.push_all(data);
134+
let (offset, nbreq) = chars_from_bytes::<T>(&bytes, &mut chars);
135135
let ncreq = n - chars.len();
136136
// again we either know we need a certain number of bytes
137137
// to complete a character, or we make sure we don't
138138
// over-read by reading 1-byte per char needed
139139
nbread = if ncreq > nbreq { ncreq } else { nbreq };
140140
if nbread > 0 {
141-
buf = vec::slice(buf, offset, buf.len());
141+
bytes = vec::slice(bytes, offset, bytes.len());
142142
}
143143
}
144144
move chars
@@ -154,12 +154,12 @@ impl<T: Reader> T : ReaderUtil {
154154
}
155155

156156
fn read_c_str() -> ~str {
157-
let mut buf: ~[u8] = ~[];
157+
let mut bytes: ~[u8] = ~[];
158158
loop {
159159
let ch = self.read_byte();
160-
if ch < 1 { break; } else { buf.push(ch as u8); }
160+
if ch < 1 { break; } else { bytes.push(ch as u8); }
161161
}
162-
str::from_bytes(buf)
162+
str::from_bytes(bytes)
163163
}
164164

165165
// FIXME deal with eof? // #2004
@@ -191,9 +191,9 @@ impl<T: Reader> T : ReaderUtil {
191191
}
192192

193193
fn read_whole_stream() -> ~[u8] {
194-
let mut buf: ~[u8] = ~[];
195-
while !self.eof() { buf.push_all(self.read_bytes(2048u)); }
196-
move buf
194+
let mut bytes: ~[u8] = ~[];
195+
while !self.eof() { bytes.push_all(self.read_bytes(2048u)); }
196+
move bytes
197197
}
198198

199199
fn each_byte(it: fn(int) -> bool) {
@@ -226,8 +226,8 @@ fn convert_whence(whence: SeekStyle) -> i32 {
226226
}
227227

228228
impl *libc::FILE: Reader {
229-
fn read(buf: &[mut u8], len: uint) -> uint {
230-
do vec::as_mut_buf(buf) |buf_p, buf_len| {
229+
fn read(bytes: &[mut u8], len: uint) -> uint {
230+
do vec::as_mut_buf(bytes) |buf_p, buf_len| {
231231
assert buf_len <= len;
232232

233233
let count = libc::fread(buf_p as *mut c_void, 1u as size_t,
@@ -250,7 +250,9 @@ impl *libc::FILE: Reader {
250250
// duration of its lifetime.
251251
// FIXME there really should be a better way to do this // #2004
252252
impl<T: Reader, C> {base: T, cleanup: C}: Reader {
253-
fn read(buf: &[mut u8], len: uint) -> uint { self.base.read(buf, len) }
253+
fn read(bytes: &[mut u8], len: uint) -> uint {
254+
self.base.read(bytes, len)
255+
}
254256
fn read_byte() -> int { self.base.read_byte() }
255257
fn unread_byte(byte: int) { self.base.unread_byte(byte); }
256258
fn eof() -> bool { self.base.eof() }
@@ -604,10 +606,10 @@ impl<T: Writer> T : WriterUtil {
604606
self.write_str(&"\n");
605607
}
606608
fn write_int(n: int) {
607-
int::to_str_bytes(n, 10u, |buf| self.write(buf))
609+
int::to_str_bytes(n, 10u, |bytes| self.write(bytes))
608610
}
609611
fn write_uint(n: uint) {
610-
uint::to_str_bytes(false, n, 10u, |buf| self.write(buf))
612+
uint::to_str_bytes(false, n, 10u, |bytes| self.write(bytes))
611613
}
612614
fn write_le_uint(n: uint) {
613615
u64_to_le_bytes(n as u64, uint::bytes, |v| self.write(v))
@@ -689,34 +691,34 @@ pub fn print(s: &str) { stdout().write_str(s); }
689691
pub fn println(s: &str) { stdout().write_line(s); }
690692
691693
pub struct BytesWriter {
692-
buf: DVec<u8>,
694+
bytes: DVec<u8>,
693695
mut pos: uint,
694696
}
695697
696698
impl BytesWriter: Writer {
697699
fn write(v: &[const u8]) {
698-
do self.buf.swap |buf| {
699-
let mut buf <- buf;
700+
do self.bytes.swap |bytes| {
701+
let mut bytes <- bytes;
700702
let v_len = v.len();
701-
let buf_len = buf.len();
703+
let bytes_len = bytes.len();
702704
703-
let count = uint::max(buf_len, self.pos + v_len);
704-
vec::reserve(&mut buf, count);
705-
unsafe { vec::raw::set_len(&mut buf, count); }
705+
let count = uint::max(bytes_len, self.pos + v_len);
706+
vec::reserve(&mut bytes, count);
707+
unsafe { vec::raw::set_len(&mut bytes, count); }
706708
707709
{
708-
let view = vec::mut_view(buf, self.pos, count);
710+
let view = vec::mut_view(bytes, self.pos, count);
709711
vec::bytes::memcpy(view, v, v_len);
710712
}
711713
712714
self.pos += v_len;
713715
714-
move buf
716+
move bytes
715717
}
716718
}
717719
fn seek(offset: int, whence: SeekStyle) {
718720
let pos = self.pos;
719-
let len = self.buf.len();
721+
let len = self.bytes.len();
720722
self.pos = seek_in_buf(offset, pos, len, whence);
721723
}
722724
fn tell() -> uint { self.pos }
@@ -733,14 +735,14 @@ impl @BytesWriter : Writer {
733735
}
734736
735737
pub pure fn BytesWriter() -> BytesWriter {
736-
BytesWriter { buf: DVec(), mut pos: 0u }
738+
BytesWriter { bytes: DVec(), mut pos: 0u }
737739
}
738740
739741
pub pure fn with_bytes_writer(f: fn(Writer)) -> ~[u8] {
740742
let wr = @BytesWriter();
741743
f(wr as Writer);
742744
// FIXME (#3758): This should not be needed.
743-
unsafe { wr.buf.check_out(|buf| move buf) }
745+
unsafe { wr.bytes.check_out(|bytes| move bytes) }
744746
}
745747
746748
pub pure fn with_str_writer(f: fn(Writer)) -> ~str {
@@ -981,15 +983,17 @@ mod tests {
981983
fn bytes_buffer_overwrite() {
982984
let wr = BytesWriter();
983985
wr.write(~[0u8, 1u8, 2u8, 3u8]);
984-
assert wr.buf.borrow(|buf| buf == ~[0u8, 1u8, 2u8, 3u8]);
986+
assert wr.bytes.borrow(|bytes| bytes == ~[0u8, 1u8, 2u8, 3u8]);
985987
wr.seek(-2, SeekCur);
986988
wr.write(~[4u8, 5u8, 6u8, 7u8]);
987-
assert wr.buf.borrow(|buf| buf == ~[0u8, 1u8, 4u8, 5u8, 6u8, 7u8]);
989+
assert wr.bytes.borrow(|bytes| bytes ==
990+
~[0u8, 1u8, 4u8, 5u8, 6u8, 7u8]);
988991
wr.seek(-2, SeekEnd);
989992
wr.write(~[8u8]);
990993
wr.seek(1, SeekSet);
991994
wr.write(~[9u8]);
992-
assert wr.buf.borrow(|buf| buf == ~[0u8, 9u8, 4u8, 5u8, 8u8, 7u8]);
995+
assert wr.bytes.borrow(|bytes| bytes ==
996+
~[0u8, 9u8, 4u8, 5u8, 8u8, 7u8]);
993997
}
994998
}
995999

src/rustc/metadata/encoder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,7 +1162,7 @@ fn encode_metadata(parms: encode_parms, crate: @crate) -> ~[u8] {
11621162

11631163
if (parms.tcx.sess.meta_stats()) {
11641164

1165-
do wr.buf.borrow |v| {
1165+
do wr.bytes.borrow |v| {
11661166
do v.each |e| {
11671167
if *e == 0 {
11681168
ecx.stats.zero_bytes += 1;
@@ -1195,7 +1195,7 @@ fn encode_metadata(parms: encode_parms, crate: @crate) -> ~[u8] {
11951195

11961196
(do str::as_bytes(&~"rust\x00\x00\x00\x01") |bytes| {
11971197
vec::slice(*bytes, 0, 8)
1198-
}) + flate::deflate_bytes(wr.buf.check_out(|buf| buf))
1198+
}) + flate::deflate_bytes(wr.bytes.check_out(|buf| buf))
11991199
}
12001200

12011201
// Get the encoded string for a type

0 commit comments

Comments
 (0)