use std::io;
use std::pin::Pin;
use std::task::{Context, Poll};
use futures_io::{AsyncRead, AsyncSeek};
pub trait SizedBodyStream: AsyncRead + AsyncSeek + Send + 'static {}
pub trait UnsizedBodyStream: AsyncRead + Send + 'static {}
pub enum BoxedStream {
Sized {
stream: Pin<Box<dyn SizedBodyStream>>,
content_length: u64,
},
Unsized {
stream: Pin<Box<dyn UnsizedBodyStream>>,
},
}
pub type Body = crate::body::Body<BoxedStream>;
impl<S: AsyncRead + AsyncSeek + Send + 'static + ?Sized> SizedBodyStream for S {}
impl<S: AsyncRead + Send + 'static + ?Sized> UnsizedBodyStream for S {}
impl AsyncRead for BoxedStream {
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
match self.get_mut() {
BoxedStream::Sized { stream, .. } => Pin::new(stream).poll_read(cx, buf),
BoxedStream::Unsized { stream } => Pin::new(stream).poll_read(cx, buf),
}
}
}