1
Fork 0

VecDeque::read_to_string: avoid making the slices contiguous

This commit is contained in:
Benoît du Garreau 2024-03-14 11:20:59 +01:00
parent b07c1f7f4d
commit 23211b638a

View file

@ -474,15 +474,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::Error::INVALID_UTF8)?;
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)) }
}
}