Skip to content

Commit 092afdb

Browse files
committed
auto merge of #12907 : alexcrichton/rust/issue-12892, r=brson
These methods can be mistaken for general "read some bytes" utilities when they're actually only meant for reading an exact number of bytes. By renaming them it's much clearer about what they're doing without having to read the documentation. Closes #12892
2 parents 993dee4 + 811257e commit 092afdb

File tree

3 files changed

+21
-20
lines changed

3 files changed

+21
-20
lines changed

src/libstd/io/extensions.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ mod test {
283283
#[test]
284284
fn read_bytes() {
285285
let mut reader = MemReader::new(~[10, 11, 12, 13]);
286-
let bytes = reader.read_bytes(4).unwrap();
286+
let bytes = reader.read_exact(4).unwrap();
287287
assert!(bytes == ~[10, 11, 12, 13]);
288288
}
289289

@@ -292,49 +292,49 @@ mod test {
292292
let mut reader = PartialReader {
293293
count: 0,
294294
};
295-
let bytes = reader.read_bytes(4).unwrap();
295+
let bytes = reader.read_exact(4).unwrap();
296296
assert!(bytes == ~[10, 11, 12, 13]);
297297
}
298298

299299
#[test]
300300
fn read_bytes_eof() {
301301
let mut reader = MemReader::new(~[10, 11]);
302-
assert!(reader.read_bytes(4).is_err());
302+
assert!(reader.read_exact(4).is_err());
303303
}
304304

305305
#[test]
306-
fn push_bytes() {
306+
fn push_exact() {
307307
let mut reader = MemReader::new(~[10, 11, 12, 13]);
308308
let mut buf = ~[8, 9];
309-
reader.push_bytes(&mut buf, 4).unwrap();
309+
reader.push_exact(&mut buf, 4).unwrap();
310310
assert!(buf == ~[8, 9, 10, 11, 12, 13]);
311311
}
312312

313313
#[test]
314-
fn push_bytes_partial() {
314+
fn push_exact_partial() {
315315
let mut reader = PartialReader {
316316
count: 0,
317317
};
318318
let mut buf = ~[8, 9];
319-
reader.push_bytes(&mut buf, 4).unwrap();
319+
reader.push_exact(&mut buf, 4).unwrap();
320320
assert!(buf == ~[8, 9, 10, 11, 12, 13]);
321321
}
322322

323323
#[test]
324-
fn push_bytes_eof() {
324+
fn push_exact_eof() {
325325
let mut reader = MemReader::new(~[10, 11]);
326326
let mut buf = ~[8, 9];
327-
assert!(reader.push_bytes(&mut buf, 4).is_err());
327+
assert!(reader.push_exact(&mut buf, 4).is_err());
328328
assert!(buf == ~[8, 9, 10, 11]);
329329
}
330330

331331
#[test]
332-
fn push_bytes_error() {
332+
fn push_exact_error() {
333333
let mut reader = ErroringLaterReader {
334334
count: 0,
335335
};
336336
let mut buf = ~[8, 9];
337-
assert!(reader.push_bytes(&mut buf, 4).is_err());
337+
assert!(reader.push_exact(&mut buf, 4).is_err());
338338
assert!(buf == ~[8, 9, 10]);
339339
}
340340

src/libstd/io/mod.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -360,13 +360,13 @@ pub trait Reader {
360360
}
361361
}
362362

363-
/// Reads `len` bytes and appends them to a vector.
363+
/// Reads exactly `len` bytes and appends them to a vector.
364364
///
365365
/// May push fewer than the requested number of bytes on error
366366
/// or EOF. If `Ok(())` is returned, then all of the requested bytes were
367367
/// pushed on to the vector, otherwise the amount `len` bytes couldn't be
368368
/// read (an error was encountered), and the error is returned.
369-
fn push_bytes(&mut self, buf: &mut ~[u8], len: uint) -> IoResult<()> {
369+
fn push_exact(&mut self, buf: &mut ~[u8], len: uint) -> IoResult<()> {
370370
struct State<'a> {
371371
buf: &'a mut ~[u8],
372372
total_read: uint
@@ -396,18 +396,19 @@ pub trait Reader {
396396
|s| unsafe { s.buf.set_len(start_len + s.total_read) })
397397
}
398398

399-
/// Reads `len` bytes and gives you back a new vector of length `len`
399+
/// Reads exactly `len` bytes and gives you back a new vector of length
400+
/// `len`
400401
///
401402
/// # Error
402403
///
403404
/// Fails with the same conditions as `read`. Additionally returns error
404405
/// on EOF. Note that if an error is returned, then some number of bytes may
405406
/// have already been consumed from the underlying reader, and they are lost
406407
/// (not returned as part of the error). If this is unacceptable, then it is
407-
/// recommended to use the `push_bytes` or `read` methods.
408-
fn read_bytes(&mut self, len: uint) -> IoResult<~[u8]> {
408+
/// recommended to use the `push_exact` or `read` methods.
409+
fn read_exact(&mut self, len: uint) -> IoResult<~[u8]> {
409410
let mut buf = slice::with_capacity(len);
410-
match self.push_bytes(&mut buf, len) {
411+
match self.push_exact(&mut buf, len) {
411412
Ok(()) => Ok(buf),
412413
Err(e) => Err(e),
413414
}
@@ -424,7 +425,7 @@ pub trait Reader {
424425
fn read_to_end(&mut self) -> IoResult<~[u8]> {
425426
let mut buf = slice::with_capacity(DEFAULT_BUF_SIZE);
426427
loop {
427-
match self.push_bytes(&mut buf, DEFAULT_BUF_SIZE) {
428+
match self.push_exact(&mut buf, DEFAULT_BUF_SIZE) {
428429
Ok(()) => {}
429430
Err(ref e) if e.kind == EndOfFile => break,
430431
Err(e) => return Err(e)

src/libterm/terminfo/parser/compiled.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ pub fn parse(file: &mut io::Reader,
207207
}
208208

209209
// don't read NUL
210-
let bytes = try!(file.read_bytes(names_bytes as uint - 1));
210+
let bytes = try!(file.read_exact(names_bytes as uint - 1));
211211
let names_str = match str::from_utf8_owned(bytes) {
212212
Some(s) => s, None => return Err(~"input not utf-8"),
213213
};
@@ -250,7 +250,7 @@ pub fn parse(file: &mut io::Reader,
250250
string_offsets.push(try!(file.read_le_u16()));
251251
}
252252

253-
let string_table = try!(file.read_bytes(string_table_bytes as uint));
253+
let string_table = try!(file.read_exact(string_table_bytes as uint));
254254

255255
if string_table.len() != string_table_bytes as uint {
256256
return Err(~"error: hit EOF before end of string table");

0 commit comments

Comments
 (0)