1 | use std::mem; |
2 | use std::pin::Pin; |
3 | use std::future::Future; |
4 | |
5 | use crate::io::{self, Read}; |
6 | use crate::task::{Context, Poll}; |
7 | |
8 | #[doc (hidden)] |
9 | #[allow (missing_debug_implementations)] |
10 | pub struct ReadExactFuture<'a, T: Unpin + ?Sized> { |
11 | pub(crate) reader: &'a mut T, |
12 | pub(crate) buf: &'a mut [u8], |
13 | } |
14 | |
15 | impl<T: Read + Unpin + ?Sized> Future for ReadExactFuture<'_, T> { |
16 | type Output = io::Result<()>; |
17 | |
18 | fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { |
19 | let Self { reader: &mut &mut T, buf: &mut &mut [u8] } = &mut *self; |
20 | |
21 | while !buf.is_empty() { |
22 | let n: usize = futures_core::ready!(Pin::new(&mut *reader).poll_read(cx, buf))?; |
23 | let (_, rest: &mut [u8]) = mem::replace(buf, &mut []).split_at_mut(mid:n); |
24 | *buf = rest; |
25 | |
26 | if n == 0 { |
27 | return Poll::Ready(Err(io::ErrorKind::UnexpectedEof.into())); |
28 | } |
29 | } |
30 | |
31 | Poll::Ready(Ok(())) |
32 | } |
33 | } |
34 | |