1 | use std::mem; |
2 | use std::pin::Pin; |
3 | use std::str; |
4 | use std::future::Future; |
5 | |
6 | use super::read_to_end_internal; |
7 | use crate::io::{self, Read}; |
8 | use crate::task::{Context, Poll}; |
9 | |
10 | #[doc (hidden)] |
11 | #[allow (missing_debug_implementations)] |
12 | pub struct ReadToStringFuture<'a, T: Unpin + ?Sized> { |
13 | pub(crate) reader: &'a mut T, |
14 | pub(crate) buf: &'a mut String, |
15 | pub(crate) bytes: Vec<u8>, |
16 | pub(crate) start_len: usize, |
17 | } |
18 | |
19 | impl<T: Read + Unpin + ?Sized> Future for ReadToStringFuture<'_, T> { |
20 | type Output = io::Result<usize>; |
21 | |
22 | fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { |
23 | let Self { |
24 | reader, |
25 | buf, |
26 | bytes, |
27 | start_len, |
28 | } = &mut *self; |
29 | let reader = Pin::new(reader); |
30 | |
31 | let ret = futures_core::ready!(read_to_end_internal(reader, cx, bytes, *start_len)); |
32 | if str::from_utf8(&bytes).is_err() { |
33 | Poll::Ready(ret.and_then(|_| { |
34 | Err(io::Error::new( |
35 | io::ErrorKind::InvalidData, |
36 | "stream did not contain valid UTF-8" , |
37 | )) |
38 | })) |
39 | } else { |
40 | #[allow (clippy::debug_assert_with_mut_call)] |
41 | { |
42 | debug_assert!(buf.is_empty()); |
43 | } |
44 | |
45 | // Safety: `bytes` is a valid UTF-8 because `str::from_utf8` returned `Ok`. |
46 | mem::swap(unsafe { buf.as_mut_vec() }, bytes); |
47 | Poll::Ready(ret) |
48 | } |
49 | } |
50 | } |
51 | |