Skip to content

Commit 4db1e83

Browse files
committed
VecDeque::read_to_string: avoid making the slices contiguous
1 parent 33262fb commit 4db1e83

File tree

1 file changed

+2
-11
lines changed

1 file changed

+2
-11
lines changed

library/std/src/io/impls.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -488,17 +488,8 @@ impl<A: Allocator> Read for VecDeque<u8, A> {
488488

489489
#[inline]
490490
fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
491-
// We have to use a single contiguous slice because the `VecDequeue` might be split in the
492-
// middle of an UTF-8 character.
493-
let len = self.len();
494-
let content = self.make_contiguous();
495-
let string = str::from_utf8(content).map_err(|_| {
496-
io::const_io_error!(ErrorKind::InvalidData, "stream did not contain valid UTF-8")
497-
})?;
498-
buf.try_reserve(len)?;
499-
buf.push_str(string);
500-
self.clear();
501-
Ok(len)
491+
// SAFETY: We only append to the buffer
492+
unsafe { io::append_to_string(buf, |buf| self.read_to_end(buf)) }
502493
}
503494
}
504495

0 commit comments

Comments
 (0)