tokio/io/util/
read_to_end.rs1use 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 read: usize,
21 #[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
58fn 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 const NUM_BYTES: usize = 32;
78 let try_small_read = buf.try_small_read_first(NUM_BYTES);
79
80 let mut read_buf;
82 let poll_result;
83
84 let n = if try_small_read {
85 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 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 buf.reserve(NUM_BYTES);
103 read_buf = buf.get_read_buf();
104
105 let filled_before = read_buf.filled().len();
107 poll_result = read.poll_read(cx, &mut read_buf);
108
109 read_buf.filled().len() - filled_before
111 };
112
113 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 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}