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