use base_scheduler::Cancellation;
use std::future::Future;
mod buffered;
mod empty;
mod error;
mod frame;
mod head;
mod writable;
pub use buffered::BufferedBody;
pub use empty::EmptyBody;
pub use error::BodyError;
pub use frame::BodyFrame;
pub use head::HeadBody;
pub use writable::{BodyReader, BodyWriter};
pub trait ReadableBody: Send {
type Data: AsRef<[u8]> + Send;
type Error: std::error::Error + Send + Sync + 'static;
fn is_ready(&self) -> bool {
false
}
fn is_empty(&self) -> bool {
false
}
fn length(&self) -> Option<u64> {
None
}
fn may_have_trailers(&self) -> bool {
true
}
fn is_rewindable(&self) -> bool {
false
}
fn rewind(&mut self) -> bool {
false
}
fn close(&mut self, error: Option<Self::Error>);
fn read(
&mut self,
cancellation: &Cancellation,
) -> impl Future<Output = Result<BodyFrame<Self::Data>, Self::Error>> + Send;
}
impl<R: ReadableBody + ?Sized> ReadableBody for &mut R {
type Data = R::Data;
type Error = R::Error;
fn is_ready(&self) -> bool {
(**self).is_ready()
}
fn is_empty(&self) -> bool {
(**self).is_empty()
}
fn length(&self) -> Option<u64> {
(**self).length()
}
fn may_have_trailers(&self) -> bool {
(**self).may_have_trailers()
}
fn is_rewindable(&self) -> bool {
(**self).is_rewindable()
}
fn rewind(&mut self) -> bool {
(**self).rewind()
}
fn close(&mut self, error: Option<Self::Error>) {
(**self).close(error)
}
async fn read(
&mut self,
cancellation: &Cancellation,
) -> Result<BodyFrame<Self::Data>, Self::Error> {
(**self).read(cancellation).await
}
}
pub trait WritableBody: Send {
type Data: AsRef<[u8]> + Send;
type Error: std::error::Error + Send + Sync + 'static;
fn write_frame(
&mut self,
frame: BodyFrame<Self::Data>,
cancellation: &Cancellation,
) -> impl Future<Output = Result<(), Self::Error>> + Send;
fn write(
&mut self,
data: Self::Data,
cancellation: &Cancellation,
) -> impl Future<Output = Result<(), Self::Error>> + Send {
self.write_frame(BodyFrame::Data { data, end: false }, cancellation)
}
fn flush(
&mut self,
cancellation: &Cancellation,
) -> impl Future<Output = Result<(), Self::Error>> + Send;
fn close_write(&mut self, error: Option<Self::Error>);
fn abort(&mut self);
}
impl<W: WritableBody + ?Sized> WritableBody for &mut W {
type Data = W::Data;
type Error = W::Error;
async fn write_frame(
&mut self,
frame: BodyFrame<Self::Data>,
cancellation: &Cancellation,
) -> Result<(), Self::Error> {
(**self).write_frame(frame, cancellation).await
}
async fn write(
&mut self,
data: Self::Data,
cancellation: &Cancellation,
) -> Result<(), Self::Error> {
(**self).write(data, cancellation).await
}
async fn flush(&mut self, cancellation: &Cancellation) -> Result<(), Self::Error> {
(**self).flush(cancellation).await
}
fn close_write(&mut self, error: Option<Self::Error>) {
(**self).close_write(error)
}
fn abort(&mut self) {
(**self).abort()
}
}