Skip to content

Commit

Permalink
VecDeque::read_to_string: avoid making the slices contiguous
Browse files Browse the repository at this point in the history
  • Loading branch information
a1phyr committed Mar 26, 2024
1 parent 809e69b commit 576a0ba
Showing 1 changed file with 2 additions and 11 deletions.
13 changes: 2 additions & 11 deletions library/std/src/io/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,17 +464,8 @@ impl<A: Allocator> Read for VecDeque<u8, A> {

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

Expand Down

0 comments on commit 576a0ba

Please sign in to comment.