1use std::{
6 borrow::Cow,
7 pin::{pin, Pin},
8 task::{Context, Poll},
9};
10
11use http_body_util::{BodyExt, Empty, Full};
12
13use crate::BoxedError;
14
15pub use bytes::*;
18pub use http_body::Body as HttpBody;
19pub use http_body::{Frame, SizeHint};
20
21type BoxedBody = http_body_util::combinators::BoxBody<Bytes, BoxedError>;
25
26#[derive(Debug, Default)]
31pub struct Body(BoxedBody);
32
33impl Body {
34 #[inline(always)]
35 pub fn new<B>(body: B) -> Self
36 where
37 B: HttpBody<Data = Bytes> + Sized + Send + Sync + 'static,
38 B::Error: Into<BoxedError>,
39 {
40 Self(BoxedBody::new(body.map_err(Into::into)))
41 }
42}
43
44impl HttpBody for Body {
45 type Data = Bytes;
46 type Error = BoxedError;
47
48 #[inline(always)]
49 fn poll_frame(
50 mut self: Pin<&mut Self>,
51 cx: &mut Context<'_>,
52 ) -> Poll<Option<Result<Frame<Self::Data>, Self::Error>>> {
53 pin!(&mut self.0).poll_frame(cx)
54 }
55}
56
57impl From<()> for Body {
60 #[inline]
61 fn from(_value: ()) -> Self {
62 Self::default()
63 }
64}
65
66impl From<Bytes> for Body {
67 #[inline]
68 fn from(bytes: Bytes) -> Self {
69 Self::new(Full::from(bytes))
70 }
71}
72
73impl From<Empty<Bytes>> for Body {
74 #[inline]
75 fn from(empty_body: Empty<Bytes>) -> Self {
76 Self::new(empty_body)
77 }
78}
79
80impl From<Full<Bytes>> for Body {
81 #[inline]
82 fn from(full_body: Full<Bytes>) -> Self {
83 Self::new(full_body)
84 }
85}
86
87impl From<&'static str> for Body {
88 #[inline]
89 fn from(str_body: &'static str) -> Self {
90 Cow::<'_, str>::Borrowed(str_body).into()
91 }
92}
93
94impl From<String> for Body {
95 #[inline]
96 fn from(string_body: String) -> Self {
97 Cow::<'_, str>::Owned(string_body).into()
98 }
99}
100
101impl From<Cow<'static, str>> for Body {
102 #[inline]
103 fn from(cow_body: Cow<'static, str>) -> Self {
104 Self::new(Full::from(cow_body))
105 }
106}
107
108impl From<&'static [u8]> for Body {
109 #[inline]
110 fn from(slice_body: &'static [u8]) -> Self {
111 Cow::<'_, [u8]>::Borrowed(slice_body).into()
112 }
113}
114
115impl From<Vec<u8>> for Body {
116 #[inline]
117 fn from(vec_body: Vec<u8>) -> Self {
118 Cow::<'_, [u8]>::Owned(vec_body).into()
119 }
120}
121
122impl From<Cow<'static, [u8]>> for Body {
123 #[inline]
124 fn from(cow_body: Cow<'static, [u8]>) -> Self {
125 Body::new(Full::from(cow_body))
126 }
127}
128
129