generic_async_http_client/
body.rs

1use crate::imp;
2
3/*use std::task::{Poll, Context};
4use std::pin::Pin;
5
6use futures_core::Stream;*/
7/*
8#[cfg(feature = "use_async_h1")]
9use async_std::io::prelude::{AsyncRead, AsyncBufRead};
10*/
11
12/// A Body for the Request. You will most likely use [`Request::body`](./struct.Request.html#method.body) directly.
13pub struct Body(imp::Body);
14impl From<String> for Body {
15    #[inline]
16    fn from(t: String) -> Self {
17        Body(imp::Body::from(t))
18    }
19}
20impl From<Vec<u8>> for Body {
21    #[inline]
22    fn from(t: Vec<u8>) -> Self {
23        Body(imp::Body::from(t))
24    }
25}
26impl From<&'static [u8]> for Body {
27    #[inline]
28    fn from(t: &'static [u8]) -> Self {
29        Body(imp::Body::from(t))
30    }
31}
32impl From<&'static str> for Body {
33    #[inline]
34    fn from(t: &'static str) -> Self {
35        Body(imp::Body::from(t))
36    }
37}
38
39impl From<Body> for imp::Body {
40    #[inline]
41    fn from(t: Body) -> Self {
42        t.0
43    }
44}
45impl From<imp::Body> for Body {
46    #[inline]
47    fn from(t: imp::Body) -> Self {
48        Body(t)
49    }
50}
51
52/*/TODO Stream to server -> AsyncWrite for Body
53impl Stream for Body {
54    type Item = Result<&'a [u8], imp::Error>;
55
56    #[cfg(feature = "use_hyper")]
57    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
58        Ok(self.0.poll_next(cx).map(|b|&b)?)
59    }
60    #[cfg(feature = "use_async_h1")]
61    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
62        Ok(self.0.poll_fill_buf(cx)?)
63    }
64}*/
65/*
66#[cfg(feature = "use_async_h1")]
67impl AsyncRead for Body {
68    #[allow(missing_doc_code_examples)]
69    fn poll_read(
70        mut self: Pin<&mut Self>,
71        cx: &mut Context<'_>,
72        buf: &mut [u8],
73    ) -> Poll<io::Result<usize>> {
74        let mut buf = match self.length {
75            None => buf,
76            Some(length) if length == self.bytes_read => return Poll::Ready(Ok(0)),
77            Some(length) => {
78                let max_len = (length - self.bytes_read).min(buf.len());
79                &mut buf[0..max_len]
80            }
81        };
82
83        let bytes = ready!(Pin::new(&mut self.reader).poll_read(cx, &mut buf))?;
84        self.bytes_read += bytes;
85        Poll::Ready(Ok(bytes))
86    }
87}
88#[cfg(feature = "use_async_h1")]
89impl AsyncBufRead for Body {
90    #[allow(missing_doc_code_examples)]
91    fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<&'_ [u8]>> {
92        self.project().reader.poll_fill_buf(cx)
93    }
94
95    fn consume(mut self: Pin<&mut Self>, amt: usize) {
96        Pin::new(&mut self.reader).consume(amt)
97    }
98}*/