Skip to content

Commit

Permalink
AsciiStreamReader - changed read_bytes to truncate 'buf' to match the…
Browse files Browse the repository at this point in the history
… data read.
  • Loading branch information
cubicle-jockey committed Jul 7, 2023
1 parent 13bcd17 commit 91f311d
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/ascii_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,13 @@ impl<R: Read> AsciiStreamReader<R> {
let mut vec = Vec::with_capacity(len);
vec.resize(len, 0);
let result = self.inner.read(&mut vec);
if result.is_ok() {
if let Ok(result) = result {
vec.truncate(result);
*buf = AsciiString::from(vec);
Ok(result)
} else {
result
}
result
}
}

Expand Down Expand Up @@ -289,6 +292,24 @@ mod test {
assert!(r.is_eof());
}

#[test]
fn test_ascii_stream_reader_bytes() {
use super::*;

let mut stream = AsciiStreamReader::new(Cursor::new(
b"This is test1\nThis is test2\r\nThis is test3",
));
let mut buf = AsciiString::new();
stream.read_bytes(&mut buf, 14);
assert_eq!(buf.to_string(), "This is test1\n");
stream.read_bytes(&mut buf, 15);
assert_eq!(buf.to_string(), "This is test2\r\n");
stream.read_bytes(&mut buf, 13);
assert_eq!(buf.to_string(), "This is test3");
let r = stream.read_bytes(&mut buf, 14);
assert_eq!(buf.to_string(), "");
}

#[test]
fn test_ascii_stream_writer() {
use super::*;
Expand Down

0 comments on commit 91f311d

Please sign in to comment.