Add tests and comments about read_to_string and read_line specializations

This commit is contained in:
Benoît du Garreau 2023-06-12 18:17:48 +02:00
parent cba6e102ec
commit ebc5970329
2 changed files with 17 additions and 0 deletions

View File

@ -2405,6 +2405,9 @@ fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize> {
Ok(read)
}
// We don't override `read_to_string` here because an UTF-8 sequence could
// be split between the two parts of the chain
fn read_buf(&mut self, mut buf: BorrowedCursor<'_>) -> Result<()> {
if buf.capacity() == 0 {
return Ok(());
@ -2454,6 +2457,9 @@ fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> Result<usize> {
read += self.second.read_until(byte, buf)?;
Ok(read)
}
// We don't override `read_line` here because an UTF-8 sequence could be
// split between the two parts of the chain
}
impl<T, U> SizeHint for Chain<T, U> {

View File

@ -231,6 +231,17 @@ fn chain_bufread() {
cmp_bufread(chain1, chain2, &testdata[..]);
}
#[test]
fn chain_splitted_char() {
let chain = b"\xc3".chain(b"\xa9".as_slice());
assert_eq!(crate::io::read_to_string(chain).unwrap(), "é");
let mut chain = b"\xc3".chain(b"\xa9\n".as_slice());
let mut buf = String::new();
assert_eq!(chain.read_line(&mut buf).unwrap(), 3);
assert_eq!(buf, "é\n");
}
#[test]
fn bufreader_size_hint() {
let testdata = b"ABCDEFGHIJKL";