• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use crate::io::util::vec_with_initialized::{into_read_buf_parts, VecU8, VecWithInitialized};
2 use crate::io::AsyncRead;
3 
4 use pin_project_lite::pin_project;
5 use std::future::Future;
6 use std::io;
7 use std::marker::PhantomPinned;
8 use std::mem;
9 use std::pin::Pin;
10 use std::task::{Context, Poll};
11 
12 pin_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 
read_to_end<'a, R>(reader: &'a mut R, buffer: &'a mut Vec<u8>) -> ReadToEnd<'a, R> where R: AsyncRead + Unpin + ?Sized,27 pub(crate) fn read_to_end<'a, R>(reader: &'a mut R, buffer: &'a mut Vec<u8>) -> ReadToEnd<'a, R>
28 where
29     R: AsyncRead + Unpin + ?Sized,
30 {
31     ReadToEnd {
32         reader,
33         buf: VecWithInitialized::new(buffer),
34         read: 0,
35         _pin: PhantomPinned,
36     }
37 }
38 
read_to_end_internal<V: VecU8, R: AsyncRead + ?Sized>( buf: &mut VecWithInitialized<V>, mut reader: Pin<&mut R>, num_read: &mut usize, cx: &mut Context<'_>, ) -> Poll<io::Result<usize>>39 pub(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) => return Poll::Ready(Err(err)),
49             Ok(0) => return Poll::Ready(Ok(mem::replace(num_read, 0))),
50             Ok(num) => {
51                 *num_read += num;
52             }
53         }
54     }
55 }
56 
57 /// Tries to read from the provided AsyncRead.
58 ///
59 /// The length of the buffer is increased by the number of bytes read.
poll_read_to_end<V: VecU8, R: AsyncRead + ?Sized>( buf: &mut VecWithInitialized<V>, read: Pin<&mut R>, cx: &mut Context<'_>, ) -> Poll<io::Result<usize>>60 fn poll_read_to_end<V: VecU8, R: AsyncRead + ?Sized>(
61     buf: &mut VecWithInitialized<V>,
62     read: Pin<&mut R>,
63     cx: &mut Context<'_>,
64 ) -> Poll<io::Result<usize>> {
65     // This uses an adaptive system to extend the vector when it fills. We want to
66     // avoid paying to allocate and zero a huge chunk of memory if the reader only
67     // has 4 bytes while still making large reads if the reader does have a ton
68     // of data to return. Simply tacking on an extra DEFAULT_BUF_SIZE space every
69     // time is 4,500 times (!) slower than this if the reader has a very small
70     // amount of data to return.
71     buf.reserve(32);
72 
73     // Get a ReadBuf into the vector.
74     let mut read_buf = buf.get_read_buf();
75 
76     let filled_before = read_buf.filled().len();
77     let poll_result = read.poll_read(cx, &mut read_buf);
78     let filled_after = read_buf.filled().len();
79     let n = filled_after - filled_before;
80 
81     // Update the length of the vector using the result of poll_read.
82     let read_buf_parts = into_read_buf_parts(read_buf);
83     buf.apply_read_buf(read_buf_parts);
84 
85     match poll_result {
86         Poll::Pending => {
87             // In this case, nothing should have been read. However we still
88             // update the vector in case the poll_read call initialized parts of
89             // the vector's unused capacity.
90             debug_assert_eq!(filled_before, filled_after);
91             Poll::Pending
92         }
93         Poll::Ready(Err(err)) => {
94             debug_assert_eq!(filled_before, filled_after);
95             Poll::Ready(Err(err))
96         }
97         Poll::Ready(Ok(())) => Poll::Ready(Ok(n)),
98     }
99 }
100 
101 impl<A> Future for ReadToEnd<'_, A>
102 where
103     A: AsyncRead + ?Sized + Unpin,
104 {
105     type Output = io::Result<usize>;
106 
poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>107     fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
108         let me = self.project();
109 
110         read_to_end_internal(me.buf, Pin::new(*me.reader), me.read, cx)
111     }
112 }
113