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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
#[cfg_attr(not(feature = "all-implementations"), allow(unused))]
use std::{
io::Result,
pin::Pin,
task::{Context, Poll},
};
pub struct TrackEof<R> {
inner: R,
eof: bool,
}
impl<R: Unpin> TrackEof<R> {
pub fn new(inner: R) -> Self {
Self { inner, eof: false }
}
pub fn project(self: Pin<&mut Self>) -> (Pin<&mut R>, &mut bool) {
let Self { inner, eof } = Pin::into_inner(self);
(Pin::new(inner), eof)
}
}
#[cfg(feature = "futures-io")]
impl<R: futures::io::AsyncRead + Unpin> futures::io::AsyncRead for TrackEof<R> {
fn poll_read(self: Pin<&mut Self>, cx: &mut Context, buf: &mut [u8]) -> Poll<Result<usize>> {
let (inner, eof) = self.project();
assert!(!*eof);
match inner.poll_read(cx, buf) {
Poll::Ready(Ok(0)) => {
if !buf.is_empty() {
*eof = true;
}
Poll::Ready(Ok(0))
}
other => other,
}
}
}
#[cfg(feature = "futures-io")]
impl<R: futures::io::AsyncBufRead + Unpin> futures::io::AsyncBufRead for TrackEof<R> {
fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<&[u8]>> {
let (inner, eof) = self.project();
assert!(!*eof);
match inner.poll_fill_buf(cx) {
Poll::Ready(Ok(buf)) => {
if buf.is_empty() {
*eof = true;
}
Poll::Ready(Ok(buf))
}
other => other,
}
}
fn consume(self: Pin<&mut Self>, amt: usize) {
self.project().0.consume(amt)
}
}
#[cfg(feature = "tokio")]
impl<R: tokio::io::AsyncRead + Unpin> tokio::io::AsyncRead for TrackEof<R> {
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context,
buf: &mut tokio::io::ReadBuf,
) -> Poll<Result<()>> {
let (inner, eof) = self.project();
assert!(!*eof);
let len = buf.filled().len();
match inner.poll_read(cx, buf) {
Poll::Ready(Ok(())) => {
if buf.filled().len() == len && buf.remaining() > 0 {
*eof = true;
}
Poll::Ready(Ok(()))
}
other => other,
}
}
}
#[cfg(feature = "tokio")]
impl<R: tokio::io::AsyncBufRead + Unpin> tokio::io::AsyncBufRead for TrackEof<R> {
fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<&[u8]>> {
let (inner, eof) = self.project();
assert!(!*eof);
match inner.poll_fill_buf(cx) {
Poll::Ready(Ok(buf)) => {
if buf.is_empty() {
*eof = true;
}
Poll::Ready(Ok(buf))
}
other => other,
}
}
fn consume(self: Pin<&mut Self>, amt: usize) {
self.project().0.consume(amt)
}
}
|