2021-12-22 07:16:07 +00:00
|
|
|
use std::{
|
2021-12-24 17:47:47 +00:00
|
|
|
mem,
|
2021-12-22 07:16:07 +00:00
|
|
|
pin::Pin,
|
|
|
|
task::{Context, Poll},
|
|
|
|
};
|
2019-11-15 09:54:11 +00:00
|
|
|
|
2019-02-07 21:39:15 +00:00
|
|
|
use bytes::Bytes;
|
2019-12-13 05:24:57 +00:00
|
|
|
use futures_core::Stream;
|
2022-01-23 23:26:35 +00:00
|
|
|
use pin_project_lite::pin_project;
|
2019-02-07 21:39:15 +00:00
|
|
|
|
|
|
|
use crate::error::PayloadError;
|
|
|
|
|
2021-12-24 17:47:47 +00:00
|
|
|
/// A boxed payload stream.
|
|
|
|
pub type BoxedPayloadStream = Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>>>>;
|
2019-02-12 19:07:42 +00:00
|
|
|
|
2022-03-09 18:11:06 +00:00
|
|
|
#[doc(hidden)]
|
|
|
|
#[deprecated(since = "3.0.0", note = "Renamed to `BoxedPayloadStream`.")]
|
2021-12-24 17:47:47 +00:00
|
|
|
pub type PayloadStream = BoxedPayloadStream;
|
|
|
|
|
2022-01-31 21:22:23 +00:00
|
|
|
#[cfg(not(feature = "http2"))]
|
|
|
|
pin_project! {
|
|
|
|
/// A streaming payload.
|
|
|
|
#[project = PayloadProj]
|
|
|
|
pub enum Payload<S = BoxedPayloadStream> {
|
|
|
|
None,
|
|
|
|
H1 { payload: crate::h1::Payload },
|
|
|
|
Stream { #[pin] payload: S },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "http2")]
|
2022-01-23 23:26:35 +00:00
|
|
|
pin_project! {
|
2021-12-24 17:47:47 +00:00
|
|
|
/// A streaming payload.
|
|
|
|
#[project = PayloadProj]
|
|
|
|
pub enum Payload<S = BoxedPayloadStream> {
|
|
|
|
None,
|
|
|
|
H1 { payload: crate::h1::Payload },
|
|
|
|
H2 { payload: crate::h2::Payload },
|
|
|
|
Stream { #[pin] payload: 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> {
|
2021-12-24 17:47:47 +00:00
|
|
|
fn from(payload: crate::h1::Payload) -> Self {
|
|
|
|
Payload::H1 { payload }
|
2019-02-07 21:39:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-31 21:22:23 +00:00
|
|
|
#[cfg(feature = "http2")]
|
2019-02-13 21:52:11 +00:00
|
|
|
impl<S> From<crate::h2::Payload> for Payload<S> {
|
2021-12-24 17:47:47 +00:00
|
|
|
fn from(payload: crate::h2::Payload) -> Self {
|
|
|
|
Payload::H2 { payload }
|
2019-02-12 19:07:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-31 21:22:23 +00:00
|
|
|
#[cfg(feature = "http2")]
|
|
|
|
impl<S> From<::h2::RecvStream> for Payload<S> {
|
|
|
|
fn from(stream: ::h2::RecvStream) -> Self {
|
2021-12-24 17:47:47 +00:00
|
|
|
Payload::H2 {
|
|
|
|
payload: crate::h2::Payload::new(stream),
|
|
|
|
}
|
2019-02-12 19:07:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-24 17:47:47 +00:00
|
|
|
impl From<BoxedPayloadStream> for Payload {
|
|
|
|
fn from(payload: BoxedPayloadStream) -> Self {
|
|
|
|
Payload::Stream { payload }
|
2019-02-12 19:07:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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> {
|
2021-12-24 17:47:47 +00:00
|
|
|
mem::replace(self, Payload::None)
|
2019-03-03 02:37:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-12 19:07:42 +00:00
|
|
|
impl<S> Stream for Payload<S>
|
|
|
|
where
|
2021-12-24 17:47:47 +00:00
|
|
|
S: Stream<Item = Result<Bytes, PayloadError>>,
|
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]
|
2021-12-08 06:01:11 +00:00
|
|
|
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
2021-12-24 17:47:47 +00:00
|
|
|
match self.project() {
|
|
|
|
PayloadProj::None => Poll::Ready(None),
|
|
|
|
PayloadProj::H1 { payload } => Pin::new(payload).poll_next(cx),
|
2022-01-31 21:22:23 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "http2")]
|
2021-12-24 17:47:47 +00:00
|
|
|
PayloadProj::H2 { payload } => Pin::new(payload).poll_next(cx),
|
2022-01-31 21:22:23 +00:00
|
|
|
|
2021-12-24 17:47:47 +00:00
|
|
|
PayloadProj::Stream { payload } => payload.poll_next(cx),
|
2019-02-07 21:39:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-24 17:47:47 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use static_assertions::{assert_impl_all, assert_not_impl_any};
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
assert_impl_all!(Payload: Unpin);
|
2022-07-24 01:47:12 +00:00
|
|
|
assert_not_impl_any!(Payload: Send, Sync);
|
2021-12-24 17:47:47 +00:00
|
|
|
}
|