1 use futures_core::future::Future; 2 use futures_core::task::{Context, Poll}; 3 use futures_io::AsyncBufRead; 4 use std::io; 5 use std::pin::Pin; 6 7 /// Future for the [`fill_buf`](super::AsyncBufReadExt::fill_buf) method. 8 #[derive(Debug)] 9 #[must_use = "futures do nothing unless you `.await` or poll them"] 10 pub struct FillBuf<'a, R: ?Sized> { 11 reader: Option<&'a mut R>, 12 } 13 14 impl<R: ?Sized> Unpin for FillBuf<'_, R> {} 15 16 impl<'a, R: AsyncBufRead + ?Sized + Unpin> FillBuf<'a, R> { new(reader: &'a mut R) -> Self17 pub(super) fn new(reader: &'a mut R) -> Self { 18 Self { reader: Some(reader) } 19 } 20 } 21 22 impl<'a, R> Future for FillBuf<'a, R> 23 where R: AsyncBufRead + ?Sized + Unpin, 24 { 25 type Output = io::Result<&'a [u8]>; 26 poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>27 fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { 28 let this = &mut *self; 29 let reader = this.reader.take().expect("Polled FillBuf after completion"); 30 31 match Pin::new(&mut *reader).poll_fill_buf(cx) { 32 // With polinius it is possible to remove this inner match and just have the correct 33 // lifetime of the reference inferred based on which branch is taken 34 Poll::Ready(Ok(_)) => match Pin::new(reader).poll_fill_buf(cx) { 35 Poll::Ready(Ok(slice)) => Poll::Ready(Ok(slice)), 36 Poll::Ready(Err(err)) => { 37 unreachable!("reader indicated readiness but then returned an error: {:?}", err) 38 } 39 Poll::Pending => { 40 unreachable!("reader indicated readiness but then returned pending") 41 } 42 }, 43 Poll::Ready(Err(err)) => Poll::Ready(Err(err)), 44 Poll::Pending => { 45 this.reader = Some(reader); 46 Poll::Pending 47 } 48 } 49 } 50 } 51