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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//! Body types.
//!
//! All these are wrappers around other body types. You shouldn't have to use them in your code.
//! Use `http-body-util` instead.
//!
//! They exist because we don't want to expose types from `http-body-util` in `tower-http`s public
//! API.

#![allow(missing_docs)]

use std::convert::Infallible;

use bytes::{Buf, Bytes};
use http_body::Body;
use pin_project_lite::pin_project;

use crate::BoxError;

macro_rules! body_methods {
    () => {
        #[inline]
        fn poll_frame(
            self: std::pin::Pin<&mut Self>,
            cx: &mut std::task::Context<'_>,
        ) -> std::task::Poll<Option<Result<http_body::Frame<Self::Data>, Self::Error>>> {
            self.project().inner.poll_frame(cx)
        }

        #[inline]
        fn is_end_stream(&self) -> bool {
            Body::is_end_stream(&self.inner)
        }

        #[inline]
        fn size_hint(&self) -> http_body::SizeHint {
            Body::size_hint(&self.inner)
        }
    };
}

pin_project! {
    #[derive(Default)]
    pub struct Full {
        #[pin]
        pub(crate) inner: http_body_util::Full<Bytes>
    }
}

impl Full {
    #[allow(dead_code)]
    pub(crate) fn new(inner: http_body_util::Full<Bytes>) -> Self {
        Self { inner }
    }
}

impl Body for Full {
    type Data = Bytes;
    type Error = Infallible;

    body_methods!();
}

pin_project! {
    pub struct Limited<B> {
        #[pin]
        pub(crate) inner: http_body_util::Limited<B>
    }
}

impl<B> Limited<B> {
    #[allow(dead_code)]
    pub(crate) fn new(inner: http_body_util::Limited<B>) -> Self {
        Self { inner }
    }
}

impl<B> Body for Limited<B>
where
    B: Body,
    B::Error: Into<BoxError>,
{
    type Data = B::Data;
    type Error = BoxError;

    body_methods!();
}

pin_project! {
    pub struct UnsyncBoxBody<D, E> {
        #[pin]
        pub(crate) inner: http_body_util::combinators::UnsyncBoxBody<D, E>
    }
}

impl<D, E> Default for UnsyncBoxBody<D, E>
where
    D: Buf + 'static,
{
    fn default() -> Self {
        Self {
            inner: Default::default(),
        }
    }
}

impl<D, E> UnsyncBoxBody<D, E> {
    #[allow(dead_code)]
    pub(crate) fn new(inner: http_body_util::combinators::UnsyncBoxBody<D, E>) -> Self {
        Self { inner }
    }
}

impl<D, E> Body for UnsyncBoxBody<D, E>
where
    D: Buf,
{
    type Data = D;
    type Error = E;

    body_methods!();
}