Skip to main content

tokio/io/util/
read_to_end.rs

1use crate::io::util::vec_with_initialized::{into_read_buf_parts, VecU8, VecWithInitialized};
2use crate::io::{AsyncRead, ReadBuf};
3
4use pin_project_lite::pin_project;
5use std::future::Future;
6use std::io;
7use std::marker::PhantomPinned;
8use std::mem::{self, MaybeUninit};
9use std::pin::Pin;
10use std::task::{ready, Context, Poll};
11
12pin_project! {
13    #[derive(Debug)]
14    #[must_use = "futures do nothing unless you `.await` or poll them"]
15    pub struct ReadToEnd<'a, R: ?Sized> {
16        reader: &'a mut R,
17        buf: VecWithInitialized<&'a mut Vec<u8>>,
18        // The number of bytes appended to buf. This can be less than buf.len() if
19        // the buffer was not empty when the operation was started.
20        read: usize,
21        // Make this future `!Unpin` for compatibility with async trait methods.
22        #[pin]
23        _pin: PhantomPinned,
24    }
25}
26
27pub(crate) fn read_to_end<'a, R>(reader: &'a mut R, buffer: &'a mut Vec<u8>) -> ReadToEnd<'a, R>
28where
29    R: AsyncRead + Unpin + ?Sized,
30{
31    ReadToEnd {
32        reader,
33        buf: VecWithInitialized::new(buffer),
34        read: 0,
35        _pin: PhantomPinned,
36    }
37}
38
39pub(super) fn read_to_end_internal<V: VecU8, R: AsyncRead + ?Sized>(
40    buf: &mut VecWithInitialized<V>,
41    mut reader: Pin<&mut R>,
42    num_read: &mut usize,
43    cx: &mut Context<'_>,
44) -> Poll<io::Result<usize>> {
45    loop {
46        let ret = ready!(poll_read_to_end(buf, reader.as_mut(), cx));
47        match ret {
48            Err(err) if err.kind() == io::ErrorKind::Interrupted => continue,
49            Err(err) => return Poll::Ready(Err(err)),
50            Ok(0) => return Poll::Ready(Ok(mem::replace(num_read, 0))),
51            Ok(num) => {
52                *num_read += num;
53            }
54        }
55    }
56}
57
58/// Tries to read from the provided [`AsyncRead`].
59///
60/// The length of the buffer is increased by the number of bytes read.
61fn poll_read_to_end<V: VecU8, R: AsyncRead + ?Sized>(
62    buf: &mut VecWithInitialized<V>,
63    read: Pin<&mut R>,
64    cx: &mut Context<'_>,
65) -> Poll<io::Result<usize>> {
66    // This uses an adaptive system to extend the vector when it fills. We want to
67    // avoid paying to allocate and zero a huge chunk of memory if the reader only
68    // has 4 bytes while still making large reads if the reader does have a ton
69    // of data to return. Simply tacking on an extra DEFAULT_BUF_SIZE space every
70    // time is 4,500 times (!) slower than this if the reader has a very small
71    // amount of data to return. When the vector is full with its starting
72    // capacity, we first try to read into a small buffer to see if we reached
73    // an EOF. This only happens when the starting capacity is >= NUM_BYTES, since
74    // we allocate at least NUM_BYTES each time. This avoids the unnecessary
75    // allocation that we attempt before reading into the vector.
76
77    const NUM_BYTES: usize = 32;
78    let try_small_read = buf.try_small_read_first(NUM_BYTES);
79
80    // Get a ReadBuf into the vector.
81    let mut read_buf;
82    let poll_result;
83
84    let n = if try_small_read {
85        // Read some bytes using a small read.
86        let mut small_buf: [MaybeUninit<u8>; NUM_BYTES] = [MaybeUninit::uninit(); NUM_BYTES];
87        let mut small_read_buf = ReadBuf::uninit(&mut small_buf);
88        poll_result = read.poll_read(cx, &mut small_read_buf);
89        let to_write = small_read_buf.filled();
90
91        // Ensure we have enough space to fill our vector with what we read.
92        read_buf = buf.get_read_buf();
93        if to_write.len() > read_buf.remaining() {
94            buf.reserve(NUM_BYTES);
95            read_buf = buf.get_read_buf();
96        }
97        read_buf.put_slice(to_write);
98
99        to_write.len()
100    } else {
101        // Ensure we have enough space for reading.
102        buf.reserve(NUM_BYTES);
103        read_buf = buf.get_read_buf();
104
105        // Read data directly into vector.
106        let filled_before = read_buf.filled().len();
107        poll_result = read.poll_read(cx, &mut read_buf);
108
109        // Compute the number of bytes read.
110        read_buf.filled().len() - filled_before
111    };
112
113    // Update the length of the vector using the result of poll_read.
114    let read_buf_parts = into_read_buf_parts(read_buf);
115    buf.apply_read_buf(read_buf_parts);
116
117    match poll_result {
118        Poll::Pending => {
119            // In this case, nothing should have been read. However we still
120            // update the vector in case the poll_read call initialized parts of
121            // the vector's unused capacity.
122            debug_assert_eq!(n, 0);
123            Poll::Pending
124        }
125        Poll::Ready(Err(err)) => {
126            debug_assert_eq!(n, 0);
127            Poll::Ready(Err(err))
128        }
129        Poll::Ready(Ok(())) => Poll::Ready(Ok(n)),
130    }
131}
132
133impl<A> Future for ReadToEnd<'_, A>
134where
135    A: AsyncRead + ?Sized + Unpin,
136{
137    type Output = io::Result<usize>;
138
139    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
140        let me = self.project();
141
142        read_to_end_internal(me.buf, Pin::new(*me.reader), me.read, cx)
143    }
144}