1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
use axum::{
body::{self, Bytes, HttpBody, StreamBody},
http::HeaderMap,
response::{IntoResponse, Response},
Error,
};
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)]
pub struct AsyncReadBody<R> {
#[pin]
read: StreamBody<ReaderStream<R>>,
}
}
impl<R> AsyncReadBody<R> {
pub fn new(read: R) -> Self
where
R: AsyncRead + Send + 'static,
{
Self {
read: StreamBody::new(ReaderStream::new(read)),
}
}
}
impl<R> HttpBody for AsyncReadBody<R>
where
R: AsyncRead + Send + 'static,
{
type Data = Bytes;
type Error = Error;
fn poll_data(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Result<Self::Data, Self::Error>>> {
self.project().read.poll_data(cx)
}
fn poll_trailers(
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
) -> Poll<Result<Option<HeaderMap>, Self::Error>> {
Poll::Ready(Ok(None))
}
}
impl<R> IntoResponse for AsyncReadBody<R>
where
R: AsyncRead + Send + 'static,
{
fn into_response(self) -> Response {
Response::new(body::boxed(self))
}
}