use std::error::Error as StdError;
use bytes::Buf;
use http::HeaderMap;
use crate::common::{Pin, Poll, task};
use http_body::{Body as HttpBody, SizeHint};
pub trait Payload: sealed::Sealed + Send + 'static {
type Data: Buf + Send;
type Error: Into<Box<dyn StdError + Send + Sync>>;
fn poll_data(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Option<Result<Self::Data, Self::Error>>>;
fn poll_trailers(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Result<Option<HeaderMap>, Self::Error>> {
drop(cx);
Poll::Ready(Ok(None))
}
fn is_end_stream(&self) -> bool {
false
}
fn size_hint(&self) -> SizeHint {
SizeHint::default()
}
}
impl<T> Payload for T
where
T: HttpBody + Send + 'static,
T::Data: Send,
T::Error: Into<Box<dyn StdError + Send + Sync>>,
{
type Data = T::Data;
type Error = T::Error;
fn poll_data(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Option<Result<Self::Data, Self::Error>>> {
HttpBody::poll_data(self, cx)
}
fn poll_trailers(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Result<Option<HeaderMap>, Self::Error>> {
HttpBody::poll_trailers(self, cx)
}
fn is_end_stream(&self) -> bool {
HttpBody::is_end_stream(self)
}
fn size_hint(&self) -> SizeHint {
HttpBody::size_hint(self)
}
}
impl<T> sealed::Sealed for T
where
T: HttpBody + Send + 'static,
T::Data: Send,
T::Error: Into<Box<dyn StdError + Send + Sync>>,
{
}
mod sealed {
pub trait Sealed {}
}