futures_util/io/
read_to_end.rs

1use futures_core::future::Future;
2use futures_core::ready;
3use futures_core::task::{Context, Poll};
4use futures_io::AsyncRead;
5use std::io;
6use std::pin::Pin;
7use std::vec::Vec;
8
9/// Future for the [`read_to_end`](super::AsyncReadExt::read_to_end) method.
10#[derive(Debug)]
11#[must_use = "futures do nothing unless you `.await` or poll them"]
12pub struct ReadToEnd<'a, R: ?Sized> {
13    reader: &'a mut R,
14    buf: &'a mut Vec<u8>,
15    start_len: usize,
16}
17
18impl<R: ?Sized + Unpin> Unpin for ReadToEnd<'_, R> {}
19
20impl<'a, R: AsyncRead + ?Sized + Unpin> ReadToEnd<'a, R> {
21    pub(super) fn new(reader: &'a mut R, buf: &'a mut Vec<u8>) -> Self {
22        let start_len = buf.len();
23        Self { reader, buf, start_len }
24    }
25}
26
27struct Guard<'a> {
28    buf: &'a mut Vec<u8>,
29    len: usize,
30}
31
32impl Drop for Guard<'_> {
33    fn drop(&mut self) {
34        unsafe {
35            self.buf.set_len(self.len);
36        }
37    }
38}
39
40// This uses an adaptive system to extend the vector when it fills. We want to
41// avoid paying to allocate and zero a huge chunk of memory if the reader only
42// has 4 bytes while still making large reads if the reader does have a ton
43// of data to return. Simply tacking on an extra DEFAULT_BUF_SIZE space every
44// time is 4,500 times (!) slower than this if the reader has a very small
45// amount of data to return.
46//
47// Because we're extending the buffer with uninitialized data for trusted
48// readers, we need to make sure to truncate that if any of this panics.
49pub(super) fn read_to_end_internal<R: AsyncRead + ?Sized>(
50    mut rd: Pin<&mut R>,
51    cx: &mut Context<'_>,
52    buf: &mut Vec<u8>,
53    start_len: usize,
54) -> Poll<io::Result<usize>> {
55    let mut g = Guard { len: buf.len(), buf };
56    loop {
57        if g.len == g.buf.len() {
58            unsafe {
59                g.buf.reserve(32);
60                let capacity = g.buf.capacity();
61                g.buf.set_len(capacity);
62                super::initialize(&rd, &mut g.buf[g.len..]);
63            }
64        }
65
66        let buf = &mut g.buf[g.len..];
67        match ready!(rd.as_mut().poll_read(cx, buf)) {
68            Ok(0) => return Poll::Ready(Ok(g.len - start_len)),
69            Ok(n) => {
70                // We can't allow bogus values from read. If it is too large, the returned vec could have its length
71                // set past its capacity, or if it overflows the vec could be shortened which could create an invalid
72                // string if this is called via read_to_string.
73                assert!(n <= buf.len());
74                g.len += n;
75            }
76            Err(e) => return Poll::Ready(Err(e)),
77        }
78    }
79}
80
81impl<A> Future for ReadToEnd<'_, A>
82where
83    A: AsyncRead + ?Sized + Unpin,
84{
85    type Output = io::Result<usize>;
86
87    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
88        let this = &mut *self;
89        read_to_end_internal(Pin::new(&mut this.reader), cx, this.buf, this.start_len)
90    }
91}