Skip to content

Commit 141ef23

Browse files
committed
Merge pull request #3805 from erickt/incoming
Variety of small cleanups
2 parents e28a161 + fe41cce commit 141ef23

File tree

15 files changed

+102
-103
lines changed

15 files changed

+102
-103
lines changed

src/libcore/dvec.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub enum DVec<A> {
5656
}
5757

5858
/// Creates a new, empty dvec
59-
pub fn DVec<A>() -> DVec<A> {
59+
pub pure fn DVec<A>() -> DVec<A> {
6060
DVec_({mut data: ~[]})
6161
}
6262

src/libcore/extfmt.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -329,11 +329,11 @@ pub mod rt {
329329
// For strings, precision is the maximum characters
330330
// displayed
331331
let mut unpadded = match cv.precision {
332-
CountImplied => s.to_unique(),
332+
CountImplied => s.to_owned(),
333333
CountIs(max) => if max as uint < str::char_len(s) {
334334
str::substr(s, 0u, max as uint)
335335
} else {
336-
s.to_unique()
336+
s.to_owned()
337337
}
338338
};
339339
return unsafe { pad(cv, move unpadded, PadNozero) };

src/libcore/io.rs

+76-66
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,8 +119,8 @@ impl<T: Reader> T : ReaderUtil {
119119
}
120120
return (i, 0);
121121
}
122-
let mut buf: ~[u8] = ~[];
123-
let mut chars: ~[char] = ~[];
122+
let mut bytes = ~[];
123+
let mut chars = ~[];
124124
// might need more bytes, but reading n will never over-read
125125
let mut nbread = n;
126126
while nbread > 0 {
@@ -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() }
@@ -297,39 +299,41 @@ pub fn file_reader(path: &Path) -> Result<Reader, ~str> {
297299
}
298300
299301
300-
// Byte buffer readers
301-
302-
pub type ByteBuf = {buf: &[const u8], mut pos: uint};
302+
// Byte readers
303+
pub struct BytesReader {
304+
bytes: &[u8],
305+
mut pos: uint
306+
}
303307
304-
impl ByteBuf: Reader {
305-
fn read(buf: &[mut u8], len: uint) -> uint {
306-
let count = uint::min(len, self.buf.len() - self.pos);
308+
impl BytesReader: Reader {
309+
fn read(bytes: &[mut u8], len: uint) -> uint {
310+
let count = uint::min(len, self.bytes.len() - self.pos);
307311
308-
let view = vec::const_view(self.buf, self.pos, self.buf.len());
309-
vec::bytes::memcpy(buf, view, count);
312+
let view = vec::view(self.bytes, self.pos, self.bytes.len());
313+
vec::bytes::memcpy(bytes, view, count);
310314
311315
self.pos += count;
312316
313317
count
314318
}
315319
fn read_byte() -> int {
316-
if self.pos == self.buf.len() { return -1; }
317-
let b = self.buf[self.pos];
320+
if self.pos == self.bytes.len() { return -1; }
321+
let b = self.bytes[self.pos];
318322
self.pos += 1u;
319323
return b as int;
320324
}
321325
// FIXME (#2738): implement this
322326
fn unread_byte(_byte: int) { error!("Unimplemented: unread_byte"); fail; }
323-
fn eof() -> bool { self.pos == self.buf.len() }
327+
fn eof() -> bool { self.pos == self.bytes.len() }
324328
fn seek(offset: int, whence: SeekStyle) {
325329
let pos = self.pos;
326-
self.pos = seek_in_buf(offset, pos, self.buf.len(), whence);
330+
self.pos = seek_in_buf(offset, pos, self.bytes.len(), whence);
327331
}
328332
fn tell() -> uint { self.pos }
329333
}
330334

331-
pub fn with_bytes_reader<t>(bytes: &[u8], f: fn(Reader) -> t) -> t {
332-
f({buf: bytes, mut pos: 0u} as Reader)
335+
pub pure fn with_bytes_reader<t>(bytes: &[u8], f: fn(Reader) -> t) -> t {
336+
f(BytesReader { bytes: bytes, pos: 0u } as Reader)
333337
}
334338

335339
pub fn with_str_reader<T>(s: &str, f: fn(Reader) -> T) -> T {
@@ -602,10 +606,10 @@ impl<T: Writer> T : WriterUtil {
602606
self.write_str(&"\n");
603607
}
604608
fn write_int(n: int) {
605-
int::to_str_bytes(n, 10u, |buf| self.write(buf))
609+
int::to_str_bytes(n, 10u, |bytes| self.write(bytes))
606610
}
607611
fn write_uint(n: uint) {
608-
uint::to_str_bytes(false, n, 10u, |buf| self.write(buf))
612+
uint::to_str_bytes(false, n, 10u, |bytes| self.write(bytes))
609613
}
610614
fn write_le_uint(n: uint) {
611615
u64_to_le_bytes(n as u64, uint::bytes, |v| self.write(v))
@@ -687,34 +691,34 @@ pub fn print(s: &str) { stdout().write_str(s); }
687691
pub fn println(s: &str) { stdout().write_line(s); }
688692
689693
pub struct BytesWriter {
690-
buf: DVec<u8>,
694+
bytes: DVec<u8>,
691695
mut pos: uint,
692696
}
693697
694698
impl BytesWriter: Writer {
695699
fn write(v: &[const u8]) {
696-
do self.buf.swap |buf| {
697-
let mut buf <- buf;
700+
do self.bytes.swap |bytes| {
701+
let mut bytes <- bytes;
698702
let v_len = v.len();
699-
let buf_len = buf.len();
703+
let bytes_len = bytes.len();
700704
701-
let count = uint::max(buf_len, self.pos + v_len);
702-
vec::reserve(&mut buf, count);
703-
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); }
704708
705709
{
706-
let view = vec::mut_view(buf, self.pos, count);
710+
let view = vec::mut_view(bytes, self.pos, count);
707711
vec::bytes::memcpy(view, v, v_len);
708712
}
709713
710714
self.pos += v_len;
711715
712-
move buf
716+
move bytes
713717
}
714718
}
715719
fn seek(offset: int, whence: SeekStyle) {
716720
let pos = self.pos;
717-
let len = self.buf.len();
721+
let len = self.bytes.len();
718722
self.pos = seek_in_buf(offset, pos, len, whence);
719723
}
720724
fn tell() -> uint { self.pos }
@@ -730,21 +734,25 @@ impl @BytesWriter : Writer {
730734
fn get_type() -> WriterType { (*self).get_type() }
731735
}
732736
733-
pub fn BytesWriter() -> BytesWriter {
734-
BytesWriter { buf: DVec(), mut pos: 0u }
737+
pub pure fn BytesWriter() -> BytesWriter {
738+
BytesWriter { bytes: DVec(), mut pos: 0u }
735739
}
736740
737-
pub fn with_bytes_writer(f: fn(Writer)) -> ~[u8] {
741+
pub pure fn with_bytes_writer(f: fn(Writer)) -> ~[u8] {
738742
let wr = @BytesWriter();
739743
f(wr as Writer);
740-
wr.buf.check_out(|buf| move buf)
744+
// FIXME (#3758): This should not be needed.
745+
unsafe { wr.bytes.check_out(|bytes| move bytes) }
741746
}
742747
743-
pub fn with_str_writer(f: fn(Writer)) -> ~str {
748+
pub pure fn with_str_writer(f: fn(Writer)) -> ~str {
744749
let mut v = with_bytes_writer(f);
745750
746-
// Make sure the vector has a trailing null and is proper utf8.
747-
v.push(0);
751+
// FIXME (#3758): This should not be needed.
752+
unsafe {
753+
// Make sure the vector has a trailing null and is proper utf8.
754+
v.push(0);
755+
}
748756
assert str::is_utf8(v);
749757
750758
unsafe { move ::cast::transmute(move v) }
@@ -975,15 +983,17 @@ mod tests {
975983
fn bytes_buffer_overwrite() {
976984
let wr = BytesWriter();
977985
wr.write(~[0u8, 1u8, 2u8, 3u8]);
978-
assert wr.buf.borrow(|buf| buf == ~[0u8, 1u8, 2u8, 3u8]);
986+
assert wr.bytes.borrow(|bytes| bytes == ~[0u8, 1u8, 2u8, 3u8]);
979987
wr.seek(-2, SeekCur);
980988
wr.write(~[4u8, 5u8, 6u8, 7u8]);
981-
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]);
982991
wr.seek(-2, SeekEnd);
983992
wr.write(~[8u8]);
984993
wr.seek(1, SeekSet);
985994
wr.write(~[9u8]);
986-
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]);
987997
}
988998
}
989999

src/libcore/logging.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub fn console_off() {
3232
#[cfg(notest)]
3333
#[lang="log_type"]
3434
pub fn log_type<T>(level: u32, object: &T) {
35-
let bytes = do io::with_bytes_writer() |writer| {
35+
let bytes = do io::with_bytes_writer |writer| {
3636
repr::write_repr(writer, object);
3737
};
3838
unsafe {

src/libcore/option.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ pub fn swap_unwrap<T>(opt: &mut Option<T>) -> T {
225225

226226
pub pure fn unwrap_expect<T>(opt: Option<T>, reason: &str) -> T {
227227
//! As unwrap, but with a specified failure message.
228-
if opt.is_none() { fail reason.to_unique(); }
228+
if opt.is_none() { fail reason.to_owned(); }
229229
unwrap(move opt)
230230
}
231231

src/libcore/repr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ impl ReprPrinter {
559559
unsafe {
560560
self.align(sys::min_align_of::<T>());
561561
let value_addr: &T = transmute(copy self.ptr);
562-
(*value_addr).write_repr(self.writer);
562+
value_addr.write_repr(self.writer);
563563
self.bump(sys::size_of::<T>());
564564
true
565565
}

src/libcore/str.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -2135,7 +2135,7 @@ pub trait StrSlice {
21352135
pure fn trim() -> ~str;
21362136
pure fn trim_left() -> ~str;
21372137
pure fn trim_right() -> ~str;
2138-
pure fn to_unique() -> ~str;
2138+
pure fn to_owned() -> ~str;
21392139
pure fn to_managed() -> @str;
21402140
pure fn char_at(i: uint) -> char;
21412141
}
@@ -2258,13 +2258,12 @@ impl &str: StrSlice {
22582258
pure fn trim_right() -> ~str { trim_right(self) }
22592259

22602260
#[inline]
2261-
pure fn to_unique() -> ~str { self.slice(0, self.len()) }
2261+
pure fn to_owned() -> ~str { self.slice(0, self.len()) }
22622262

22632263
#[inline]
22642264
pure fn to_managed() -> @str {
2265-
let v = at_vec::from_fn(self.len() + 1, |i| {
2266-
if i == self.len() { 0 } else { self[i] }
2267-
});
2265+
let bytes = as_bytes_slice(self);
2266+
let v = at_vec::from_fn(bytes.len(), |i| bytes[i]);
22682267
unsafe { ::cast::transmute(v) }
22692268
}
22702269

0 commit comments

Comments
 (0)