tokio/io/util/
read_to_string.rs

1use 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    /// Future for the [`read_to_string`](super::AsyncReadExt::read_to_string) method.
15    #[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        // This is the buffer we were provided. It will be replaced with an empty string
20        // while reading to postpone utf-8 handling until after reading.
21        output: &'a mut String,
22        // The actual allocation of the string is moved into this vector instead.
23        buf: VecWithInitialized<Vec<u8>>,
24        // The number of bytes appended to buf. This can be less than buf.len() if
25        // the buffer was not empty when the operation was started.
26        read: usize,
27        // Make this future `!Unpin` for compatibility with async trait methods.
28        #[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    // At this point both buf and output are empty. The allocation is in utf8_res.
61
62    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}