Skip to content

Commit ebc5970

Browse files
committed
Add tests and comments about read_to_string and read_line specializations
1 parent cba6e10 commit ebc5970

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

Diff for: library/std/src/io/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -2405,6 +2405,9 @@ impl<T: Read, U: Read> Read for Chain<T, U> {
24052405
Ok(read)
24062406
}
24072407

2408+
// We don't override `read_to_string` here because an UTF-8 sequence could
2409+
// be split between the two parts of the chain
2410+
24082411
fn read_buf(&mut self, mut buf: BorrowedCursor<'_>) -> Result<()> {
24092412
if buf.capacity() == 0 {
24102413
return Ok(());
@@ -2454,6 +2457,9 @@ impl<T: BufRead, U: BufRead> BufRead for Chain<T, U> {
24542457
read += self.second.read_until(byte, buf)?;
24552458
Ok(read)
24562459
}
2460+
2461+
// We don't override `read_line` here because an UTF-8 sequence could be
2462+
// split between the two parts of the chain
24572463
}
24582464

24592465
impl<T, U> SizeHint for Chain<T, U> {

Diff for: library/std/src/io/tests.rs

+11
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,17 @@ fn chain_bufread() {
231231
cmp_bufread(chain1, chain2, &testdata[..]);
232232
}
233233

234+
#[test]
235+
fn chain_splitted_char() {
236+
let chain = b"\xc3".chain(b"\xa9".as_slice());
237+
assert_eq!(crate::io::read_to_string(chain).unwrap(), "é");
238+
239+
let mut chain = b"\xc3".chain(b"\xa9\n".as_slice());
240+
let mut buf = String::new();
241+
assert_eq!(chain.read_line(&mut buf).unwrap(), 3);
242+
assert_eq!(buf, \n");
243+
}
244+
234245
#[test]
235246
fn bufreader_size_hint() {
236247
let testdata = b"ABCDEFGHIJKL";

0 commit comments

Comments
 (0)