Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
a1phyr committed May 20, 2024
1 parent cfb0479 commit a197ff3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion library/std/src/io/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ where

match result {
Ok(_) => self.pos += buf.len() as u64,
// The only posible error condition is EOF
// The only possible error condition is EOF, so place the cursor at "EOF"
Err(_) => self.pos = self.inner.as_ref().len() as u64,
}

Expand Down
32 changes: 32 additions & 0 deletions library/std/src/io/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,38 @@ fn test_take_wrong_length() {
let _ = reader.read(&mut buffer[..]);
}

#[test]
fn slice_read_exact_eof() {
let slice = &b"123456"[..];

let mut r = slice;
assert!(r.read_exact(&mut [0; 10]).is_err());
assert!(r.is_empty());

let mut r = slice;
let buf = &mut [0; 10];
let mut buf = BorrowedBuf::from(buf.as_mut_slice());
assert!(r.read_buf_exact(buf.unfilled()).is_err());
assert!(r.is_empty());
assert_eq!(buf.filled(), b"123456");
}

#[test]
fn cursor_read_exact_eof() {
let slice = Cursor::new(b"123456");

let mut r = slice.clone();
assert!(r.read_exact(&mut [0; 10]).is_err());
assert!(r.is_empty());

let mut r = slice;
let buf = &mut [0; 10];
let mut buf = BorrowedBuf::from(buf.as_mut_slice());
assert!(r.read_buf_exact(buf.unfilled()).is_err());
assert!(r.is_empty());
assert_eq!(buf.filled(), b"123456");
}

#[bench]
fn bench_take_read(b: &mut test::Bencher) {
b.iter(|| {
Expand Down

0 comments on commit a197ff3

Please sign in to comment.