use axum_core::{
body::Body,
response::{IntoResponse, Response},
Error,
};
use bytes::Bytes;
use http_body::Body as HttpBody;
use pin_project_lite::pin_project;
use std::{
pin::Pin,
task::{Context, Poll},
};
use tokio::io::AsyncRead;
use tokio_util::io::ReaderStream;
pin_project! {
#[cfg(feature = "async-read-body")]
#[derive(Debug)]
#[must_use]
pub struct AsyncReadBody {
#[pin]
body: Body,
}
}
impl AsyncReadBody {
pub fn new<R>(read: R) -> Self
where
R: AsyncRead + Send + 'static,
{
Self {
body: Body::from_stream(ReaderStream::new(read)),
}
}
}
impl HttpBody for AsyncReadBody {
type Data = Bytes;
type Error = Error;
#[inline]
fn poll_frame(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Result<http_body::Frame<Self::Data>, Self::Error>>> {
self.project().body.poll_frame(cx)
}
#[inline]
fn is_end_stream(&self) -> bool {
self.body.is_end_stream()
}
#[inline]
fn size_hint(&self) -> http_body::SizeHint {
self.body.size_hint()
}
}
impl IntoResponse for AsyncReadBody {
fn into_response(self) -> Response {
self.body.into_response()
}
}