2019-11-15 09:54:11 +00:00
|
|
|
use std::pin::Pin;
|
|
|
|
use std::task::{Context, Poll};
|
|
|
|
|
2019-02-07 21:39:15 +00:00
|
|
|
use bytes::Bytes;
|
2019-11-15 09:54:11 +00:00
|
|
|
use futures::Stream;
|
2019-02-07 21:39:15 +00:00
|
|
|
use h2::RecvStream;
|
|
|
|
|
|
|
|
use crate::error::PayloadError;
|
|
|
|
|
2019-02-12 19:07:42 +00:00
|
|
|
/// Type represent boxed payload
|
2019-11-15 09:54:11 +00:00
|
|
|
pub type PayloadStream = Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>>>>;
|
2019-02-12 19:07:42 +00:00
|
|
|
|
|
|
|
/// Type represent streaming payload
|
|
|
|
pub enum Payload<S = PayloadStream> {
|
|
|
|
None,
|
2019-02-07 21:39:15 +00:00
|
|
|
H1(crate::h1::Payload),
|
|
|
|
H2(crate::h2::Payload),
|
2019-02-12 19:07:42 +00:00
|
|
|
Stream(S),
|
2019-02-07 21:39:15 +00:00
|
|
|
}
|
|
|
|
|
2019-02-13 21:52:11 +00:00
|
|
|
impl<S> From<crate::h1::Payload> for Payload<S> {
|
|
|
|
fn from(v: crate::h1::Payload) -> Self {
|
|
|
|
Payload::H1(v)
|
2019-02-07 21:39:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-13 21:52:11 +00:00
|
|
|
impl<S> From<crate::h2::Payload> for Payload<S> {
|
|
|
|
fn from(v: crate::h2::Payload) -> Self {
|
|
|
|
Payload::H2(v)
|
2019-02-12 19:07:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-13 21:52:11 +00:00
|
|
|
impl<S> From<RecvStream> for Payload<S> {
|
|
|
|
fn from(v: RecvStream) -> Self {
|
|
|
|
Payload::H2(crate::h2::Payload::new(v))
|
2019-02-12 19:07:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<PayloadStream> for Payload {
|
|
|
|
fn from(pl: PayloadStream) -> Self {
|
|
|
|
Payload::Stream(pl)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-03 02:37:09 +00:00
|
|
|
impl<S> Payload<S> {
|
|
|
|
/// Takes current payload and replaces it with `None` value
|
2019-03-18 16:44:48 +00:00
|
|
|
pub fn take(&mut self) -> Payload<S> {
|
2019-03-03 02:37:09 +00:00
|
|
|
std::mem::replace(self, Payload::None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-12 19:07:42 +00:00
|
|
|
impl<S> Stream for Payload<S>
|
|
|
|
where
|
2019-11-15 09:54:11 +00:00
|
|
|
S: Stream<Item = Result<Bytes, PayloadError>> + Unpin,
|
2019-02-12 19:07:42 +00:00
|
|
|
{
|
2019-11-15 09:54:11 +00:00
|
|
|
type Item = Result<Bytes, PayloadError>;
|
2019-02-07 21:39:15 +00:00
|
|
|
|
2019-04-13 21:50:54 +00:00
|
|
|
#[inline]
|
2019-11-15 09:54:11 +00:00
|
|
|
fn poll_next(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
|
|
|
|
match self.get_mut() {
|
|
|
|
Payload::None => Poll::Ready(None),
|
|
|
|
Payload::H1(ref mut pl) => pl.readany(cx),
|
|
|
|
Payload::H2(ref mut pl) => Pin::new(pl).poll_next(cx),
|
|
|
|
Payload::Stream(ref mut pl) => Pin::new(pl).poll_next(cx),
|
2019-02-07 21:39:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|