1use crate::io::AsyncRead;
2use futures_core::future::Future;
3use futures_core::ready;
4use futures_core::task::{Context, Poll};
5use std::io;
6use std::mem;
7use std::pin::Pin;
8
9/// Future for the [`read_exact`](super::AsyncReadExt::read_exact) method.
10#[derive(Debug)]
11#[must_use = "futures do nothing unless you `.await` or poll them"]
12pub struct ReadExact<'a, R: ?Sized> {
13 reader: &'a mut R,
14 buf: &'a mut [u8],
15}
16
17impl<R: ?Sized + Unpin> Unpin for ReadExact<'_, R> {}
18
19impl<'a, R: AsyncRead + ?Sized + Unpin> ReadExact<'a, R> {
20 pub(super) fn new(reader: &'a mut R, buf: &'a mut [u8]) -> Self {
21 Self { reader, buf }
22 }
23}
24
25impl<R: AsyncRead + ?Sized + Unpin> Future for ReadExact<'_, R> {
26 type Output = io::Result<()>;
27
28 fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
29 let this: &mut ReadExact<'_, R> = &mut *self;
30 while !this.buf.is_empty() {
31 let n: usize = ready!(Pin::new(&mut this.reader).poll_read(cx, this.buf))?;
32 {
33 let (_, rest: &mut [u8]) = mem::take(&mut this.buf).split_at_mut(mid:n);
34 this.buf = rest;
35 }
36 if n == 0 {
37 return Poll::Ready(Err(io::ErrorKind::UnexpectedEof.into()));
38 }
39 }
40 Poll::Ready(Ok(()))
41 }
42}
43