tokio/io/util/
read_to_string.rs1use crate::io::util::read_line::finish_string_read;
2use crate::io::util::read_to_end::read_to_end_internal;
3use crate::io::util::vec_with_initialized::VecWithInitialized;
4use crate::io::AsyncRead;
5
6use pin_project_lite::pin_project;
7use std::future::Future;
8use std::marker::PhantomPinned;
9use std::pin::Pin;
10use std::task::{ready, Context, Poll};
11use std::{io, mem};
12
13pin_project! {
14 #[derive(Debug)]
16 #[must_use = "futures do nothing unless you `.await` or poll them"]
17 pub struct ReadToString<'a, R: ?Sized> {
18 reader: &'a mut R,
19 output: &'a mut String,
22 buf: VecWithInitialized<Vec<u8>>,
24 read: usize,
27 #[pin]
29 _pin: PhantomPinned,
30 }
31}
32
33pub(crate) fn read_to_string<'a, R>(
34 reader: &'a mut R,
35 string: &'a mut String,
36) -> ReadToString<'a, R>
37where
38 R: AsyncRead + ?Sized + Unpin,
39{
40 let buf = mem::take(string).into_bytes();
41 ReadToString {
42 reader,
43 buf: VecWithInitialized::new(buf),
44 output: string,
45 read: 0,
46 _pin: PhantomPinned,
47 }
48}
49
50fn read_to_string_internal<R: AsyncRead + ?Sized>(
51 reader: Pin<&mut R>,
52 output: &mut String,
53 buf: &mut VecWithInitialized<Vec<u8>>,
54 read: &mut usize,
55 cx: &mut Context<'_>,
56) -> Poll<io::Result<usize>> {
57 let io_res = ready!(read_to_end_internal(buf, reader, read, cx));
58 let utf8_res = String::from_utf8(buf.take());
59
60 debug_assert!(buf.is_empty());
63 debug_assert!(output.is_empty());
64 finish_string_read(io_res, utf8_res, *read, output, true)
65}
66
67impl<A> Future for ReadToString<'_, A>
68where
69 A: AsyncRead + ?Sized + Unpin,
70{
71 type Output = io::Result<usize>;
72
73 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
74 let me = self.project();
75
76 read_to_string_internal(Pin::new(*me.reader), me.output, me.buf, me.read, cx)
77 }
78}