1use crate::io::{AsyncRead, ReadBuf};
2
3use pin_project_lite::pin_project;
4use std::future::Future;
5use std::io;
6use std::marker::PhantomPinned;
7use std::marker::Unpin;
8use std::pin::Pin;
9use std::task::{ready, Context, Poll};
10
11pub(crate) fn read<'a, R>(reader: &'a mut R, buf: &'a mut [u8]) -> Read<'a, R>
17where
18 R: AsyncRead + Unpin + ?Sized,
19{
20 Read {
21 reader,
22 buf,
23 _pin: PhantomPinned,
24 }
25}
26
27pin_project! {
28 #[derive(Debug)]
33 #[must_use = "futures do nothing unless you `.await` or poll them"]
34 pub struct Read<'a, R: ?Sized> {
35 reader: &'a mut R,
36 buf: &'a mut [u8],
37 #[pin]
39 _pin: PhantomPinned,
40 }
41}
42
43impl<R> Future for Read<'_, R>
44where
45 R: AsyncRead + Unpin + ?Sized,
46{
47 type Output = io::Result<usize>;
48
49 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<usize>> {
50 let me = self.project();
51 let mut buf = ReadBuf::new(me.buf);
52 ready!(Pin::new(me.reader).poll_read(cx, &mut buf))?;
53 Poll::Ready(Ok(buf.filled().len()))
54 }
55}