async_std/io/read/
read_exact.rs

1use std::mem;
2use std::pin::Pin;
3use std::future::Future;
4
5use crate::io::{self, Read};
6use crate::task::{Context, Poll};
7
8#[doc(hidden)]
9#[allow(missing_debug_implementations)]
10pub struct ReadExactFuture<'a, T: Unpin + ?Sized> {
11    pub(crate) reader: &'a mut T,
12    pub(crate) buf: &'a mut [u8],
13}
14
15impl<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, buf } = &mut *self;
20
21        while !buf.is_empty() {
22            let n = futures_core::ready!(Pin::new(&mut *reader).poll_read(cx, buf))?;
23            let (_, rest) = mem::replace(buf, &mut []).split_at_mut(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}