1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
use std::io::Result;
use std::pin::Pin;
use std::task::{Poll, Context};

use tokio::io::AsyncRead;
use tokio::io::ReadBuf;

use super::{Stream, RoleHelper};
use super::detail::read_some;

impl<IO, Role> AsyncRead for Stream<IO, Role>
where
    IO: AsyncRead + Unpin,
    Stream<IO, Role>: Unpin,
    Role: RoleHelper,
{
    /// Async version of `Stream::read`.
    #[rustfmt::skip]
    fn poll_read(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &mut ReadBuf<'_>,
    ) -> Poll<Result<()>> {
        read_some(self.get_mut(), |io, buf| {
                let mut buf = ReadBuf::new(buf);
                Pin::new(io).poll_read(cx, &mut buf)
                .map_ok(|_| buf.filled().len())
            },
            buf.initialize_unfilled(),
        ).map_ok(|n| buf.advance(n))
    }
}