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;
89/// 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}
1718impl<R: ?Sized + Unpin> Unpin for ReadToEnd<'_, R> {}
1920impl<'a, R: AsyncRead + ?Sized + Unpin> ReadToEnd<'a, R> {
21pub(super) fn new(reader: &'a mut R, buf: &'a mut Vec<u8>) -> Self {
22let start_len = buf.len();
23Self { reader, buf, start_len }
24 }
25}
2627struct Guard<'a> {
28 buf: &'a mut Vec<u8>,
29 len: usize,
30}
3132impl Drop for Guard<'_> {
33fn drop(&mut self) {
34unsafe {
35self.buf.set_len(self.len);
36 }
37 }
38}
3940// 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>(
50mut rd: Pin<&mut R>,
51 cx: &mut Context<'_>,
52 buf: &mut Vec<u8>,
53 start_len: usize,
54) -> Poll<io::Result<usize>> {
55let mut g = Guard { len: buf.len(), buf };
56loop {
57if g.len == g.buf.len() {
58unsafe {
59 g.buf.reserve(32);
60let capacity = g.buf.capacity();
61 g.buf.set_len(capacity);
62super::initialize(&rd, &mut g.buf[g.len..]);
63 }
64 }
6566let buf = &mut g.buf[g.len..];
67match ready!(rd.as_mut().poll_read(cx, buf)) {
68Ok(0) => return Poll::Ready(Ok(g.len - start_len)),
69Ok(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.
73assert!(n <= buf.len());
74 g.len += n;
75 }
76Err(e) => return Poll::Ready(Err(e)),
77 }
78 }
79}
8081impl<A> Future for ReadToEnd<'_, A>
82where
83A: AsyncRead + ?Sized + Unpin,
84{
85type Output = io::Result<usize>;
8687fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
88let this = &mut *self;
89 read_to_end_internal(Pin::new(&mut this.reader), cx, this.buf, this.start_len)
90 }
91}