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

65 lines
1.5 KiB
Rust
Raw Normal View History

2019-02-07 22:39:15 +01:00
use bytes::Bytes;
2019-02-12 20:07:42 +01:00
use futures::{Async, Poll, Stream};
2019-02-07 22:39:15 +01:00
use h2::RecvStream;
use crate::error::PayloadError;
2019-02-12 20:07:42 +01:00
/// Type represent boxed payload
pub type PayloadStream = Box<dyn Stream<Item = Bytes, Error = PayloadError>>;
/// Type represent streaming payload
pub enum Payload<S = PayloadStream> {
None,
2019-02-07 22:39:15 +01:00
H1(crate::h1::Payload),
H2(crate::h2::Payload),
2019-02-12 20:07:42 +01:00
Stream(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(v: crate::h1::Payload) -> Self {
Payload::H1(v)
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(v: crate::h2::Payload) -> Self {
Payload::H2(v)
2019-02-12 20:07:42 +01:00
}
}
2019-02-13 22:52:11 +01:00
impl<S> From<RecvStream> for Payload<S> {
fn from(v: RecvStream) -> Self {
Payload::H2(crate::h2::Payload::new(v))
2019-02-12 20:07:42 +01:00
}
}
impl From<PayloadStream> for Payload {
fn from(pl: PayloadStream) -> Self {
Payload::Stream(pl)
}
}
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> {
2019-03-03 03:37:09 +01:00
std::mem::replace(self, Payload::None)
}
}
2019-02-12 20:07:42 +01:00
impl<S> Stream for Payload<S>
where
S: Stream<Item = Bytes, Error = PayloadError>,
{
2019-02-07 22:39:15 +01:00
type Item = Bytes;
type Error = PayloadError;
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
match self {
2019-02-12 20:07:42 +01:00
Payload::None => Ok(Async::Ready(None)),
2019-02-07 22:39:15 +01:00
Payload::H1(ref mut pl) => pl.poll(),
Payload::H2(ref mut pl) => pl.poll(),
2019-02-12 20:07:42 +01:00
Payload::Stream(ref mut pl) => pl.poll(),
2019-02-07 22:39:15 +01:00
}
}
}