std: Rename {push,read}_bytes to {push,read}_exact
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
This commit is contained in:
parent
6eae7df43c
commit
811257eda5
src
@ -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]);
|
||||
}
|
||||
|
||||
@ -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]);
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
@ -396,7 +396,8 @@ 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
|
||||
///
|
||||
@ -404,10 +405,10 @@ pub trait Reader {
|
||||
/// 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),
|
||||
}
|
||||
@ -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)
|
||||
|
@ -208,7 +208,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"),
|
||||
};
|
||||
@ -251,7 +251,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");
|
||||
|
Loading…
x
Reference in New Issue
Block a user