Skip to content

Commit 3089f51

Browse files
committed
VecDeque::read_to_string: avoid making the slices contiguous
1 parent b015b32 commit 3089f51

File tree

1 file changed

+2
-9
lines changed

1 file changed

+2
-9
lines changed

std/src/io/impls.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -474,15 +474,8 @@ impl<A: Allocator> Read for VecDeque<u8, A> {
474474

475475
#[inline]
476476
fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
477-
// We have to use a single contiguous slice because the `VecDequeue` might be split in the
478-
// middle of an UTF-8 character.
479-
let len = self.len();
480-
let content = self.make_contiguous();
481-
let string = str::from_utf8(content).map_err(|_| io::Error::INVALID_UTF8)?;
482-
buf.try_reserve(len)?;
483-
buf.push_str(string);
484-
self.clear();
485-
Ok(len)
477+
// SAFETY: We only append to the buffer
478+
unsafe { io::append_to_string(buf, |buf| self.read_to_end(buf)) }
486479
}
487480
}
488481

0 commit comments

Comments
 (0)