faucet_server/client/
body.rs

1use std::pin::Pin;
2
3use super::pool::HttpConnection;
4use crate::error::FaucetError;
5use http_body_util::{BodyExt, Empty, Full};
6use hyper::body::{Body, Bytes, SizeHint};
7
8pub struct ExclusiveBody {
9    inner: Pin<Box<dyn Body<Data = Bytes, Error = FaucetError> + Send + 'static>>,
10    _connection: Option<HttpConnection>,
11}
12
13impl core::fmt::Debug for ExclusiveBody {
14    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
15        f.debug_struct("ExclusiveBody").finish()
16    }
17}
18
19impl ExclusiveBody {
20    pub fn new(
21        body: impl Body<Data = Bytes, Error = FaucetError> + Send + Sync + 'static,
22        connection: Option<HttpConnection>,
23    ) -> Self {
24        Self {
25            inner: Box::pin(body),
26            _connection: connection,
27        }
28    }
29    pub fn empty() -> Self {
30        Self::new(Empty::new().map_err(Into::into), None)
31    }
32    pub fn plain_text(text: impl Into<String>) -> Self {
33        Self::new(Full::from(text.into()).map_err(Into::into), None)
34    }
35}
36
37impl Body for ExclusiveBody {
38    type Data = Bytes;
39    type Error = FaucetError;
40    fn poll_frame(
41        mut self: std::pin::Pin<&mut Self>,
42        cx: &mut std::task::Context<'_>,
43    ) -> std::task::Poll<Option<Result<hyper::body::Frame<Self::Data>, Self::Error>>> {
44        self.inner.as_mut().poll_frame(cx)
45    }
46    fn is_end_stream(&self) -> bool {
47        self.inner.is_end_stream()
48    }
49    fn size_hint(&self) -> SizeHint {
50        self.inner.size_hint()
51    }
52}