1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-30 18:44:35 +01:00
actix-web/actix-http/src/payload.rs

90 lines
2.2 KiB
Rust
Raw Normal View History

2021-12-22 08:16:07 +01:00
use std::{
mem,
2021-12-22 08:16:07 +01:00
pin::Pin,
task::{Context, Poll},
};
2019-02-07 22:39:15 +01:00
use bytes::Bytes;
2019-12-13 06:24:57 +01:00
use futures_core::Stream;
2019-02-07 22:39:15 +01:00
use crate::error::PayloadError;
/// A boxed payload stream.
pub type BoxedPayloadStream = Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>>>>;
2019-02-12 20:07:42 +01:00
#[deprecated(since = "4.0.0", note = "Renamed to `BoxedPayloadStream`.")]
pub type PayloadStream = BoxedPayloadStream;
pin_project_lite::pin_project! {
/// 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 22:39:15 +01:00
}
2019-02-13 22:52:11 +01:00
impl<S> From<crate::h1::Payload> for Payload<S> {
fn from(payload: crate::h1::Payload) -> Self {
Payload::H1 { payload }
2019-02-07 22:39:15 +01:00
}
}
2019-02-13 22:52:11 +01:00
impl<S> From<crate::h2::Payload> for Payload<S> {
fn from(payload: crate::h2::Payload) -> Self {
Payload::H2 { payload }
2019-02-12 20:07:42 +01:00
}
}
impl<S> From<h2::RecvStream> for Payload<S> {
fn from(stream: h2::RecvStream) -> Self {
Payload::H2 {
payload: crate::h2::Payload::new(stream),
}
2019-02-12 20:07:42 +01:00
}
}
impl From<BoxedPayloadStream> for Payload {
fn from(payload: BoxedPayloadStream) -> Self {
Payload::Stream { payload }
2019-02-12 20:07:42 +01:00
}
}
2019-03-03 03:37:09 +01:00
impl<S> Payload<S> {
/// Takes current payload and replaces it with `None` value
pub fn take(&mut self) -> Payload<S> {
mem::replace(self, Payload::None)
2019-03-03 03:37:09 +01:00
}
}
2019-02-12 20:07:42 +01:00
impl<S> Stream for Payload<S>
where
S: Stream<Item = Result<Bytes, PayloadError>>,
2019-02-12 20:07:42 +01:00
{
type Item = Result<Bytes, PayloadError>;
2019-02-07 22:39:15 +01:00
#[inline]
2021-12-08 07:01:11 +01:00
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
match self.project() {
PayloadProj::None => Poll::Ready(None),
PayloadProj::H1 { payload } => Pin::new(payload).poll_next(cx),
PayloadProj::H2 { payload } => Pin::new(payload).poll_next(cx),
PayloadProj::Stream { payload } => payload.poll_next(cx),
2019-02-07 22:39:15 +01:00
}
}
}
#[cfg(test)]
mod tests {
use std::panic::{RefUnwindSafe, UnwindSafe};
use static_assertions::{assert_impl_all, assert_not_impl_any};
use super::*;
assert_impl_all!(Payload: Unpin);
assert_not_impl_any!(Payload: Send, Sync, UnwindSafe, RefUnwindSafe);
}