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
//! Types and traits for request and response bodies.

// ----------

use std::{
	pin::{pin, Pin},
	task::{Context, Poll},
};

use http_body_util::BodyExt;

use crate::BoxedError;

// ----------

pub use bytes::*;
pub use http_body::Body as HttpBody;
pub use http_body::{Frame, SizeHint};

// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------

type BoxedBody = http_body_util::combinators::BoxBody<Bytes, BoxedError>;

// --------------------------------------------------

/// An [`HttpBody`] type used in requests and responses.
#[derive(Debug, Default)]
pub struct Body(BoxedBody);

impl Body {
	#[inline(always)]
	pub fn new<B>(body: B) -> Self
	where
		B: HttpBody<Data = Bytes> + Sized + Send + Sync + 'static,
		B::Error: Into<BoxedError>,
	{
		Self(BoxedBody::new(body.map_err(Into::into)))
	}
}

impl HttpBody for Body {
	type Data = Bytes;
	type Error = BoxedError;

	#[inline(always)]
	fn poll_frame(
		mut self: Pin<&mut Self>,
		cx: &mut Context<'_>,
	) -> Poll<Option<Result<Frame<Self::Data>, Self::Error>>> {
		pin!(&mut self.0).poll_frame(cx)
	}
}

// --------------------------------------------------------------------------------