mod chunked;
mod eof;
mod fixed;
use futures_io::AsyncRead;
use std::io::Result;
use std::pin::Pin;
use std::task::{Context, Poll};
#[derive(Debug)]
enum Impl<'socket, Socket: AsyncRead + ?Sized> {
Chunked(chunked::Receive<'socket, Socket>),
EOF(eof::Receive<'socket, Socket>),
Fixed(fixed::Receive<'socket, Socket>),
}
#[derive(Debug)]
pub struct Receive<'socket, Socket: AsyncRead + ?Sized> {
body_impl: Impl<'socket, Socket>,
persistent: bool,
}
impl<'socket, Socket: AsyncRead + ?Sized> Receive<'socket, Socket> {
pub(crate) fn new_chunked(socket: Pin<&'socket mut Socket>, persistent: bool) -> Self {
Self {
body_impl: Impl::Chunked(chunked::Receive::new(socket)),
persistent,
}
}
pub(crate) fn new_eof(socket: Pin<&'socket mut Socket>) -> Self {
Self {
body_impl: Impl::EOF(eof::Receive::new(socket)),
persistent: false,
}
}
pub(crate) fn new_fixed(
socket: Pin<&'socket mut Socket>,
length: u64,
persistent: bool,
) -> Self {
Self {
body_impl: Impl::Fixed(fixed::Receive::new(socket, length)),
persistent,
}
}
#[allow(clippy::must_use_candidate)] pub fn finish(self) -> bool {
self.persistent
&& match self.body_impl {
Impl::Chunked(chunked) => chunked.finish(),
Impl::EOF(_) => false,
Impl::Fixed(fixed) => fixed.finish(),
}
}
}
impl<'socket, Socket: AsyncRead + ?Sized> AsyncRead for Receive<'socket, Socket> {
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<Result<usize>> {
match self.body_impl {
Impl::Chunked(ref mut chunked) => Pin::new(chunked).poll_read(cx, buf),
Impl::EOF(ref mut eof) => Pin::new(eof).poll_read(cx, buf),
Impl::Fixed(ref mut fixed) => Pin::new(fixed).poll_read(cx, buf),
}
}
fn poll_read_vectored(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
bufs: &mut [std::io::IoSliceMut<'_>],
) -> Poll<Result<usize>> {
match self.body_impl {
Impl::Chunked(ref mut chunked) => Pin::new(chunked).poll_read_vectored(cx, bufs),
Impl::EOF(ref mut eof) => Pin::new(eof).poll_read_vectored(cx, bufs),
Impl::Fixed(ref mut fixed) => Pin::new(fixed).poll_read_vectored(cx, bufs),
}
}
}