Skip to content

Commit

Permalink
auto merge of #12907 : alexcrichton/rust/issue-12892, r=brson
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors committed Mar 22, 2014
2 parents 993dee4 + 811257e commit 092afdb
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
22 changes: 11 additions & 11 deletions src/libstd/io/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ mod test {
#[test]
fn read_bytes() {
let mut reader = MemReader::new(~[10, 11, 12, 13]);
let bytes = reader.read_bytes(4).unwrap();
let bytes = reader.read_exact(4).unwrap();
assert!(bytes == ~[10, 11, 12, 13]);
}

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

#[test]
fn read_bytes_eof() {
let mut reader = MemReader::new(~[10, 11]);
assert!(reader.read_bytes(4).is_err());
assert!(reader.read_exact(4).is_err());
}

#[test]
fn push_bytes() {
fn push_exact() {
let mut reader = MemReader::new(~[10, 11, 12, 13]);
let mut buf = ~[8, 9];
reader.push_bytes(&mut buf, 4).unwrap();
reader.push_exact(&mut buf, 4).unwrap();
assert!(buf == ~[8, 9, 10, 11, 12, 13]);
}

#[test]
fn push_bytes_partial() {
fn push_exact_partial() {
let mut reader = PartialReader {
count: 0,
};
let mut buf = ~[8, 9];
reader.push_bytes(&mut buf, 4).unwrap();
reader.push_exact(&mut buf, 4).unwrap();
assert!(buf == ~[8, 9, 10, 11, 12, 13]);
}

#[test]
fn push_bytes_eof() {
fn push_exact_eof() {
let mut reader = MemReader::new(~[10, 11]);
let mut buf = ~[8, 9];
assert!(reader.push_bytes(&mut buf, 4).is_err());
assert!(reader.push_exact(&mut buf, 4).is_err());
assert!(buf == ~[8, 9, 10, 11]);
}

#[test]
fn push_bytes_error() {
fn push_exact_error() {
let mut reader = ErroringLaterReader {
count: 0,
};
let mut buf = ~[8, 9];
assert!(reader.push_bytes(&mut buf, 4).is_err());
assert!(reader.push_exact(&mut buf, 4).is_err());
assert!(buf == ~[8, 9, 10]);
}

Expand Down
15 changes: 8 additions & 7 deletions src/libstd/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,13 +360,13 @@ pub trait Reader {
}
}

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

/// Reads `len` bytes and gives you back a new vector of length `len`
/// Reads exactly `len` bytes and gives you back a new vector of length
/// `len`
///
/// # Error
///
/// Fails with the same conditions as `read`. Additionally returns error
/// on EOF. Note that if an error is returned, then some number of bytes may
/// have already been consumed from the underlying reader, and they are lost
/// (not returned as part of the error). If this is unacceptable, then it is
/// recommended to use the `push_bytes` or `read` methods.
fn read_bytes(&mut self, len: uint) -> IoResult<~[u8]> {
/// recommended to use the `push_exact` or `read` methods.
fn read_exact(&mut self, len: uint) -> IoResult<~[u8]> {
let mut buf = slice::with_capacity(len);
match self.push_bytes(&mut buf, len) {
match self.push_exact(&mut buf, len) {
Ok(()) => Ok(buf),
Err(e) => Err(e),
}
Expand All @@ -424,7 +425,7 @@ pub trait Reader {
fn read_to_end(&mut self) -> IoResult<~[u8]> {
let mut buf = slice::with_capacity(DEFAULT_BUF_SIZE);
loop {
match self.push_bytes(&mut buf, DEFAULT_BUF_SIZE) {
match self.push_exact(&mut buf, DEFAULT_BUF_SIZE) {
Ok(()) => {}
Err(ref e) if e.kind == EndOfFile => break,
Err(e) => return Err(e)
Expand Down
4 changes: 2 additions & 2 deletions src/libterm/terminfo/parser/compiled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ pub fn parse(file: &mut io::Reader,
}

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

let string_table = try!(file.read_bytes(string_table_bytes as uint));
let string_table = try!(file.read_exact(string_table_bytes as uint));

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

0 comments on commit 092afdb

Please sign in to comment.