Skip to main content

StreamingResponse

Struct StreamingResponse 

Source
pub struct StreamingResponse<S> {
    pub status: StatusCode,
    pub headers: HeaderMap,
    pub stream: S,
}
Expand description

Streaming HTTP Response

Fields§

§status: StatusCode

The HTTP status code.

§headers: HeaderMap

The response headers.

§stream: S

The streaming body source.

Implementations§

Source§

impl<S> StreamingResponse<S>
where S: Stream<Item = Result<Bytes, Box<dyn Error + Send + Sync>>> + Send + 'static,

Source

pub fn new(stream: S) -> Self

Create a new streaming response with OK status

§Examples
use reinhardt_http::StreamingResponse;
use hyper::StatusCode;
use futures::stream;
use bytes::Bytes;

let data = vec![Ok(Bytes::from("chunk1")), Ok(Bytes::from("chunk2"))];
let stream = stream::iter(data);
let response = StreamingResponse::new(stream);

assert_eq!(response.status, StatusCode::OK);
Source

pub fn with_status(stream: S, status: StatusCode) -> Self

Create a streaming response with a specific status code

§Examples
use reinhardt_http::StreamingResponse;
use hyper::StatusCode;
use futures::stream;
use bytes::Bytes;

let data = vec![Ok(Bytes::from("data"))];
let stream = stream::iter(data);
let response = StreamingResponse::with_status(stream, StatusCode::PARTIAL_CONTENT);

assert_eq!(response.status, StatusCode::PARTIAL_CONTENT);
Source

pub fn status(self, status: StatusCode) -> Self

Set the status code

§Examples
use reinhardt_http::StreamingResponse;
use hyper::StatusCode;
use futures::stream;
use bytes::Bytes;

let data = vec![Ok(Bytes::from("data"))];
let stream = stream::iter(data);
let response = StreamingResponse::new(stream).status(StatusCode::ACCEPTED);

assert_eq!(response.status, StatusCode::ACCEPTED);
Source

pub fn header(self, key: HeaderName, value: HeaderValue) -> Self

Add a header to the streaming response

§Examples
use reinhardt_http::StreamingResponse;
use hyper::header::{HeaderName, HeaderValue, CACHE_CONTROL};
use futures::stream;
use bytes::Bytes;

let data = vec![Ok(Bytes::from("data"))];
let stream = stream::iter(data);
let response = StreamingResponse::new(stream)
    .header(CACHE_CONTROL, HeaderValue::from_static("no-cache"));

assert_eq!(
    response.headers.get(CACHE_CONTROL).unwrap().to_str().unwrap(),
    "no-cache"
);
Source

pub fn media_type(self, media_type: &str) -> Self

Set the Content-Type header (media type)

§Examples
use reinhardt_http::StreamingResponse;
use hyper::header::CONTENT_TYPE;
use futures::stream;
use bytes::Bytes;

let data = vec![Ok(Bytes::from("data"))];
let stream = stream::iter(data);
let response = StreamingResponse::new(stream).media_type("video/mp4");

assert_eq!(
    response.headers.get(CONTENT_TYPE).unwrap().to_str().unwrap(),
    "video/mp4"
);
Source§

impl<S> StreamingResponse<S>

Source

pub fn into_stream(self) -> S

Consume the response and return the underlying stream

§Examples
use reinhardt_http::StreamingResponse;
use futures::stream::{self, StreamExt};
use bytes::Bytes;

let data = vec![Ok(Bytes::from("chunk1")), Ok(Bytes::from("chunk2"))];
let stream = stream::iter(data);
let response = StreamingResponse::new(stream);

let mut extracted_stream = response.into_stream();
let first_chunk = extracted_stream.next().await.unwrap().unwrap();
assert_eq!(first_chunk, Bytes::from("chunk1"));

Auto Trait Implementations§

§

impl<S> Freeze for StreamingResponse<S>
where S: Freeze,

§

impl<S> RefUnwindSafe for StreamingResponse<S>
where S: RefUnwindSafe,

§

impl<S> Send for StreamingResponse<S>
where S: Send,

§

impl<S> Sync for StreamingResponse<S>
where S: Sync,

§

impl<S> Unpin for StreamingResponse<S>
where S: Unpin,

§

impl<S> UnsafeUnpin for StreamingResponse<S>
where S: UnsafeUnpin,

§

impl<S> UnwindSafe for StreamingResponse<S>
where S: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more