1use std::{
4 convert::Infallible,
5 pin::Pin,
6 task::{Context, Poll},
7};
8
9use bytes::Bytes;
10use futures_core::Stream;
11use http_body_util::BodyExt;
12
13use crate::BoxError;
14
15#[derive(Debug)]
17pub struct Body(BodyInner);
18
19type BodyInner = http_body_util::combinators::UnsyncBoxBody<Bytes, BoxError>;
20
21impl Body {
22 pub fn empty() -> Self {
24 Body::new(http_body_util::Empty::new())
25 }
26
27 pub fn full(data: Bytes) -> Self {
29 Body::new(http_body_util::Full::new(data))
30 }
31
32 pub fn stream<S, E>(stream: S) -> Self
34 where
35 S: Stream<Item = Result<http_body::Frame<Bytes>, E>> + Send + 'static,
36 E: Into<BoxError>,
37 {
38 Body::new(http_body_util::StreamBody::new(stream))
39 }
40
41 pub(crate) fn new<B>(body: B) -> Self
42 where
43 B: http_body::Body<Data = Bytes> + Send + 'static,
44 B::Error: Into<BoxError>,
45 {
46 Body(body.map_err(|err| err.into()).boxed_unsync())
47 }
48}
49
50impl http_body::Body for Body {
51 type Data = Bytes;
52 type Error = BoxError;
53
54 fn poll_frame(
55 mut self: Pin<&mut Self>,
56 cx: &mut Context<'_>,
57 ) -> Poll<Option<Result<http_body::Frame<Self::Data>, Self::Error>>> {
58 Pin::new(&mut self.0).poll_frame(cx)
59 }
60
61 fn is_end_stream(&self) -> bool {
62 self.0.is_end_stream()
63 }
64
65 fn size_hint(&self) -> http_body::SizeHint {
66 self.0.size_hint()
67 }
68}
69
70pub struct ResponseFuture(ResponseFutureInner);
72
73type ResponseFutureInner =
74 Pin<Box<dyn Future<Output = Result<http::Response<Body>, Infallible>> + Send>>;
75
76impl ResponseFuture {
77 pub fn new<F>(future: F) -> Self
78 where
79 F: Future<Output = Result<http::Response<Body>, Infallible>> + Send + 'static,
80 {
81 ResponseFuture(Box::pin(future))
82 }
83}
84
85impl Future for ResponseFuture {
86 type Output = Result<http::Response<Body>, Infallible>;
87
88 fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
89 self.0.as_mut().poll(cx)
90 }
91}