tower_http/
body.rs

1//! Body types.
2//!
3//! All these are wrappers around other body types. You shouldn't have to use them in your code.
4//! Use `http-body-util` instead.
5//!
6//! They exist because we don't want to expose types from `http-body-util` in `tower-http`s public
7//! API.
8
9#![allow(missing_docs)]
10
11use std::convert::Infallible;
12
13use bytes::{Buf, Bytes};
14use http_body::Body;
15use pin_project_lite::pin_project;
16
17use crate::BoxError;
18
19macro_rules! body_methods {
20    () => {
21        #[inline]
22        fn poll_frame(
23            self: std::pin::Pin<&mut Self>,
24            cx: &mut std::task::Context<'_>,
25        ) -> std::task::Poll<Option<Result<http_body::Frame<Self::Data>, Self::Error>>> {
26            self.project().inner.poll_frame(cx)
27        }
28
29        #[inline]
30        fn is_end_stream(&self) -> bool {
31            Body::is_end_stream(&self.inner)
32        }
33
34        #[inline]
35        fn size_hint(&self) -> http_body::SizeHint {
36            Body::size_hint(&self.inner)
37        }
38    };
39}
40
41pin_project! {
42    #[derive(Default)]
43    pub struct Full {
44        #[pin]
45        pub(crate) inner: http_body_util::Full<Bytes>
46    }
47}
48
49impl Full {
50    #[allow(dead_code)]
51    pub(crate) fn new(inner: http_body_util::Full<Bytes>) -> Self {
52        Self { inner }
53    }
54}
55
56impl Body for Full {
57    type Data = Bytes;
58    type Error = Infallible;
59
60    body_methods!();
61}
62
63pin_project! {
64    pub struct Limited<B> {
65        #[pin]
66        pub(crate) inner: http_body_util::Limited<B>
67    }
68}
69
70impl<B> Limited<B> {
71    #[allow(dead_code)]
72    pub(crate) fn new(inner: http_body_util::Limited<B>) -> Self {
73        Self { inner }
74    }
75}
76
77impl<B> Body for Limited<B>
78where
79    B: Body,
80    B::Error: Into<BoxError>,
81{
82    type Data = B::Data;
83    type Error = BoxError;
84
85    body_methods!();
86}
87
88pin_project! {
89    pub struct UnsyncBoxBody<D, E> {
90        #[pin]
91        pub(crate) inner: http_body_util::combinators::UnsyncBoxBody<D, E>
92    }
93}
94
95impl<D, E> Default for UnsyncBoxBody<D, E>
96where
97    D: Buf + 'static,
98{
99    fn default() -> Self {
100        Self {
101            inner: Default::default(),
102        }
103    }
104}
105
106impl<D, E> UnsyncBoxBody<D, E> {
107    #[allow(dead_code)]
108    pub(crate) fn new(inner: http_body_util::combinators::UnsyncBoxBody<D, E>) -> Self {
109        Self { inner }
110    }
111}
112
113impl<D, E> Body for UnsyncBoxBody<D, E>
114where
115    D: Buf,
116{
117    type Data = D;
118    type Error = E;
119
120    body_methods!();
121}