use ::tokio::io::{AsyncRead, ReadBuf};
use pin_project_lite::pin_project;
use std::{
future::Future,
io,
marker::PhantomPinned,
pin::Pin,
task::{Context, Poll},
};
use super::{AsyncPeek, AsyncPeekable, Buffer, DefaultBuffer};
pub(crate) fn peek<'a, R, B>(
peekable: &'a mut AsyncPeekable<R, B>,
buf: &'a mut [u8],
) -> Peek<'a, R, B>
where
R: AsyncRead + Unpin,
{
Peek {
peekable,
buf,
_pin: PhantomPinned,
}
}
pin_project! {
#[derive(Debug)]
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct Peek<'a, R, B = DefaultBuffer> {
peekable: &'a mut AsyncPeekable<R, B>,
buf: &'a mut [u8],
#[pin]
_pin: PhantomPinned,
}
}
impl<R, B: Buffer> Future for Peek<'_, R, B>
where
R: AsyncRead + Unpin,
{
type Output = io::Result<usize>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<usize>> {
let me = self.project();
let mut buf = ReadBuf::new(me.buf);
ready!(Pin::new(me.peekable).poll_peek(cx, &mut buf))?;
Poll::Ready(Ok(buf.filled().len()))
}
}